How to create new console sessions in Python and work with them
Question: I'm trying to figure out how to work with consoles in Python. Let's say, I
have a Python2 script. And this script should create 3 consoles (bash or any
other) and provide different commands to them.

Example:

  * Console #1 will be responsible for telnetting
  * Console #2 for pinging
  * Console #3 will be responsible for new ssh access

What I've found so far is **subprocess** module.

I can use this

    
    
    import subprocess
    term1 = subprocess.Popen(['open', '-a', 'Terminal'])
    

But I still cannot find how to send a command to **term1**

Thank you.


Answer: If you want to have the ability to communicate with your subprocess its best
to use `subprocess.popen`.

<https://docs.python.org/2/library/subprocess.html#subprocess.Popen>

This way you can create a stdin and stout to communicate with the process. As
shown in the link above you simply add them to the popen argument:

    
    
    subprocess.Popen(args, stdin=PIPE, stdout=PIPE)
    

There is also `popen.communicate(input= 'your input')`.

That will wait for the command to finish.

