Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place! |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
05-11-2012, 07:28 AM
|
#1
|
LQ Newbie
Registered: May 2012
Posts: 6
Rep: 
|
my shell Script stops after doing ssh to remote server
Hi,
Could please anyone help me with my script! My Linux version is
uname -a
Linux Hostname 2.6.18-164.6.1.el5 #1 SMP Tue Oct 27 11:28:30 EDT 2009 x86_64 x86_64 x86_64 GNU/Linux
I have done the keygen part and while logging through script it doesn't ask for any password. But after doing ssh, the scripts just logs into the remote server and not processing further. My script goes likes this.....
[START MY_SCRIPT]
#!/bin/bash
ssh remote-host
retVal=$?
if [ "$retVal" -eq "0" ]; then
cd /apps ;
fi
echo 'Script Completed' ;
[END MY_SCRIPT]
Basically it's not able to get into the /apps path.
Any help would be greatly appreciated.
Thanks,
Shasank
|
|
|
05-11-2012, 07:36 AM
|
#2
|
LQ 5k Club
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
|
You can investigate why the script behaves as it does by entering the same commands at the command prompt.
|
|
|
05-11-2012, 07:47 AM
|
#3
|
LQ Newbie
Registered: May 2012
Posts: 6
Original Poster
Rep: 
|
Well, i did and below are the steps:
1) ssh remote-host
2) cd /apps
And its working fine. 
|
|
|
05-11-2012, 07:55 AM
|
#4
|
LQ 5k Club
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
|
What did you do after running ssh remote-host to make it terminate? Did you provide any keyboard input?
|
|
|
05-11-2012, 07:57 AM
|
#5
|
LQ Newbie
Registered: May 2012
Posts: 6
Original Poster
Rep: 
|
Well, i did and below are the steps:
1) ssh remote-host
2) cd /apps

|
|
|
05-11-2012, 08:00 AM
|
#6
|
LQ 5k Club
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
|
Did you provide any keyboard input to terminate the ssh remote-host command?
|
|
|
05-11-2012, 08:46 AM
|
#7
|
LQ Newbie
Registered: May 2012
Posts: 6
Original Poster
Rep: 
|
Yes, after logging into remote-host. I use 'exit' command in order to again come back to my local-host.
|
|
|
05-11-2012, 08:54 AM
|
#8
|
LQ Addict
Registered: Dec 2011
Location: UK
Distribution: Debian Sid AMD64, Raspbian Wheezy, various VMs
Posts: 7,680
|
Isn't the problem here that the script is running on the local host, so once you ssh into the remote host it suspends until you return? At least that's what happens when I try this.
Edit: I was wondering how this could be done myself and I came across this, which may be useful:
http://stackoverflow.com/questions/3...remote-machine
Last edited by 273; 05-11-2012 at 08:59 AM.
|
|
|
05-11-2012, 09:32 AM
|
#9
|
LQ 5k Club
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
|
Quote:
Originally Posted by Shasank
Yes, after logging into remote-host. I use 'exit' command in order to again come back to my local-host.
|
So your script would need a way to do something similar or the ssh session waits indefinitely for input.
|
|
|
05-11-2012, 09:41 AM
|
#10
|
LQ Newbie
Registered: May 2012
Posts: 6
Original Poster
Rep: 
|
@catkin, nice thought, but still it didn't worked.
[START MY_SCRIPT]
#!/bin/bash
ssh remote-host
retVal=$?
if [ "$retVal" -eq "0" ]; then
cd /apps ;
fi
exit ;
echo 'Script Completed' ;
[END MY_SCRIPT]
|
|
|
05-11-2012, 09:47 AM
|
#11
|
LQ Guru
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573
|
Quote:
Originally Posted by Shasank
@catkin, nice thought, but still it didn't worked.
|
The ssh command is opening a new shell. Your script is still stuck in the old shell running the ssh command.
Think about it this way...
If you were at the terminal, and typed ssh, you would then be connected to the other computer. You could then do whatever you wanted, run whatever you wanted, etc. When you typed "exit", your ssh command would end and it would drop you back to the previous shell. THIS is what your script is waiting for. At this point your script would run its next command, "retVal=$?".
Your script will not continue on to the retval, or the cd, etc., until the ssh command has terminated. The ssh command will not terminate until you FINISH whatever it is you are doing on the other machine and return back to the original terminal.
Your script is not stopping or getting stuck after running the ssh, it's simply waiting for the ssh to exit so that it can continue running the script.
Hopefully that makes sense.
Last edited by suicidaleggroll; 05-11-2012 at 09:54 AM.
|
|
|
05-11-2012, 09:51 AM
|
#12
|
Member
Registered: Nov 2011
Location: Germany, Bavaria, Nueremberg area
Distribution: openSUSE, Debian, LFS
Posts: 205
Rep:
|
The return value of "ssh remote-host" is the return code of the last executed command on the remote-host box.
Maybe this is not 0
|
|
|
05-11-2012, 10:03 AM
|
#13
|
Moderator
Registered: Aug 2002
Posts: 26,858
|
It depends on what you are actually trying to accomplish.
You can execute a script or commands directly i.e.
ssh user@someserver 'ls -l;'
If your task requires sending commands interactively then you might need to write an expect script.
|
|
|
05-11-2012, 10:10 AM
|
#14
|
LQ Newbie
Registered: May 2012
Posts: 6
Original Poster
Rep: 
|
@uhelp, it's returning value as 0.
@273, thanks for the wonderful link. It solved my problem.
Now, i will be writing two scripts in the local machine.
will be having a wrapper script will do the necessary stuff in the remote-host.
The link which @273 mentioned had something like this....
ssh root@MachineB 'bash -s' < local_script.sh
You shouldn't have to copy the script to the remote server to run it.
So, i will have wrapper script which will solve my problem.
[START MY_SCRIPT]
#!/bin/bash
ssh remote-host 'bash -s' < main_script.sh
[END MY_SCRIPT]
Thanks a lot for all your inputs... Very much appreciated. I thought my problem would take some time to get solved, but amanzingly it got solved within few hours... Thanks again.. 
|
|
|
05-11-2012, 10:17 AM
|
#15
|
LQ Addict
Registered: Dec 2011
Location: UK
Distribution: Debian Sid AMD64, Raspbian Wheezy, various VMs
Posts: 7,680
|
Glad that solved your problem. Please use the link towards the top of the page to mark this thread solved. 
|
|
|
All times are GMT -5. The time now is 04:24 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|