0

So when I run my Perl script on command line, it will ask for

User: (type name and press enter)

Password: (then it will ask for password and then I press enter) then I can run commands

So if you see, that is a sequence of steps

I know when you use the subproccess commands, I can call a script and have it run. But once Python runs the command, how do you continue to type in the user password after typing in user? When I ran it using "subprocess.call(command, shell=True)" Pycharm ask for "User:" and when I type it in, it freezes.

If possible, I want to configure python to put in my login info into the script and then run a command. I've done something like this in the past but it just ran the script and outputed the data. This perl script requires me to logon before I can output data which is where im having problems.

Brian
  • 66
  • 3
  • related: here's how you could [play a guessing game using `pexpect` or `subprocess`](http://stackoverflow.com/a/23795689/4279) – jfs Jul 02 '15 at 12:17

1 Answers1

3

check out the pexpect module. It does exactly what you want, for example:

child = pexpect.spawn('perl foo.pl')
child.expect('User:')
child.sendline('my_username')
child.expect('Password:')
child.sendline('my_password')
jfs
  • 399,953
  • 195
  • 994
  • 1,670
Corey Goldberg
  • 59,062
  • 28
  • 129
  • 143
  • or `output, status = pexpect.run('perl foo.pl', withexitstatus=1, events={'(?i)user:': 'my_username', '(?i)password:': 'my_password'})` – jfs Jul 02 '15 at 12:14