LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
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



Reply
 
Search this Thread
Old 03-04-2010, 07:55 AM   #1
jabalsad
LQ Newbie
 
Registered: Oct 2007
Posts: 5

Rep: Reputation: 0
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.
 
Old 03-04-2010, 09:31 AM   #2
nuwen52
Member
 
Registered: Feb 2009
Distribution: CentOS 5, Gentoo, FreeBSD, Fedora, Mint, Slackware64
Posts: 205

Rep: Reputation: 46
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 09:49 AM.
 
Old 03-04-2010, 09:57 AM   #3
computerman1983
LQ Newbie
 
Registered: Aug 2009
Distribution: Ubuntu 9.10, Red Hat WS 4, CentOS, OpenBSD, and all windows flavs
Posts: 20

Rep: Reputation: 1
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
 
Old 03-05-2010, 01:28 AM   #4
jabalsad
LQ Newbie
 
Registered: Oct 2007
Posts: 5

Original Poster
Rep: Reputation: 0
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...
 
Old 03-05-2010, 01:33 AM   #5
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.6, Centos 5.10
Posts: 16,324

Rep: Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041
How about ssh-agent?
 
Old 03-05-2010, 08:47 AM   #6
jabalsad
LQ Newbie
 
Registered: Oct 2007
Posts: 5

Original Poster
Rep: Reputation: 0
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?
 
Old 03-05-2010, 08:54 AM   #7
nuwen52
Member
 
Registered: Feb 2009
Distribution: CentOS 5, Gentoo, FreeBSD, Fedora, Mint, Slackware64
Posts: 205

Rep: Reputation: 46
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 09:06 AM.
 
Old 03-08-2010, 09:20 AM   #8
Eric K
LQ Newbie
 
Registered: Mar 2010
Location: Ohio, USA
Distribution: Linux Mint, Ubuntu, CentOS, Red Hat Enterprise Linux, Fedora
Posts: 1

Rep: Reputation: 0
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...
 
Old 03-09-2010, 12:04 AM   #9
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.6, Centos 5.10
Posts: 16,324

Rep: Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041
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.
 
  


Reply

Tags
automation, scp, script, shell, ssh


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
ssh shell script question ihickman Linux - Newbie 4 02-02-2009 02:10 PM
How to ssh from a shell script ? For ppl who can write shell scripts. thefountainhead100 Programming 14 10-22-2008 07:24 AM
Shell Script with ssh & scp ErikJohnson Linux - Networking 1 03-11-2004 05:14 PM
Ssh or logging from shell script Alek Linux - General 5 09-24-2003 11:30 AM


All times are GMT -5. The time now is 10:01 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration