title: Exit out of a console connection in Python
body: <p>I am working on some python code to log into a whitebox device that houses a virtual application (VTA). There will be two different VTAs installed and the code will log into the physical device, then log into the VTA using a virsh console (Name of VTA).</p>
  <p>The issue I am running into is exiting one VTA and then virsh console into another. The exit command simply brings me to a login prompt again but will not exit out of the console connection.</p>
  <p>In order to do so, I must send a &quot;control + ]&quot; in order to break out of the console. I have been searching online to try and find a solution but the only option I have found is to send and &quot;exit&quot; followed by &quot;\x1b&quot;. However, This does not actually break out of the console window. Rather, it ends the session which is not what I am looking for.</p>
  <p>Is there a way to send a &quot;Control + ]&quot; in python?</p>
  <p>Here is some of the code showing the steps:</p>
  <pre><code>from paramiko import SSHClient, AutoAddPolicy
  import time
  import re
  import os
  import sys
  import progressbar
  import stat
  
  def create_connection(host):
      username = ''
      password = ''
      port = 22
      connection_info = {
          'port': port,
          'username': username,
          'password': password
      }
  
      client = SSHClient()
      client.set_missing_host_key_policy(AutoAddPolicy())
      client.connect(host, timeout=10, auth_timeout=10, **connection_info)
      ssh_session = client.invoke_shell()
      ssh_session.settimeout(10.0)
  
      return ssh_session
  
  
  def send_commands(ssh_session, commands, sleep_time=1):
      for command in commands:
          ssh_session.send(command)
          time.sleep(sleep_time)
  
      output = ssh_session.recv(1048576)
      decoded_output = output.decode()
      return decoded_output
  
  
  console_commands = [
          'virsh console vw-vta\n',
          '\n',
          '\n',  # Place the username of the VTA here
          '\n'  # Place the password of the VTA here
      ]
  show_mgmt_commands = [
          'ifconfig ens2 | grep Bcast\n'
      ]
  
  exit_console = [
          'exit\n'
          '\x1b'
      ]
  validate_commands = [
          'virsh list\n'
      ]
  
  
  def validation():
      host = input('What is the IP address? ')
      print('\n')
      print(f'\033[1;33m--&lt; Checking {host} for a valid VTA &gt;------------\033[0m')
  
      try:
          ssh_session = create_connection(host)
      except Exception as l:
          print(f&quot;\033[1;31mCannot connect to {host}!\033[0m&quot;)
          return
  
      validate = send_commands(ssh_session, validate_commands)
  
      if 'y1564' in validate:
          print(f&quot;\033[1;32mThere is a Y1564 VTA running! Obtaining information...\033[0m&quot;)
          ssh_session = create_connection(host)
          console = send_commands(ssh_session, console_commands)
  
          try:
              show_mgmt = send_commands(ssh_session, show_mgmt_commands, sleep_time=2)
  
          except Exception as e:
              print(f&quot;\033[1;31mCouldn't reach the console on &quot; f&quot;\033[1;33m{host}\033[0m&quot;f&quot;\033[1;31m. This VTA will need to be rebuilt.\033[0m&quot;)
  
  
  
          if 'Login incorrect' in show_mgmt:
              print(f&quot;\033[1;31m--&lt; Begin ERROR MESSAGE &gt;------------\033[0m&quot;)
              print(show_mgmt)
              print(f&quot;\033[1;31m--&lt; End ERROR MESSAGE &gt;------------\033[0m&quot;)
              print(f&quot;\033[1;31mThis VTA has the incorrect password!\033[0m&quot;)
              print(f'{host},VTA Password Error', file=f)
              exit = send_commands(ssh_session, exit_console)
              return
          else:
              try:
                  mgmt = show_mgmt.split('addr:')[1].split(&quot; &quot;)[0]
              except Exception as g:
                  print(f&quot;\033[1;31mThis VTA is corrupt and will need to be rebuilt!\033[0m&quot;)
                  exit = send_commands(ssh_session, exit_console)
                  return
              print(&quot;Y1564 VTA IP: &quot;, mgmt)
              exit = send_commands(ssh_session, exit_console)
  
      else:
          print(&quot;\033[1;33mThere is NOT a Y1564 VTA running on \033[0m&quot;f&quot;\033[1;34m {host}\033[0m&quot;)
      
      ssh_session.close()
  
  if __name__ == '__main__':
      full_check()
  </code></pre>
  <p>When this function finishes, the second part of the code calls a similar function. However it fails because the previous function did not break out of the console connection. The code attempts to send the commands for the next function while it is still inside the previous VTA.</p>
  <p>Here is an output showing what it is doing:</p>
  <pre><code>What is the IP address? 1.1.1.1
  
  
  --&lt; Checking 1.1.1.1 for a valid VTA &gt;------------
  There is a Y1564 VTA running! Obtaining information...
  Y1564 VTA IP:  10.10.10.10
  exit
  logout
  
  
  --&lt; Checking 1.1.1.1 for a valid VTA &gt;------------
  Ubuntu 16.04.6 LTS y1564 ttyS0
  
  y1564 login:
  
  virsh list
  Password:
  There is NOT a VTA running on 1.1.1.1
  
  </code></pre>
  <p>The output above shows that when the exit command is run and followed by the \x1b, it does not properly exit out but attempts to send the &quot;virsh list&quot; command from the next part.</p>