python3 pexpect 自动交互修改linux系统密码

#!/usr/bin/python3

import pexpect

new_pw = '12345678'
try:
    process = pexpect.spawn('passwd',timeout=5)
    process.expect('[Nn]ew')
    process.sendline(new_pw)
    process.expect('[Rn]etype')
    process.sendline(new_pw)
    process.expect('[Ss]uccess.*')
    print('ACK')
except:
    print('NAK')

 

posted @ 2021-04-27 19:37  bert_qin  阅读(213)  评论(0编辑  收藏  举报