LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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


Reply
  Search this Thread
Old 05-11-2012, 08:28 AM   #1
Shasank
LQ Newbie
 
Registered: May 2012
Posts: 6

Rep: Reputation: Disabled
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
 
Old 05-11-2012, 08:36 AM   #2
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
You can investigate why the script behaves as it does by entering the same commands at the command prompt.
 
Old 05-11-2012, 08:47 AM   #3
Shasank
LQ Newbie
 
Registered: May 2012
Posts: 6

Original Poster
Rep: Reputation: Disabled
Well, i did and below are the steps:

1) ssh remote-host
2) cd /apps

And its working fine.
 
Old 05-11-2012, 08:55 AM   #4
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
What did you do after running ssh remote-host to make it terminate? Did you provide any keyboard input?
 
Old 05-11-2012, 08:57 AM   #5
Shasank
LQ Newbie
 
Registered: May 2012
Posts: 6

Original Poster
Rep: Reputation: Disabled
Well, i did and below are the steps:

1) ssh remote-host
2) cd /apps

 
Old 05-11-2012, 09:00 AM   #6
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
Did you provide any keyboard input to terminate the ssh remote-host command?
 
Old 05-11-2012, 09:46 AM   #7
Shasank
LQ Newbie
 
Registered: May 2012
Posts: 6

Original Poster
Rep: Reputation: Disabled
Yes, after logging into remote-host. I use 'exit' command in order to again come back to my local-host.
 
Old 05-11-2012, 09:54 AM   #8
273
LQ Addict
 
Registered: Dec 2011
Location: UK
Distribution: Debian Sid AMD64, Raspbian Wheezy, various VMs
Posts: 6,852

Rep: Reputation: 1952Reputation: 1952Reputation: 1952Reputation: 1952Reputation: 1952Reputation: 1952Reputation: 1952Reputation: 1952Reputation: 1952Reputation: 1952Reputation: 1952
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 09:59 AM.
 
Old 05-11-2012, 10:32 AM   #9
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
Quote:
Originally Posted by Shasank View Post
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.
 
Old 05-11-2012, 10:41 AM   #10
Shasank
LQ Newbie
 
Registered: May 2012
Posts: 6

Original Poster
Rep: Reputation: Disabled
@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]
 
Old 05-11-2012, 10:47 AM   #11
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,552

Rep: Reputation: 2112Reputation: 2112Reputation: 2112Reputation: 2112Reputation: 2112Reputation: 2112Reputation: 2112Reputation: 2112Reputation: 2112Reputation: 2112Reputation: 2112
Quote:
Originally Posted by Shasank View Post
@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 10:54 AM.
 
Old 05-11-2012, 10:51 AM   #12
uhelp
Member
 
Registered: Nov 2011
Location: Germany, Bavaria, Nueremberg area
Distribution: openSUSE, Debian, LFS
Posts: 205

Rep: Reputation: 43
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
 
Old 05-11-2012, 11:03 AM   #13
michaelk
Moderator
 
Registered: Aug 2002
Posts: 16,324

Rep: Reputation: 1905Reputation: 1905Reputation: 1905Reputation: 1905Reputation: 1905Reputation: 1905Reputation: 1905Reputation: 1905Reputation: 1905Reputation: 1905Reputation: 1905
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.
 
Old 05-11-2012, 11:10 AM   #14
Shasank
LQ Newbie
 
Registered: May 2012
Posts: 6

Original Poster
Rep: Reputation: Disabled
@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..
 
Old 05-11-2012, 11:17 AM   #15
273
LQ Addict
 
Registered: Dec 2011
Location: UK
Distribution: Debian Sid AMD64, Raspbian Wheezy, various VMs
Posts: 6,852

Rep: Reputation: 1952Reputation: 1952Reputation: 1952Reputation: 1952Reputation: 1952Reputation: 1952Reputation: 1952Reputation: 1952Reputation: 1952Reputation: 1952Reputation: 1952
Glad that solved your problem. Please use the link towards the top of the page to mark this thread solved.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to use ssh and run command in the remote machine using shell script salmanmanekia Linux - Newbie 8 04-28-2011 08:41 AM
[SOLVED] Script question: create a shell script in kde to log in on a server with ssh c4719929 Linux - Newbie 1 01-31-2011 04:05 AM
How to call Shell Script on a remote server with remote servers env variables need Linux - Server 1 10-14-2009 09:37 PM
shell script for remote ssh login sefaklc Programming 5 08-28-2007 10:03 PM
exacuting remote comands over SSH with a shell script? dasy2k1 Linux - Software 1 09-05-2006 08:01 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 03:43 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration