title: Python Telnet script
body: <p>Thanks to <a href="https://docs.python.org/2/library/telnetlib.html" rel="nofollow noreferrer" title="Library">Python Library</a> i was able to use their example to telnet to Cisco switches, I am using this for learning purposes, specifically learning python. </p>
  
  <p>However, although all the code seem generally easy to read, I am a bit confused as to the following:</p>
  
  <p>1- why use the if statement below
  2- why use the "\n" after the username and password write method
  3- why am i not getting the output on my bash terminal when the changes are infact committed and successful </p>
  
  <pre><code>HOST = "172.16.1.76"
  user = raw_input("Enter your Telnet username : ")
  password = getpass.getpass()
  
  tn = telnetlib.Telnet(HOST) 
  
  tn.read_until("Username: ") 
  tn.write(user + '\n')                &lt;----- 2
  if password:                         &lt;----- 1
      tn.read_until("Password: ")
      tn.write(password + "\n")        &lt;------2
  tn.write("show run \n") 
  
  time.sleep(5)
  
  
  
  output = tn.read_all()              &lt;----- 3
  print output  
  
  print "=" * 30
  print "Configuration Complete."
  </code></pre>
  
  <p>I am not sure as to why using the if statement above, typically once you input in the Username, you get the password prompt right afterward. why cant we just type :</p>
  
  <pre><code>tn.read_until("Username: ") 
  tn.write(user + '\n')
  tn.read_until("Password: ")
  tn.write(password + "\n")
  </code></pre>
  
  <p>As for the second point, why use the '\n' after the passwords and username in the write method if we going to hit enter after we add them anyway?</p>