python connect to server using SSH protocol

Discussion in 'Python' started by ajikoe@gmail.com, Feb 8, 2005.

  1. Guest

    How can python connect to server which use SSH protocol?
    Is it easy since my python has to run third party vendor, write data,
    read data inside the server (supercomputer).

    Any suggestion?

    Sincerely Yours,
    Pujo Aji
    , Feb 8, 2005
    #1
    1. Advertising

  2. wrote:

    >How can python connect to server which use SSH protocol?
    >Is it easy since my python has to run third party vendor, write data,
    >read data inside the server (supercomputer).
    >
    >

    In advance, I'm not sure if I understood your problem. SSH is clearly a
    remote
    shell. You will be able to execute other programs on the remote computer.
    Is is suitable to use this:

    child_stdin,child_stdout,child_stderr = os.popen2('ssh -l my_username
    my_host')


    On the other side, you can write an application that also uses
    stdin/stdout for communication.
    Best,

    Laci 2.0

    --
    _________________________________________________________________
    Laszlo Nagy web: http://designasign.biz
    IT Consultant mail:

    Python forever!
    Laszlo Zsolt Nagy, Feb 8, 2005
    #2
    1. Advertising

  3. Guest

    wrote:
    > How can python connect to server which use SSH protocol?
    > Is it easy since my python has to run third party vendor, write data,
    > read data inside the server (supercomputer).
    >
    > Any suggestion?


    you can use popen around the ssh binary.
    You man need the pty module if you want to deal with password prompts
    If you want lower level control, use the twisted module.

    --
    Pádraig Brady - http://www.pixelbeat.org
    --
    , Feb 8, 2005
    #3
  4. Simon Anders Guest

    Hi

    Laszlo Zsolt Nagy wrote:
    > wrote:
    >
    >> How can python connect to server which use SSH protocol?
    >> Is it easy since my python has to run third party vendor, write data,
    >> read data inside the server (supercomputer).
    >>
    >>

    > In advance, I'm not sure if I understood your problem. SSH is clearly a
    > remote
    > shell. You will be able to execute other programs on the remote computer.
    > Is is suitable to use this:
    >
    > child_stdin,child_stdout,child_stderr = os.popen2('ssh -l my_username
    > my_host')


    This is what I was about to reply as well. But I did a short test
    program and encountered a problem:

    import os
    fi, foe = os.popen4 ('ssh dopey')
    print >>fi, 'ls'
    fi.close () # <-- this is annoying
    for line in foe:
    print line,
    foe.close ()

    The above connects to a server, passes the command 'ls', which is
    executed there, and prints the returned result.

    However, reading from foe succeeds only if fin has been closed before.
    An fi.flush() seems to be not sufficient. But if one wants Python to
    interactivly communicate with some shell on a remote machine, it is
    inconvenient to have to close and reopen the connection all the time.

    There should be a better way.

    Simon
    Simon Anders, Feb 8, 2005
    #4

  5. >
    > import os
    > fi, foe = os.popen4 ('ssh dopey')
    > print >>fi, 'ls'
    > fi.close () # <-- this is annoying
    > for line in foe:
    > print line,
    > foe.close ()
    >
    > The above connects to a server, passes the command 'ls', which is
    > executed there, and prints the returned result.
    >
    > However, reading from foe succeeds only if fin has been closed before.
    > An fi.flush() seems to be not sufficient.


    Hmm. Is it possible to use ssh for simple port forwarding (-L option)
    and write a server program on the remote machine?



    --
    _________________________________________________________________
    Laszlo Nagy web: http://designasign.biz
    IT Consultant mail:

    Python forever!
    Laszlo Zsolt Nagy, Feb 8, 2005
    #5
  6. Kartic Guest

    wrote:
    > How can python connect to server which use SSH protocol?
    > Is it easy since my python has to run third party vendor, write data,
    > read data inside the server (supercomputer).
    >
    > Any suggestion?
    >
    > Sincerely Yours,
    > Pujo Aji


    Pujo,

    There are two Python oriented SSH solutions.
    1. pyssh (pyssh.sf.net) which is a library for programmatically
    controlling ssh.
    2. Paramiko (http://www.lag.net/paramiko/) which are python bindings
    for the SSH2 protocol.

    I have used neither of these packages, so I am in no position to
    recommend anything.

    Of course, you have the usual popen() functions that you can use to
    invoke SSH.

    Thanks,
    -Kartic
    Kartic, Feb 8, 2005
    #6
  7. Simon Anders wrote:
    > Hi
    >
    > Laszlo Zsolt Nagy wrote:
    >
    >> wrote:
    >>
    >>> How can python connect to server which use SSH protocol?


    <BIG SNIP>

    >
    > There should be a better way.
    >
    > Simon


    There is : pexpect! http://pexpect.sourceforge.net/
    Martin Franklin, Feb 9, 2005
    #7
  8. On Tuesday 08 February 2005 13:26, Simon Anders wrote:

    > This is what I was about to reply as well. But I did a short test
    > program and encountered a problem:
    >
    > import os
    > fi, foe = os.popen4 ('ssh dopey')
    > print >>fi, 'ls'
    > fi.close () # <-- this is annoying
    > for line in foe:
    > print line,
    > foe.close ()
    >
    > The above connects to a server, passes the command 'ls', which is
    > executed there, and prints the returned result.
    >
    > However, reading from foe succeeds only if fin has been closed before.
    > An fi.flush() seems to be not sufficient.


    But this version below does work. Im not sure whats happening when using the
    file as an iterator to make a difference.

    import os, sys
    fi, foe = os.popen4 ('ssh xxxxx')
    print >>fi, 'ls'
    fi.flush () # <-- this is annoying
    while 1:
    b = foe.readline()
    sys.stdout.write(b)


    > But if one wants Python to
    > interactivly communicate with some shell on a remote machine, it is
    > inconvenient to have to close and reopen the connection all the time.


    But this route carries a big deadlock risk. the rsync program can tunnel over
    ssh this way, but have first hand experience of
    http://www.google.com/search?q=rsync ssh deadlock

    In the script above, if 'ls' is replaced with a longer input then there is
    every chance that the fi stream will block before all of it is written,
    because this script hasnt started draining foe.

    For a real python program using ssh (but not 'interactive'... data written to
    fi does not depend on data read from foe) see
    http://dirstorage.sourceforge.net/replica.html
    --
    Toby Dickenson
    Toby Dickenson, Feb 9, 2005
    #8
    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. yaron
    Replies:
    2
    Views:
    765
    Acid_X
    Nov 3, 2003
  2. loial
    Replies:
    0
    Views:
    387
    loial
    Feb 3, 2009
  3. loial
    Replies:
    0
    Views:
    391
    loial
    Feb 10, 2009
  4. Deepika
    Replies:
    2
    Views:
    199
    Deepika
    Nov 14, 2006
  5. Replies:
    2
    Views:
    272
Loading...

Share This Page