通过python控制命令行
可选方案
- subprocess python原生,实在是难用
- pexpect 第三方,非常好用,但是demo复杂,完成简单功能还是可以的
pexpect
- 安装 pip install pexpect
- 代码来了哈
import pexpect
import re
if __name__ == '__main__':
username=
password=
keyword=
child = pexpect.spawn('git clone https://gitee.com/superfun/rtl8021factory.git')
ret = child.expect('Username') # 获取包含Username的返回,能捕获到
if ret == 0:
print(child.before, child.after)
pattern = re.compile('Username') # 正则
if re.match(pattern, child.after.decode()):
child.sendline('superfun')
ret = child.expect('Password') # 获取包含Username的返回,能捕获到
if ret == 0:
print(child.before, child.after)
pattern = re.compile('Password') # 正则
if re.match(pattern, child.after.decode()):
child.sendline('hahahahahah')
补
后期碰到repo android source 需要无尽输入密码,我也是醉了,加了一个while循环,和timeout,让他自己去玩吧,不过仔细想想,还是哪里不对劲呢。不知道什么时候结束(这个不知道),超时时间太长了,如果错误也不知道(重复执行repo即可),感觉还是不够灵活。
版权声明:本文为ShuoWangLiangXian原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。