Advertisement
Guest User

For Ryan

a guest
Jan 23rd, 2017
566
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.91 KB | None | 0 0
  1. #!/usr/bin/python
  2.  
  3. import pexpect
  4.  
  5. #Define the IP Address for the Switch SSH Connection
  6. while True:
  7.      #Inialize our variables to a known state (this prevents stupid errors)
  8.      SwitchIP = ""
  9.      CheckIP = ""
  10.      MoveOn = False
  11.  
  12.      #Get the whole IP address in one input, simplifies things a LOT
  13.      SwitchIP = raw_input('\n\nEnter the IP address of your switch (eg. 192.168.100.254): ')
  14.      #Split the IP on '.' then use a loop to check each octet
  15.      CheckIP = SwitchIP.split(".")
  16.      for Octet in CheckIP:
  17.           if (int(Octet) > 0) and (int(Octet) < 256):
  18.                #if it's within the range we want, exit this for loop
  19.                MoveOn = True
  20.                break
  21.           #If you hit this, it means we weren't in the correct range
  22.           print('Incorrect Octet ' + str(Octet) + ' Supplied, Please Try Again')
  23.      if MoveOn == True:
  24.           break
  25.  
  26.  
  27. #Define the Username for the Switch SSH Connection
  28. SwitchUser = raw_input("\n\nEnter the Switch Username: ")
  29.  
  30. #Print Relevant Details to user
  31. print('\n\n\n\n\n\n\n***************************')
  32. print('Your IP Address is - ' + SwitchIP)
  33. print('Your SSH Username is - ' + SwitchUser)
  34. print('***************************')
  35.  
  36.  
  37. #SSH Connection Establishing
  38. print('\n\n\n\n\n\n\n\n\n\n\n\n\n***************************')
  39.  
  40.  
  41. print('SSH Connection Establishing')
  42. ProcSSH = pexpect.spawn('ssh %s@%s' % (SwitchUser, SwitchIP))
  43. print ('SSH Connection into %s@%s' % (SwitchUser, SwitchIP))
  44. print('***************************')
  45.  
  46. #Inialize a variable to a known state
  47. BadPass = False
  48.  
  49. while True:
  50.  
  51.      #If this is a repeat, skip accepting a new switch
  52.      if BadPass == False:
  53.           #If we haven't conencted to this switch before, we must accept it's host key    
  54.           ProcStatus = ProcSSH.expect(['Are you sure you want to continue connecting (yes/no)?',pexpect.TIMEOUT], timeout=3)
  55.           if ProcStatus != 1:
  56.                SwitchNew = raw_input ('You have not connected to this switch before, Proceed (yes/no)?')
  57.                ProcSSH.sendline(SwitchNew)
  58.      
  59.      #Now we can look for the password
  60.      ProcStatus = ProcSSH.expect(['password:', pexpect.TIMEOUT], timeout=5)
  61.      if ProcStatus != 1:
  62.           SwitchPass = raw_input('What is the Login Password: ')
  63.           #Commands to grant entry level access
  64.           ProcSSH.sendline(SwitchPass)
  65.           #I used '$' because I was testing on a ubuntu machine
  66.           #if ProcSSH.expect(['\>', pexpect.TIMEOUT], timeout=3) != 1:
  67.           if ProcSSH.expect(['\$', pexpect.TIMEOUT], timeout=3) != 1:
  68.                print ("1 - Entry to Switch was Granted")
  69.                break
  70.           elif ProcSSH.expect(['Permission denied', pexpect.TIMEOUT], timeout=3) != 1:
  71.                print ("Password Error or some message...")
  72.                BadPass = True
  73.           else:
  74.                print ("1 - Entry to Switch was Denied, Try Again")
  75.                BadPass = True
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement