How do I reuse one ssh connection in a shell script?
Linux - SoftwareThis forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.
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.
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.
Introduction to Linux - A Hands on Guide
This guide was created as an overview of the Linux Operating System, geared toward new users as an exploration tour and getting started guide, with exercises at the end of each chapter.
For more advanced trainees it can be a desktop reference, and a collection of the base knowledge needed to proceed with system and network administration. This book contains many real life examples derived from the author's experience as a Linux system and network administrator, trainer and consultant. They hope these examples will help you to get a better understanding of the Linux system and that you feel encouraged to try out things on your own.
Click Here to receive this Complete Guide absolutely free.
How do I reuse one ssh connection in a shell script?
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.
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?
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...
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?
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:
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.
Distribution: Linux Mint, Ubuntu, CentOS, Red Hat Enterprise Linux, Fedora
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...
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.