Unable to run command after ssh


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Unable to run command after ssh
# 1  
Old 11-28-2012
Unable to run command after ssh

Hello,

I am trying to create a ksh script to login to server and collect gather output of some command to troubleshoot some issue.

Code:
DATE=`date +%b.%d.%Y.%M.%H`
echo " Enter emp id to login to server"
read Eid
Eid=$Eid
echo " Enter hostname of the system"
read HOST
HOST=$HOST
/usr/bin/ssh $Eid@$HOST   <===== It is working upto here
OSVALUE=`uname -s|tail -1`
if [ $OSVALUE = Linux ]
then
/usr/bin/sudo su - root
else
/prod/prop/tools/bin/sudo su - root
fi

issue) My script is working upto marked arrow and it is prompting me for password and i am able to login to server, BUT command after that are not running.


pls gelp

I am beginner in scripting. This is ksh

Last edited by Corona688; 11-28-2012 at 02:50 PM..
# 2  
Old 11-28-2012
If you want to run some commands on a remote machine using ssh, then you have to use below syntax:-
Code:
ssh $user@$host "command1; command2; ..."

E.g.
Code:
ssh $Eid@$HOST "uname; ls"

# 3  
Old 11-28-2012
running 'ssh' does not feed the rest of the script into ssh. It's a separate shell on a completely different system -- it's not reading your script.

If you want to feed a script into it, you have to feed a script into it. You can use what's called a "here-document" to effectively feed a file full of text into a program:

Code:
MYVAR="asdf"
ANOTHERVAR="qwertyuiop"

ssh username@host exec /bin/sh -s "$MYVAR" "$ANOTHERVAR" <<"EOF"

MYVAR="$1"
ANOTHERVAR="$2"

echo "MYVAR is $MYVAR"
echo "ANOTHERVAR is $ANOTHERVAR"

OSVALUE=`uname -s|tail -1`
if [ $OSVALUE = Linux ]
then
/usr/bin/sudo su - root
else
/prod/prop/tools/bin/sudo su - root
fi
EOF

That's a complicated but thorough way to do what bipinajith is doing, except with complete, unaltered, unescaped scripts.

Note the ending EOF must be at the very beginning of the line, do not indent.

If it was <<EOF instead of <<"EOF", the shell would substitute variables inside for you. That's bad -- $OSVALUE would become a blank string immediately, before it even gets sent to the other side.

Also note that variables don't cross. It's a new program on another system, it won't know your variables if you don't tell them it. Note how I pass two variables into the remote script. You can have more of those, and they can be anything you want. They become $1, $2, and so forth.

Last edited by Corona688; 11-28-2012 at 03:13 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Run awk command inside ssh

I am trying to run an awk command inside of ssh and it is not working. These are AIX servers. for i in `cat servers`; do ssh $i "/bin/hostname; df -g | awk '/dev/ && $4+0 > 70'"; done server1 server2 server3 server4 I also tried these two methods and they did not work. It just seemed... (5 Replies)
Discussion started by: cokedude
5 Replies

2. UNIX for Dummies Questions & Answers

Unable to run awk command

hello Don Cragun sir, i am not able to run this command awk 'NR==FNR{if($0~/^>/){i=substr($0,2);getline};a=a $0;next}{print ">" $1 ORS substr(a, $2, $3-$2+1)}' file1 FS=\\t file2 i searched do text you on twitter, facebook but i get no reply from over there so i post here. ---------- Post... (2 Replies)
Discussion started by: harpreetmanku04
2 Replies

3. Shell Programming and Scripting

Script for telnet and run one command kill it and run another command using while loop

( sleep 3 echo ${LOGIN} sleep 2 echo ${PSWD} sleep 2 while read line do echo "$line" PID=$? sleep 2 kill -9 $PID done < temp sleep 5 echo "exit" ) | telnet ${HOST} while is executing only command and exits. (5 Replies)
Discussion started by: sooda
5 Replies

4. Shell Programming and Scripting

Unable to run application using ssh

I'm testing a C++ based application (HLR) in my solaris system. Whenever i start the application remotely from some other solaris server using ssh command the application throws an error and goes down. command i used: ssh root@192.168.151.77 "./start_hlr.sh" Below is the error observed : ... (1 Reply)
Discussion started by: Arun_Linux
1 Replies

5. Shell Programming and Scripting

run command with ssh[solved]

Hi all, Is it possible to make this possible ? $ echo $SKY_HOME /var/sink/SKY $ echo $SKY_HOME /home/smily/SKY $ ssh root@xyz "echo $SKY_HOME" root@xyz 's password: ****** /home/smily/SKY wrong output I was expecting the output as /var/sink/SKY (3 Replies)
Discussion started by: linuxadmin
3 Replies

6. HP-UX

Unable to run some commands in HP-UX

Hi All, I want to get %cpu and %memory utilization for a given process id in HP-UX so am using the following commands 1)TOP -p <PID> am getting error message like Quitting top: pset 26323 doesn't exist,but when am using only TOP command without any options its working fine. 2)ps -e -o pcpu... (5 Replies)
Discussion started by: Ramya_Nm
5 Replies

7. Solaris

Run command on sc via ssh

when i run a command on ALOM via ssh i get following error ssh root@10.23.12.51 showhosts Password: Waiting for daemons to initialize... Daemons ready shell: Invalid credentials how can i run commands without actually loging to the sc (3 Replies)
Discussion started by: fugitive
3 Replies

8. Solaris

Unable to run xclock

Hello. I am trying to run xclock on newly built solaris box - These are the steps I followed: # DISPLAY=localhost:0.0 # export DISPLAY # xclock xclock: not found # cd /usr/openwin/bin # ./xclock Error: Can't open display: localhost:0.0 # Please suggest, what am i doing wrong? Thank... (27 Replies)
Discussion started by: panchpan
27 Replies

9. Shell Programming and Scripting

how to run a command in different machine using SSH

how to run a command in different machie in my case script will runs in solaries machine.. in one instance it has to run a command in different machine with different operating system ( linux ) using SSH command i tried ssh -l (login_name) (machine name/host ) " command " but it is... (3 Replies)
Discussion started by: mail2sant
3 Replies

10. Shell Programming and Scripting

How do I get ssh to run a command in one line?

How would I combine something like: localserver# ssh remoteserver remoteserver# find blah blah blah into a one liner that would ssh to the remote server and run the find command, so I could put it in a script to automatically go out and run things on remote servers with out needed user... (2 Replies)
Discussion started by: LordJezo
2 Replies
Login or Register to Ask a Question