Background process for ssh port forwarding

Discussion in 'Python' started by Jesse Rosenthal, Nov 15, 2005.

  1. Hello all,

    I'm writing a script which will backup data from my machine to a server
    using rsync. It checks to see if I am on the local network. If I am, it
    runs rsync over ssh to 192.168.2.6 using the pexpect module to log in.
    That's the easy part.

    Now, when I'm not on the local network, I first want to open up an ssh
    connection to do port forwarding, so something like this:

    def hostforward():
    #This is based on the assumption that the passfile is the gnus
    #authinfo file, or has a similar format...
    f = open(PASS_FILE, "r")
    f_list = f.read().split(' ')
    f.close()
    #Now, we get the entry after "password" (be slicker to make it a
    #dictionary, but maybe wouldn't work as well).
    pass_index = f_list.index('password') + 1
    forwardpass = f_list[pass_index]
    #now we connect
    command = 'ssh -l %s -L 2022:%s:22 %s' % \
    (login, my_server, forwarding_server)
    connection = pexpect.spawn(command)
    connection.expect('.*assword:')
    connection.sendline(forwardpass)

    If I end this with 'connection.interact()', I will end up logged in to the
    forwarding server. But what I really want is to go on and run rsync to
    localhost port 2022, which will forward to my_server port 22. So, how can
    I put the ssh connection I set up in hostforward() in the background?
    I need to make sure that connection is made before I can run the rsync
    command.

    I've looked at threading, but that seems excessive. There must be an
    easier way. Whatever I do, though, I'll need to use pexpect to spawn the
    processes, since I'll need to log in to ssh servers with a password.

    Thanks for any help.

    --Jesse
    Jesse Rosenthal, Nov 15, 2005
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Navic
    Replies:
    2
    Views:
    2,584
  2. sime
    Replies:
    0
    Views:
    602
  3. Jesse Rosenthal

    Background process for ssh port forwarding

    Jesse Rosenthal, Oct 2, 2005, in forum: Python
    Replies:
    1
    Views:
    525
    Juho Schultz
    Oct 4, 2005
  4. Andrew Tomazos
    Replies:
    5
    Views:
    574
  5. Jamis Buck

    Net::SSH and port forwarding

    Jamis Buck, Jul 6, 2004, in forum: Ruby
    Replies:
    2
    Views:
    147
    Jamis Buck
    Jul 7, 2004
Loading...

Share This Page