Register
It is currently Fri Sep 19, 2014 7:51 am

Handling SSH on Bash Scripts


All times are UTC - 6 hours


Post new topic Reply to topic  [ 4 posts ] 
Author Message
 PostPosted: Thu Sep 27, 2007 5:32 am   

Joined: Mon Apr 24, 2006 2:39 pm
Posts: 30
Location: Villahermosa
I want to know how to handle logins in bash since I am not sure if I am not sure if I can send it to background.

So basically I need to sign in to SSH (open a tunnel) and then get a cvs login. I wonder if there is a method on how to handle this multi-proceses.

Code:
ssh -2 -x -L 2401:localhost:2401xyz@server.org

and on a different terminal
Code:
cvs -d :pserver:jza@localhost:/cvs login



NOTE: the cvs login si not within the tunnel but originally done on an outside window.


Top
 Profile WWW ICQ YIM  
 PostPosted: Tue Oct 09, 2007 5:13 pm   
Moderator
User avatar

Joined: Wed May 03, 2006 2:05 pm
Posts: 242
Hi jza!

There are a couple of ways you can do this:

1 - You can fork SSH to the background with the "-f" and "-N" flags

2 - You can start the tunnel in a 'screen' session

This link has some suggestions for doing something similar to what it sounds like you're trying:

http://www.g-loaded.eu/2006/11/24/auto- ... h-tunnels/

so you'd want to do something like this:

Code:
ssh -2 -f -N -x -L 2401:localhost:2401xyz@server.org; cvs -d :pserver:jza@localhost:/cvs login


Of course, this leaves that tunnel open in the background, so you'd probably want to get the pid of that ssh session and kill it at the end of the script.

Hope this helps!


Top
 Profile YIM  
 PostPosted: Tue Oct 30, 2007 1:48 am   

Joined: Mon Apr 24, 2006 2:39 pm
Posts: 30
Location: Villahermosa
jeo wrote:
Hi jza!

2 - You can start the tunnel in a 'screen' session


This would be cool but my question will remain for doing a double authentication, so if I shoot the ssh command to one window and the cvs to another I will still need to switch back and forth to provide the required authentication. right?

BTW your other option did work althought I still need to read about killing the tunnel after.


Top
 Profile WWW ICQ YIM  
 PostPosted: Tue Oct 30, 2007 9:28 pm   
Moderator
User avatar

Joined: Wed May 03, 2006 2:05 pm
Posts: 242
Hmm... there's a couple of ways to do that too :)

Option 1: A Timer

Code:
ssh -2 -f -x -L 2401:localhost:2401xyz@server.org sleep 10; cvs -d :pserver:jza@localhost:/cvs login


(Note that we leave off the '-N' option) The timer will keep the tunnel open for 10 seconds by itself, but the tunnel shouldn't close until after your cvs command finishes. After the cvs command finishes, the tunnel should close automagically. I haven't tested this one yet, but it's based on one of the examples in that link I posted before. Looks like the cleanest option!

Option 2: Capture the pid, kill when finished

Code:
#!/bin/sh
# start the tunnel, use bash to send it to the background instead of "-f"
ssh -2 -N -x -L 2401:localhost:2401xyz@server.org &

# $! gives us the pid of he last process sent to the background
PID=`echo ${!}`

# run your cvs command
cvs -d :pserver:jza@localhost:/cvs login

# Kill the tunnel
kill -9 $PID


The problem with option 2 is it tends to leave the tunnel port occupied until it times out, so it's kind of a dirty way to do it. I think Option 1 is the best way to go, and I learned several new things trying to figure it out! :)

Hope it helps!
-jeo


Top
 Profile YIM  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP