pexpect
import pexpect ip="127.0.0.1" name="root" pwd="root" #发送命令执行交互 child=pexpect.spawn('ssh %s@%s' % ("root",ip) ) child.expect ('password:') child.sendline(pwd) child.expect('$') child.sendline('df -h') #发送命令 child.sendline("exit") child.interact() #关闭pexpect child.close()