利用sshpass和ssh編寫腳本遠程執行命令

import subprocess

import os


(rfd, wfd) = os.pipe()

arg = "-d%d" % rfd

try:

    p = subprocess.Popen(["sshpass",arg,"ssh","-l","haohzhang","phxaishdc9dn1447.stratus.phx.ebay.com","ls","/tmp/"], stdin=subprocess.PIPE,stdout=subprocess.PIPE, stderr=subprocess.PIPE)

    os.close(rfd)

    os.write(wfd,"PASSWORD\n")

    os.close(wfd)

    stdout, stderr = p.communicate()

    print stdout, stderr

except OSError:

    print("to use the 'ssh' connection type with passwords, you must install the sshpass program")


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章