pexpect 学习

#!/usr/bin/python

#coding:utf-8


import pexpect

import time


ip="192.168.122.10"

name="root"

pwd="XXXXX"

port="22"


print '------ssh -p %s %s@%s' %(port,"root",ip)

child=pexpect.spawn('ssh -p %s %s@%s' %(port,"root",ip)  )


ii=child.expect ('password:')

ii=child.sendline(pwd)


ii=child.expect('#')


ii=child.sendline('mkdir -p /root/demo/ && touch /root/demo/ptest.txt')

ii=child.sendline('test -e /root/demo/ptest.txt  && rm -f /root/demo/ptest.txt')

child.sendline("exit")  #必须指向这一行,才能指向上一行


child.close()


来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/9606353/viewspace-2639409/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/9606353/viewspace-2639409/

你可能感兴趣的:(python)