title: python telnet not pressing enter with "\n"
body: <p>I am working on a script to pull configs from Cisco devices in GNS3.  Currently it is using a hard coded IP &amp; port due to another separate issue but this is the code I am currently using.</p>
  
  <pre><code>import getpass
  import telnetlib
  
  print "Getting running-config 127.0.0.1:5002"
  tn = telnetlib.Telnet("127.0.0.1","5002")
  
  tn.write("terminal length 0\n")
  tn.write("show run\n")
  tn.write("exit\n")
  
  readoutput = tn.read_until("exit")
  saveoutput = open("C:\Programdata\switch.txt","w")
  saveoutput.write(readoutput)
  saveoutput.write("\n")
  saveoutput.close
  print tn.read_all()
  </code></pre>
  
  <p>If I comment out the <code>readouput</code> and <code>saveoutput</code> blocks of code the script runs through as fast as I would expect it to.  However once I leave that code in there it just sits and hangs.  I have put print() commands in to verify and it hangs on the readoutput line.  I have tried both <code>tn.read_all</code> &amp; <code>tn.read_until</code> (without and without a string parameter) and neither works.</p>
  
  <p>Even though the readoutput is not working I know something is occurring because if I telnet into the switch with <code>telnet 127.0.0.1 5002</code> in a command prompt it comes up without issue as well as being able to progress through my terminal commands.  </p>
  
  <p>Edit:
  I did more testing with this today and found out through the use of a lot of sleep timers and printing to the terminal what was going on.  As it turns out for some reason the telnet session is not actually pressing enter when it should.  I tried changing the <code>tn.write("text\n")</code> to <code>tn.write("text" + "\n")</code>and even splitting that into two write commands, but none of it works.  Below is a telnet session I opened after some failed attempts at making the script work.  Note all the commands are just sitting there in line?  If I change the readoutput to look for "xit" which is the text on the first line of the ios console, then it will find it in telnet and print everything in the telnet session to that point.  </p>
  
  <p>If anyone knows why <code>"\n"</code> is not actually hitting enter I would greatly appreciate the help as this should be my last major hurdle to making the program this script is a part of work.</p>
  
  <p><strong><a href="https://i.stack.imgur.com/TwAW9.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/TwAW9.png" alt="strong text"></a></strong></p>