Paramiko, termios - interactive shell connection.

Pawel Gega pgega at secpay.com
Sun Oct 19 19:29:07 CEST 2008


Hi,

I am coding a small SSH client, I ve got some issues with creating 
pseudo terminal on server side, or at least I suppose that's the problem.
That is the 'ps auxf' run on SSH server:

    root      4317  0.0  0.3  33744   876 ?        Ss   11:36   0:00
    /usr/sbin/sshd
    *### **4525** is the proper connection done with standard ssh
    client   *|
    root      4525  0.3  1.1  53036  2896 ?        Ss   11:37   0:00  \_
    sshd: root at pts/0**
    root      4532  0.0  0.8  15700  2060 pts/0    Ss   11:37   0:00 
    |   \_ -bash
    root      4987  0.0  0.4  12596  1024 pts/0    R+   11:38   0:00 
    |       \_ ps auxf  
    *### ..and 4733 is the connection made with my python script , as
    you can see it is not using PTS *
    root      4733  0.0  1.0  52940  2676 ?        Ss   11:37   0:00  \_
    sshd: pgega [priv]
    pgega     4741  0.0  0.6  52940  1600 ?        S    11:37   0:00 
    |   \_ sshd: pgega
    root      4745  0.0  0.9  33768  2252 ?        Ss   11:37   0:00  \_
    sshd: [accepted]

Do you see anything worng with my script ? Here is the source code:

Regards,
Pawel Gega

    ##########################################################

    #! /usr/bin/env python

    import paramiko
    import termios
    import sys
    import tty

    hostname = 'h1m'
    port = 22
    username = 'pgega'
    password = 'xxx'
    known_hosts = '/home/pgega/.ssh/known_hosts'

    def shell(chan):
        import select

        oldtty = termios.tcgetattr(sys.stdin)
        try:
            tty.setraw(sys.stdin.fileno())
            tty.setcbreak(sys.stdin.fileno())
            chan.settimeout(0.0)

            while True:
                r, w, e = select.select([chan, sys.stdin], [], [])
                if chan in r:
                    try:
                        x = chan.recv(1024)
                        if len(x) == 0:
                            print '\r\n*** EOF\r\n',
                            break
                        sys.stdout.write(x)
                        sys.stdout.flush()
                    except socket.timeout:
                        pass
                if sys.stdin in r:
                    x = sys.stdin.read(1)
                    if len(x) == 0:
                        break
                    chan.send(x)

        finally:
            termios.tcsetattr(sys.stdin, termios.TCSADRAIN, oldtty)

    if __name__ == '__main__':
        paramiko.util.log_to_file('psshc-interactive_shell.log')
        trn = paramiko.Transport((hostname,port))
        trn.connect(username=username, password=password)
        chn = trn.open_channel(kind='direct-tcpip',
    dest_addr=('h1m',22), src_addr=('hm',22))
        shell(chn)
        chn. close()

    ##########################################################






More information about the Python-list mailing list