0

How can I open in Python a unix shell, type a command and some other inputs and close the unix shell?

Example commands and inputs:

telnet 127.0.0.1:6000
user
pass
save-all
restart

Greets

miny

miny1997
  • 467
  • 3
  • 11
  • [`subprocess.Popen`](http://docs.python.org/2/library/subprocess.html#popen-constructor)? – Sam Mussmann Mar 06 '14 at 14:27
  • 4
    seems you want to use the telnet program and propably write/read to stdin/stdou of telnet. there is no (bash) shell at all. – PeterMmm Mar 06 '14 at 14:28
  • Have you given a look to the [subprocess module](http://docs.python.org/dev/library/subprocess.html)? Also, are you sure you need a shell at all? – Andrea Corbellini Mar 06 '14 at 14:28

1 Answers1

2

You can have a look at the pexpect module and more precisely the interact function.
See documentation here.

Basically, you juste spawn your sheel, program or whatever you want, and interact with it like you would normally do.

import pexepect
p = pexpect.spawn('/bin/bash')
p.interact()

Then you escape with an escape character as explained in the doc.

d6bels
  • 1,337
  • 1
  • 16
  • 27