title: Verifying Authentication to a Telnet Session in Python
body: <p>I am trying to telnet into a Cisco Switch and run a couple of commands on it.  I am able to check if the host doesn't exist, not sure how to check if the username or password is correct.  This is what I got so far(This is part of my class)</p>
  
  <pre><code>def login(self):
      if self.user_name and self.password: 
          try:
              self.connection=telnetlib.Telnet(self.telnet_host)
              try:
                  self.connection.read_until('sername:',1)
                  self.connection.write(self.user_name+'\r\n')
                  self.connection.read_until('assword:',1)
                  self.connection.write(self.password+'\r\n')
                  self.connection.read_until(self.prompt,1)
                  print "Connected"
                  self.loggedON=True
              except EOFError:
                  print "Authentication to "+ self.telnet_host+" failed.\n"
                  return
          except: 
              print "Can't connect to "+self.telnet_host+"\n"
              return
      else:
          if not self.user_name:
              self.user_name=raw_input("Username: ")
              self.login()
          else:
              self.password=raw_input("Password: ")
              self.login()
  </code></pre>
  
  <p>It will still say it is connected even if the wrong password or username.  </p>