How to redirect data to a “getpass” like password input?

眉间皱痕 提交于 2019-12-04 06:09:24

You need the pexpect module for such cases.

Pexpect is a Python module for spawning child applications and controlling them automatically. Pexpect can be used for automating interactive applications such as ssh, ftp, passwd, telnet, etc.

There's definitely no need for two classes to make something like this. All you'd need to do is create another method in put_data.py called init_() and then do something along the lines of:

x = raw_input('text1:')
y = getpass.getpass('pass1:')
z = getpass.getpass('pass2:')

Then you can just use pexpect to do the rest:

child = pexpect.spawn(x, timeout=180)
while True:
   x = child.expect(["(current)", "new", "changed", pexpect.EOF, pexpect.TIMEOUT])
   if x is 0:
      child.sendline(y)
      time.sleep(1)
   if x is 1:
      child.sendline(z)
      time.sleep(1)
   if x is 2:
      print "success!"
      break

tada! Granted you'll probably get a ton of errors with code like that. You should always utilize the provided methods, if you're using linux it may just be easier to run os.system("passwd") and let the shell take care of the rest. Also, if at all possible always avoid using getpass, it's a funky obsolete method and can mess things up down the road.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!