scp module for python ?

eichin at metacarta.com eichin at metacarta.com
Tue Feb 11 15:06:15 EST 2003


here's a snippet of the code I use in my local customizations of
qmtest; this is using the debian python-expect package, not sure where
it came from before that.  This runs ssh, not scp, but the concept is
the same.  You'll probably want to delete even more of the code, the
key concepts are "use expect" and "wait for the password prompt before
sending it".  (The rest is mostly arranging that the user's privileges
don't get used by accident.)

import expect

        # start the command
        if os.environ.has_key("SSH_AUTH_SOCK"):
            del os.environ["SSH_AUTH_SOCK"]
        env_fix = " 'PATH=/sbin:$PATH;' "
        ssh_cmd = "ssh -F ssh_safe_config " + app_ut_user + "@" + app_ut_host + " " + env_fix + app_cmd
        # result[Result.CAUSE]="ok: " + ssh_cmd
        f = expect.popen2(ssh_cmd + " < /dev/null", "p")
        # give the password
        toss = f.read("password: ")
        f.write(app_ut_pass + "\n")
        # toss = f.read("\n") # this was problematic
        # get all the input
        stdout = f.read()
        # result[Result.CAUSE]="ok: stdout=" + stdout
        f.close()




More information about the Python-list mailing list