title: Paramiko - SSH to remote host
body: <p>I have a server:</p>
  
  <pre><code>import threading
  import paramiko
  import subprocess
  import sys
  import socket
  
  host_key = paramiko.RSAKey(filename='test_rsa.key')
  
  class Server(paramiko.ServerInterface):
      def _init_(self):
          self.event = threading.Event()
      def check_channel_request(self, kind, chanid):
          if kind == 'session':
              return paramiko.OPEN_SUCCEEDED
          return paramiko.OPEN_FAILED_ADMINISTRATIVELY_PROHIBITED
      def check_auth_password(self, username, password):
          if(username=='justin') and (password == 'lovesthepython'):
              return paramiko.AUTH_SUCCESSFUL
          return paramiko.AUTH_FAILED
  
  server = sys.argv[1]
  ssh_port = int(sys.argv[2])
  try:
      sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
      sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
      sock.bind((server, ssh_port))
      sock.listen(100)
      print '[+] Listening for connection ...'
      client, addr = sock.accept()
  except Exception, e:
      print '[-] Listen failed: ' + str(e)
      sys.exit(1)
  print '[+] Got a connection!'
  
  try:
      bhSession = paramiko.Transport(client)
      bhSession.add_server_key(host_key)
      server = Server()
      try: 
          bhSession.start_server(server=server)
      except paramiko.SSHException, x:
          print '[-] SSH Negotiation Failed'
      chan = bhSession.accept(20)
      print '[+] Authenticated!'
      print chan.recv(1024)
      chan.send('Welcome to bh_ssh')
      while True:
          try:
              command= raw_input("Enter command: ").strip('\n')
              if command != 'exit':
                  chan.send(command)
                  print chan.recv(1024) + '\n'
              else: 
                  chan.send('exit')
                  print 'exiting'
                  bhSession.close()
                  raise Exception ('exit')
          except KeyboardInterrupt:
              bhSession.close()
  except Exception, e:
      print '[-] Caught exception: ' + str(e)
      try:
          bhSession.close()
      except:
          pass
      sys.exit(1)
  </code></pre>
  
  <p>My code to connect to this is:</p>
  
  <pre><code>import threading
  import paramiko
  import subprocess
  
  def ssh_command(ip, port, user, passwd, command):
      client = paramiko.SSHClient()
      #client.load_host_keys('/home/justin/.ssh/known_hosts')
      client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
      client.connect(ip, port, username=user, password=passwd)
      ssh_session = client.get_transport().open_session()
      if ssh_session.active:
          ssh_session.send(command)
          print ssh_session.recv(1024)
          while True:
              command = ssh_session.recv(1024)
              try:
                  cmd_output = subprocess.check_output(command, shell=True)
                  ssh_session.send(cmd_output)
              except Exception,e:
                  ssh_session.send(str(e))
          client.close()
      return
  ssh_command('IP_ADDRESS_HERE',PORT_HERE,'justin','lovesthepython','id')
  </code></pre>
  
  <p>When I try to use these on separate PCs and use public IP addresses it won't connect. The server I bind to 0.0.0.0 and then use the public IP address of the server's computer to the client code. I imagine I am doing something fairly obvious wrong. If anyone can help, it would be very much appreciated. </p>