Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- import pexpect
- #Define the IP Address for the Switch SSH Connection
- while True:
- #Inialize our variables to a known state (this prevents stupid errors)
- SwitchIP = ""
- CheckIP = ""
- MoveOn = False
- #Get the whole IP address in one input, simplifies things a LOT
- SwitchIP = raw_input('\n\nEnter the IP address of your switch (eg. 192.168.100.254): ')
- #Split the IP on '.' then use a loop to check each octet
- CheckIP = SwitchIP.split(".")
- for Octet in CheckIP:
- if (int(Octet) > 0) and (int(Octet) < 256):
- #if it's within the range we want, exit this for loop
- MoveOn = True
- break
- #If you hit this, it means we weren't in the correct range
- print('Incorrect Octet ' + str(Octet) + ' Supplied, Please Try Again')
- if MoveOn == True:
- break
- #Define the Username for the Switch SSH Connection
- SwitchUser = raw_input("\n\nEnter the Switch Username: ")
- #Print Relevant Details to user
- print('\n\n\n\n\n\n\n***************************')
- print('Your IP Address is - ' + SwitchIP)
- print('Your SSH Username is - ' + SwitchUser)
- print('***************************')
- #SSH Connection Establishing
- print('\n\n\n\n\n\n\n\n\n\n\n\n\n***************************')
- print('SSH Connection Establishing')
- ProcSSH = pexpect.spawn('ssh %s@%s' % (SwitchUser, SwitchIP))
- print ('SSH Connection into %s@%s' % (SwitchUser, SwitchIP))
- print('***************************')
- #Inialize a variable to a known state
- BadPass = False
- while True:
- #If this is a repeat, skip accepting a new switch
- if BadPass == False:
- #If we haven't conencted to this switch before, we must accept it's host key
- ProcStatus = ProcSSH.expect(['Are you sure you want to continue connecting (yes/no)?',pexpect.TIMEOUT], timeout=3)
- if ProcStatus != 1:
- SwitchNew = raw_input ('You have not connected to this switch before, Proceed (yes/no)?')
- ProcSSH.sendline(SwitchNew)
- #Now we can look for the password
- ProcStatus = ProcSSH.expect(['password:', pexpect.TIMEOUT], timeout=5)
- if ProcStatus != 1:
- SwitchPass = raw_input('What is the Login Password: ')
- #Commands to grant entry level access
- ProcSSH.sendline(SwitchPass)
- #I used '$' because I was testing on a ubuntu machine
- #if ProcSSH.expect(['\>', pexpect.TIMEOUT], timeout=3) != 1:
- if ProcSSH.expect(['\$', pexpect.TIMEOUT], timeout=3) != 1:
- print ("1 - Entry to Switch was Granted")
- break
- elif ProcSSH.expect(['Permission denied', pexpect.TIMEOUT], timeout=3) != 1:
- print ("Password Error or some message...")
- BadPass = True
- else:
- print ("1 - Entry to Switch was Denied, Try Again")
- BadPass = True
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement