乱读天书, 不求甚解
周祎骏的个人云笔记
Toggle navigation
乱读天书, 不求甚解
主页
Linux:系统配置
Linux:用户管理
Linux:优化排错
Linux:进程调度
Linux:文件系统
Linux:网络
Linux:系统服务
Linux:安全
Linux:内核
容器:Docker
容器:containerd
容器编排:Kubernetes
IAC:Terraform
大数据:Hadoop
大数据:Zookeeper
大数据:Hbase
消息队列:rsyslog
消息队列:kafka
数据库:MySQL
数据库:MongoDB
搜索引擎:Elasticsearch
时序数据库:OpenTSDB
网站服务:Nginx
编程:Bash
编程:Perl
编程:Python
编程:C
编程:JAVA
编程:Rust
版本控制:gitlab
知识管理:docusaurus
常用小工具
关于我
标签
python 3.13 pexpect 模块
2017-07-16 04:45:45
60
0
0
admin
> python 的自动交互模块 ``` #!/usr/bin/python import pexpect,sys #(command_output,exitcode)=pexpect.run('ls -l /', withexitstatus=1) #获得命令输出和返回值 #print command_output #print exitcode #session=pexpect.spawn('/bin/bash',['-c','ls *']) #如果在命令中有管道,星号等需要转义的,就用这种方式 session=pexpect.spawn('ssh',['localhost'],timeout=5) #命令,参数组成的数组, session.logfile=sys.stdout #记录到标准输出 #session.logfile=file('./log','w') #记录到文件中 count = 0 while True: match=session.expect(['continue connecting \(yes\/no\)','password:','#',pexpect.TIMEOUT]) #列举各种情况 if match == 0: session.send('yes\n') #发送命令 elif match == 1: session.send('XXX\n') elif match == 2: if count == 0: session.sendline('hostname') #发送命令 加上 回车 elif count == 1: session.sendline('uptime') elif count == 2: session.sendline('sleep 10') session.sendcontrol('c') #发送Ctrl + C count = count + 1 elif match == 3: print "TimeOut!" break session.close(force=True) #强制退出 ```
上一篇:
python 3.12 urllib2 模块
下一篇:
python 3.14 socket 模块
文档导航