SSH using Paramiko

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • hanjar

    SSH using Paramiko

    Hello,

    I'm using paramiko 0.9 to access one of my home machines and then execute
    a command. But with the script included below, the command gets executed
    on the 2nd to 6th go, never on the first. And the commands aren't that
    commplicated, they are usualy 'touch some-file' or
    'cp some_file to /tmp/a'.

    Am I misunderstandin g the paramiko's API or ... ?

    This is the script:

    import sys
    import socket
    import traceback
    import threading
    import paramiko


    hostname = "192.168.0. 10"
    username = "alex"
    port = 22
    password = 'asdfg6'

    # now connect
    try:
    sock = socket.socket(s ocket.AF_INET, socket.SOCK_STR EAM)
    sock.connect((h ostname, port))
    except Exception, e:
    print 'Connect failed: ' + str(e)
    traceback.print _exc()
    sys.exit(1)

    t = paramiko.Transp ort(sock)
    event = threading.Event ()
    t.start_client( event)
    print "started client"
    event.wait(15)

    if not t.is_active():
    print 'SSH negotiation failed.'
    sys.exit(1)
    else:
    print "SSH negotiation sucessful"

    print "doing authentication"
    t.auth_password (username, password, event)


    event.clear()
    event.wait(20)

    if not t.is_authentica ted():
    print 'Authentication failed. :('
    t.close()
    sys.exit(1)

    if not t.is_active():
    print "Channel is not active"
    else:
    print "Channel is active"
    chan = t.open_session( )
    print "session opened"
    print "Channel name is " + chan.get_name()
    chan.exec_comma nd("cp /etc/ftpusers /tmp/")
    print "executing the command"
    chan.close()
    t.close()
    print "exiting"



    Thanks

    P.S. I'm aware that this is not one of the cleanest scripts out there.
    Or even very security conscious. All I'm looking for is to get it to work,
    then I'll worry about it after.

Working...