11

I'm trying to instruct my Python installation to execute an Expect script "myexpect.sh":

#!/usr/bin/expect
spawn ssh usr@myip
expect "password:"
send "mypassword\n";
send "./mycommand1\r"
send "./mycommand2\r"
interact

I'm on Windows so re-writing the lines in the Expect script into Python are not an option. Any suggestions? Is there anything that can run it the way "./myexpect.sh" does from a bash shell?


I have had some success with the subprocess command:

subprocess.call("myexpect.sh",  shell=True)

I receive the error:

myexpect.sh is not a valid Win32 application.

How do I get around this?

8
  • ...are you asking how to run a bash script on Windows without bash? I don't even see where Python comes in here. Furthermore, you probably should give .txt extensions to your shell scripts (.sh if anything) Commented Jun 22, 2012 at 16:43
  • @MateuszKowalczyk in a sense yes: I would like to automate running this script, from a .py file - when I run the .py file, the expect script will be called and run as part of the sequence of the .py file. Commented Jun 22, 2012 at 16:53
  • So it looks like you're not looking for a python-related solution at all (other than the fact that it's being controlled from Python). You simply want a program on Windows that will be able to read the expect script and execute it, right? Commented Jun 22, 2012 at 17:20
  • @EricFinn I am looking for a python solution - the myexpect.sh runs fine if I execute it from a cygwin terminal, but that is not my final goal. What I would like to do is have my python script execute my .sh script whenever I run my .py file, so I don't have to execute it from a terminal by hand before I run my .py file. Commented Jun 22, 2012 at 17:29
  • 1
    @gortron But it looks like your question isn't really Python-specific; you're just telling the OS to run a file as a program, and a solution that will allow the OS to run said file (most likely through a Windows port of Expect) would work with any language, not just Python. Or would also reading in the script and interpreting it yourself in Python be acceptable? Commented Jun 22, 2012 at 17:36

2 Answers 2

26

Use the pexpect library. This is the Python version for Expect functionality.

Example:

child = pexpect.spawn('Some command that requires password')
child.expect('Enter password:')
child.sendline('password')
child.expect(pexpect.EOF, timeout=None)
cmd_show_data = child.before
cmd_output = cmd_show_data.split('\r\n')
for data in cmd_output:
    print data

Pexpect comes with lots of examples to learn from. For use of interact(), check out script.py from examples:

(For Windows, there is an alternative to pexpect.)

Sign up to request clarification or add additional context in comments.

7 Comments

pexpect is a brilliant solution if you're on a Linux platform, but there is no pexpect library available for Windows platforms.
@gortron: There was an SO discussion on pexpect equivalent for windows. I have added that to the answer. See if that helps.
For posterity: winpexpect is a clean solution.
@gortron: Thanks! It has been long since I have been on windows platform.
@gortron: there is no ssh command on Windows. I don't see how using winpexpect would help here.
|
0

Since it is an .expect script, I think you should change the extend name of your script.

Instead of using

subprocess.call("myexpect.sh", shell=True)

you should use

subprocess.call("myexpect.expect", shell=True)

2 Comments

It's fine, bro. Since there is a shebang, it doesn't matter whatever the file extension is.
The system should not care but it is good to be explicit when possible.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.