|
发表于 2020-12-28 17:33:49
From FishC Mobile
|
显示全部楼层
本帖最后由 hrp 于 2020-12-29 23:03 编辑
试试这个,手机写的,测试不了
- import re
- from subprocess import PIPE, Popen
- fake_file = Popen(['ftp', '10.40.92.3'], stdin=PIPE, stdout=PIPE, shell=True, universal_newlines=True)
- while fake_file.poll() is None:
- info = fake_file.stdout.readline()
- if re.search(r'用户.*', info):
- # 用户名自行更改
- fake_file.stdin.write('abc\n')
- elif re.search(r'密码.*', info):
- # 密码自行更改
- fake_file.stdin.write('xxx\n')
- elif re.search(r'ftp>.*', info):
- fake_file.stdin.write('get 111.txt\n')
- # 超时限制60秒
- fake_file.communicate(timeout=60)
- print('退出状态码:', fake_file.returncode)
复制代码
如果不是非用subprocess不可的话,建议用pexpect库,这个是为命令行自动交互而生的。
|
|