PDA

View Full Version : using ssh in a shell script


rossb
12-08-2005, 06:54 PM
Hi,

I have a problem I cannot solve after hours and I'm hoping someone can help.

I am trying to connect to a server through ssh to tunnel file sharing through it. I have set it up with key authentication, etc., so that this is very easily done. All I do is open the terminal, type ssh user@server -c blowfish -X -L 10548:127.0.0.1:548, and then go to the finder & connect to server 127.0.0.1:10548. It works every time. I have no problems here.

THIS is the problem.

I need to script this so I have something to double-click that just does these two steps for me and brings up the file sharing dialogue automatically. What I cannot get to happen is the first step. I have in Automator a "Run shell script" with ssh user@server -c blowfish -X -L 10548:127.0.0.1:548 in it, as I type into the terminal, but when the shell script runs the ssh does not stay open. In fact, even if I have a shell script as so:

ssh user@server
ls

I can see that the "ls" is from my local directory. The ssh is not sticking! If I go like:

ssh user@server ls

on the one line, it lists correctly!!!

It's as if the ssh sessions in shell scripts are set to close themselves within one line always, and never remain open, even to the next line. This is useless!

I am so close and yet so far away! Help!

hayne
12-08-2005, 07:03 PM
When you run a shell script from Automator, I think it is intended that the script should run, do its business, and then quit. That is what you are seeing.
If you need a shell script to stay running, then I think you need a Terminal window to be open with that script running in it. (At least that's the easiest way.) So you should look at targeting Terminal in your Automator actions.

An alternative way to get a double-clickable shell script is to save it in a file with a ".command" suffix.

rossb
12-08-2005, 07:13 PM
When you run a shell script from Automator, I think it is intended that the script should run, do its business, and then quit. That is what you are seeing.
If you need a shell script to stay running, then I think you need a Terminal window to be open with that script running in it. (At least that's the easiest way.)

That's what I was thinking was happening. But even then, the ssh connection is actually closing itself by the next line of the shell script it is WITHIN. If I could get the ssh connection to stay open for as long as the shell script lasts, I think I could do everything in the one shell script, even if things were more complicated than ideal. BUT… I can't get the ssh connection to stay open. Like I say, when I type:

ssh user@server
ls

The ssh connection is made successfully on the first line, but by "ls" it has been closed, and my local directory get's listed.

If I type "ssh user@server ls" all on the one line, I get the listing of the remote computer – this is how I know the ssh connection is being made successfully.

However there is one strange thing… Even when the shell script gives me back the remote listing, I can go open a terminal window and login and it will say the last time the ssh user logged in was before the shell script last ran. So the remote computer is not aware of the shell script having logged into it a moment ago… but it clearly did.

hayne
12-08-2005, 07:26 PM
ssh user@server
ls

The ssh connection is made successfully on the first line, but by "ls" it has been closed, and my local directory get's listed

ssh starts a new shell, so the above commands do the following:
- start a new shell on the remote machine, then close that shell
- run 'ls' in the previously existing shell

rossb
12-08-2005, 07:32 PM
ssh starts a new shell, so the above commands do the following:
- start a new shell on the remote machine, then close that shell
- run 'ls' in the previously existing shell

Okay… Thanks for your quick replies btw! Making much more progress in the last 30 mins than the last 3 hours.

What I don't understand is why the first line has the effect of opening the shell AND THEN CLOSING IT. I just said connect, I never said to close the shell it's just opened.

Is there any way to either stop the first line from closing the remote shell before moving to the next line, or… Is there any way to issue further commands to the new shell it has opened?

Thanks,
Ross

hayne
12-08-2005, 07:44 PM
Is there any way to issue further commands to the new shell it has opened?

You could enter the commands into a file (e.g. one named "foo") and then tell the shell to read from that file instead of reading from the keyboard by using the '<' symbol.

ssh@server < foo

rossb
12-08-2005, 07:48 PM
You could enter the commands into a file (e.g. one named "foo") and then tell the shell to read from that file instead of reading from the keyboard by using the '<' symbol.

ssh@server < foo

Fantastic Hayne, you're a genius!

Thanks,
Ross

hayne
12-09-2005, 11:12 AM
You could enter the commands into a file (e.g. one named "foo") and then tell the shell to read from that file instead of reading from the keyboard by using the '<' symbol.

ssh@server < foo

You could also use a "here document" - that would allow you to have the commands inside the script file:

ssh server <<EOT
first_command
second_command
EOT