Linux - Software This 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. |
| Notices |
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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
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.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
03-04-2010, 06:55 AM
|
#1
|
|
LQ Newbie
Registered: Oct 2007
Posts: 5
Rep:
|
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.
|
|
|
|
03-04-2010, 08:31 AM
|
#2
|
|
Member
Registered: Feb 2009
Distribution: CentOS 5, Gentoo, FreeBSD, Fedora, Mint, Slackware64
Posts: 205
Rep:
|
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?
Last edited by nuwen52; 03-04-2010 at 08:49 AM.
|
|
|
|
03-04-2010, 08:57 AM
|
#3
|
|
LQ Newbie
Registered: Aug 2009
Distribution: Ubuntu 9.10, Red Hat WS 4, CentOS, OpenBSD, and all windows flavs
Posts: 20
Rep:
|
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
|
|
|
|
03-05-2010, 12:28 AM
|
#4
|
|
LQ Newbie
Registered: Oct 2007
Posts: 5
Original Poster
Rep:
|
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...
|
|
|
|
03-05-2010, 12:33 AM
|
#5
|
|
Guru
Registered: Aug 2004
Location: Brisbane
Distribution: Centos 6.4, Centos 5.9
Posts: 15,261
|
How about ssh-agent?
|
|
|
|
03-05-2010, 07:47 AM
|
#6
|
|
LQ Newbie
Registered: Oct 2007
Posts: 5
Original Poster
Rep:
|
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?
|
|
|
|
03-05-2010, 07:54 AM
|
#7
|
|
Member
Registered: Feb 2009
Distribution: CentOS 5, Gentoo, FreeBSD, Fedora, Mint, Slackware64
Posts: 205
Rep:
|
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.
Last edited by nuwen52; 03-05-2010 at 08:06 AM.
|
|
|
|
03-08-2010, 08:20 AM
|
#8
|
|
LQ Newbie
Registered: Mar 2010
Location: Ohio, USA
Distribution: Linux Mint, Ubuntu, CentOS, Red Hat Enterprise Linux, Fedora
Posts: 1
Rep:
|
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...
|
|
|
|
03-08-2010, 11:04 PM
|
#9
|
|
Guru
Registered: Aug 2004
Location: Brisbane
Distribution: Centos 6.4, Centos 5.9
Posts: 15,261
|
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.
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 09:17 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|