LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (http://www.linuxquestions.org/questions/linux-software-2/)
-   -   How do I reuse one ssh connection in a shell script? (http://www.linuxquestions.org/questions/linux-software-2/how-do-i-reuse-one-ssh-connection-in-a-shell-script-793132/)

jabalsad 03-04-2010 06:55 AM

How do I reuse one ssh connection in a shell script?
 
Hi there,

I'm writing a shell script that does multiple scp's and ssh'es to the same host. I would like to know if there is a way to write the script such that in the beginning an ssh connection is established. Then scp transfers files over the already open ssh connection. After which, terminal commands are executed on the host. A final scp transfers files again over the ssh connection. Then the ssh connection is terminated.

The whole point is *not* to have:

#!/bin/sh

scp the_file host
ssh host 'execute-some-command.sh'
scp host:~/some_file .

# In this scenario, three ssh connections are established and authentication is required for each.
# I'm trying to create a single ssh connection initially and then reuse it.

nuwen52 03-04-2010 08:31 AM

Looks like the idea is to copy a file over to the server, run it, and retrieve it's results.

Personally, I would just run the three commands and be done with it. But, I suppose you could do something where you set up an ssh with port forwarding. And, when it came to the copy, you could use ncat or socat (pointing at the forwarded port) to transfer the file to a waiting copy of the same program (ncat, etc.) which redirects it's output to a file. Then run the file and do the same again in return? But, the way I see this happening, you would need to bind two extra ports on each computer. This is probably overly complex and there might be a much easier answer.

But, is there a problem with using the 3 commands?

computerman1983 03-04-2010 08:57 AM

I would say your best bet would be to exchanged keys with your boxes. This would allow you to login without typing a password. He's a how to. http://oreilly.com/pub/h/66

jabalsad 03-05-2010 12:28 AM

Hi Guys,

Thanks for the replies. In response to nuwen52, yes that does sound a bit more complex than what it needs to be. I had hoped for a command line parameter I can simply pass to ssh that allows other connections to use it. And it should stay alive until the script is terminated. Its not that I don't want to use the three commands, its that I need to reauthenticate for every ssh I need to do (including scp), i.e. type in the password x number of times everytime the script is executed. Three times is still okay, but what if x becomes a large number?

Even though computerman1983 has a point, I can easily install some public keys on the remote host. The problem with this approach is that what if someone else needs to use the script? Then they also need to install public keys. Fair enough. Now what if 50 other people uses the script? It can get a bit messy :)

I have used ssh before to forward a vnc connection, so I'm fairly certain its possible...

chrism01 03-05-2010 12:33 AM

How about ssh-agent?

jabalsad 03-05-2010 07:47 AM

chrism01,

ssh-agent requires the use of public keys, meaning I'll need to have a public key on the remote host anyway. This solution works if I'm the only user of the script, but if there are many users then each of them need a public key installed on the remote host. This gets a bit messy.

Perhaps there is functionality of ssh-agent I'm not aware of?

nuwen52 03-05-2010 07:54 AM

Okay. What about using "expect" to automate the script? With that, you can type the password on the command line once, and it will pass on that password each time that ssh asks for it. Google "expect automated ssh" and there's a lot of links for it. Added with this to get the password from the command line:
Code:

set psswd [lrange $argv 0 0]
Just a thought. This requires that the people logging in have expect available on their workstations. There are things like expect in python and other languages.

Eric K 03-08-2010 08:20 AM

Was any progress made on this front? I am using expect now and am trying to do something similar.

Each of my expect scripts starts by spawning a telnet session and then logging in. I would like to have one log in script that returns a telnet session. That way each of my other scripts can use the already connected session instead of repeatedly logging in, doing something, logging out, repeating...

chrism01 03-08-2010 11:04 PM

In some cases it makes more sense to put the relevant scripts onto the target systems and just call them (once) from the driver/src system, then collect the results.
Otherwise, use Expect is prob the way to go if ssh-keys are in-feasible.


All times are GMT -5. The time now is 05:32 AM.