[paramiko] Problem using channels

Heine Andersen heine.andersen at gmail.com
Fri Dec 7 15:34:12 PST 2007


Hi,

I'm trying to create a program that log in to a chain of hosts, and
setup the users keys,
to do this job I have created a recursive program, but the problem is
that i only work for the first host i log in to.

this is just a snip to show my problem,

#!/usr/bin/env python

import paramiko, socket, time, sys


def exec_cmd(username, password, hostname, cmd):

    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.connect((hostname, 22))

    transport = paramiko.Transport(sock)
    transport.connect(username=username, password=password)

    channel = transport.open_session()
    channel.setblocking(0)
    channel.set_combine_stderr(True)
    channel.exec_command(cmd)
    channel.send(password)

    rc = channel.recv_exit_status()

    cmdout = ""
    while channel.recv_ready() and rc != -1:
        cmdout += channel.recv(1024)

    transport.close()

    return cmdout


if __name__ == "__main__":

    try:
        count = sys.argv[1]
        print count

        password = sys.stdin.read()
        password = password.strip(" \n")

        paramiko.util.log_to_file("para.log" + count, level=10)

        count = int(count) - 1
        if count > 0:
            print exec_cmd("user", password, "localhost",
"./paratest.py " + str(count))

    except:
        print "Unexpected error:", sys.exc_info()[0]


I run it like this :

user at comp:~$ cat pw | ./paratest.py 2
2


If i hardcode the password instead of parssing it ( using stdin ) it's
working great, anybody can tell me what I'm missing here ?

regards



More information about the paramiko mailing list