ITPub博客

首页 > 自动化运维 > 应用服务器 > pexpect 学习

pexpect 学习

原创 应用服务器 作者:longer3281 时间:2019-03-26 17:21:30 1 删除 编辑

#!/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博客 ” ,链接:https://blog.itpub.net/9606353/viewspace-2639409/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论
数据库架构师,DB从业经历 DB2,Oracle,MySQL,PGSQL,MonogDB 等数据库,hadoop,hive,spark,flink,doris等大数据技术。

注册时间:2009-05-22

  • 博文量
    69
  • 访问量
    185392