LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   my shell Script stops after doing ssh to remote server (https://www.linuxquestions.org/questions/linux-newbie-8/my-shell-script-stops-after-doing-ssh-to-remote-server-944395/)

Shasank 05-11-2012 07:28 AM

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

catkin 05-11-2012 07:36 AM

You can investigate why the script behaves as it does by entering the same commands at the command prompt.

Shasank 05-11-2012 07:47 AM

Well, i did and below are the steps:

1) ssh remote-host
2) cd /apps

And its working fine. :)

catkin 05-11-2012 07:55 AM

What did you do after running ssh remote-host to make it terminate? Did you provide any keyboard input?

Shasank 05-11-2012 07:57 AM

Well, i did and below are the steps:

1) ssh remote-host
2) cd /apps

:)

catkin 05-11-2012 08:00 AM

Did you provide any keyboard input to terminate the ssh remote-host command?

Shasank 05-11-2012 08:46 AM

Yes, after logging into remote-host. I use 'exit' command in order to again come back to my local-host.

273 05-11-2012 08:54 AM

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

catkin 05-11-2012 09:32 AM

Quote:

Originally Posted by Shasank (Post 4675934)
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.

Shasank 05-11-2012 09:41 AM

@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]

suicidaleggroll 05-11-2012 09:47 AM

Quote:

Originally Posted by Shasank (Post 4675982)
@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.

uhelp 05-11-2012 09:51 AM

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

michaelk 05-11-2012 10:03 AM

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.

Shasank 05-11-2012 10:10 AM

@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.. :)

273 05-11-2012 10:17 AM

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 07:55 PM.