title: Prevent Paramiko from Exiting
body: <h1>Description</h1>
  <p>I'm trying to write a python script to SSH into my VM and execute some commands.</p>
  <ul>
  <li><p>If I do it manually, my session stay open, and I can still see my <strong>logs live</strong>.</p>
  </li>
  <li><p>If I use the script, after SSH in and automate a few commands, it kept exiting out and return to my user prompt. I'm trying to prevent that.</p>
  </li>
  </ul>
  <hr />
  <h1>Video</h1>
  <p>Manually =
  <a href="https://dl.dropboxusercontent.com/u/56134944/ssh_wag_manually.mov" rel="nofollow noreferrer">https://dl.dropboxusercontent.com/u/56134944/ssh_wag_manually.mov</a></p>
  <p>Via Python Script =
  <a href="https://dl.dropboxusercontent.com/u/56134944/ssh_wag_py.mov" rel="nofollow noreferrer">https://dl.dropboxusercontent.com/u/56134944/ssh_wag_py.mov</a></p>
  <hr />
  <p>This is what I have right now</p>
  <pre><code>import paramiko
  import time
  import sys
  
  # Note
  # sudo pip install --user paramiko
  
  
  def ssh_con (ip, un, pw):
      global client
      client = paramiko.SSHClient()
      client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
      print (&quot;Connecting to device/VM: %s&quot; % ip)
      client.connect(ip, username=un, password=pw)
  
  
  def cmd_io (command):
      global client_cmd
      client_cmd.send(&quot;%s \n&quot; %command)
      time.sleep(1)
      output = client_cmd.recv(10000).decode(&quot;utf-8&quot;)
      print (output)
  
  # ip = raw_input(&quot;Enter WAG IP : &quot;)
  # ip  = sys.argv[1]
  
  ip = '172.168.200.300'
  un = 'xyz'
  pw = 'abc'
  
  ssh_con(ip,un,pw)
  client_cmd = client.invoke_shell()
  
  print (&quot;SSH CONNECTION ESTABLISHED TO vMEG %s&quot; % ip)
  cmd_io (&quot;en&quot;)
  cmd_io (&quot;terminal monitor&quot;)
  cmd_io (&quot;debug wag https&quot;)
  cmd_io (&quot;debug wag httpc&quot;)
  cmd_io (&quot;debug https&quot;)
  cmd_io (&quot;debug httpc&quot;)
  cmd_io (&quot;debug wag kafka&quot;)
  </code></pre>
  <hr />
  <p>Result</p>
  <pre><code>[local]site#
  terminal monitor 
  [local]site#
  debug wag https 
  [local]site#
  debug wag httpc 
  [local]site#
  debug https 
  [local]site#
  debug httpc 
  [local]site#
  debug wag kafka 
  [local]site#
  
  .... Quitting and return the command prompt ....
  
  ──[/Applications/MAMP/htdocs/code/python] 
  └── 
  </code></pre>
  <hr />
  <p>How would one go about preventing this?</p>