View Single Post
  #3  
Old 03-20-2018, 12:27 PM
conip conip is offline
Registered User
 
Join Date: Mar 2018
Posts: 7
no worries

I didn't want to paste too much as I thought it was irrelevant

Code:
import re
import hashlib
import os.path
#=========================================================
class DEVICE:
	def __init__(self,DEV_IP=None,PASS=None,USER=None,FILE=None,JUMPBOX_NAME=None,CURRENT_TAB=None,COMMAND=None):
		self.DEV_IP = DEV_IP
		self.PASS = PASS
		self.USER = USER
		self.JUMPBOX_NAME = JUMPBOX_NAME
		self.FILE = FILE
		self.objTAB = CURRENT_TAB
		self.command = COMMAND
	#-----------------------	
	def f_connect_ssh(self):
		errcode = 0
		result = ""
		data = ""
		errorMessages = ""
		#try:
		self.FILE.write("\nTrying SSH to - %s -> RESULT =  " % (str.strip(self.DEV_IP)))
			#1st "true/false" arg - true - wait for the connection to fully auth before continuing (default TRUE)
			#2nd "true/false" arg - if true - when attempt fails, method returns an object, if false - return exception(default FALSE)
		self.objTAB = crt.Session.ConnectInTab("/Firewall=firewall /SSH2 /ACCEPTHOSTKEYS /PASSWORD %s %s@%s" % (self.PASS,self.USER,self.DEV_IP), False, True)
		errorMessages = crt.GetLastErrorMessage()
		self.objTAB.Screen.Synchronous = True
		
		if self.objTAB.Session.Connected == True:
			screenrow = self.objTAB.Screen.CurrentRow - 1
			data = self.objTAB.Screen.Get(screenrow,1,screenrow,40)
			if self.objTAB.Screen.WaitForString('assword:', 4) == True:
				self.FILE.write("FAILED: WRONG Password")
				return "SSH-FAILED"
			else:
				self.FILE.write("SUCCESS")
				self.f_command()
				return "SSH-SUCCESS"
		else:
			if (errorMessages != ""):
				self.FILE.write("FAILED: Could not establish SSH")
				return "SSH-FAILED"
					
	#-----------------------
	def f_connect_telnet(self):
		errcode = 0
		errorMessages = ""
		self.FILE.write("\nTrying TELNET to - %s -> RESULT = " % (str.strip(self.DEV_IP)))
		try:
			self.objTAB = crt.Session.ConnectInTab("/Firewall=firewall /telnet %s " % (self.DEV_IP))
			self.objTAB.Screen.Synchronous = True
			if self.objTAB.Screen.WaitForString("sername:",2) == True:
				self.objTAB.Screen.Send(self.USER + "\r")
				self.objTAB.Screen.Send(self.PASS + "\r")
				if self.objTAB.Screen.WaitForString("sername:",2) == False:
					self.FILE.write("SUCCESS")
					self.f_command()
					return "TELNET-SUCCESS"
				else:
					self.FILE.write("FAILED: Wrong Password")
					return "TELNET-FAILED"
					
		except:
			errorMessages = crt.GetLastErrorMessage()
			self.FILE.write("FAILED: %s " % (errorMessages))
			return "TELNET-FAILED"

	#-----------------------
	def f_command(self):
		szPrompt = "#"
		# Wait for the command and the trailing CR to be echoed back from the remote
		# before we start capturing data... Otherwise, we'll capture the command we
		# issued, as well as the results, and in this example, we only want to
		# capture the results.
		self.objTAB.Screen.Send(self.command)
		self.objTAB.Screen.WaitForString("#",3)
		self.objTAB.Screen.Send("configure terminal\r\n")
		if self.objTAB.Screen.WaitForString("(config)#") == True:
			crt.Dialog.MessageBox("you are in CONF T")
			#self.objTAB.Activate()			//doesn't help
			self.objTAB.Screen.Send("end\r\n")
			self.objTAB.Screen.WaitForString("#")
		else:
			crt.Dialog.MessageBox("you are not in CONF T")
		#self.FILE.write("\n"+szResult)
		#crt.Dialog.MessageBox(szResult)
		return
	#-----------------------
	def f_close(self):
		self.objTAB.Close()
		return

#=========================================================		
		
		
def main():
	#------var initiation --------------
	crt.Screen.Synchronous = True
	auth_error_count = 0
	result = "SSH-FAILED"
	file_output = "C:\\python\\output.txt"
	dev_counter = 1
	command = "sh run | i snmp-server location\r"
	USERNAME = "user1"
	#------end of var initiation--------
	with open(file_output,'w') as f:
		try:
			pass
		except:
			crt.Dialog.MessageBox("can't open file")
			quit()
		
	file = open(file_output,'w') 
	
	PASS = crt.Dialog.Prompt("Enter password: " , "Login", "", True)

	with open("c:\\python\\MYFILE.txt","r") as MYFILE:
		MYDATA = MYFILE.readlines()		
	
	#=== table declaration [ IP , SNMP to be sent ]
	TABLE = [["" for j in range(2)] for i in range(len(MYDATA))]	
	for x in range(len(MYDATA)):
		LINE_X = MYDATA[x]
		lista = re.split(r'\|',LINE_X)
		IP_X = lista[0]
		SNMP_X = lista[1]
		TABLE[x][0] = IP_X
		TABLE[x][1] = SNMP_X		
	
	# checking default setting for Authentication prompt in case of failure 
	# if TRUE - windows popup, we need cli in order to process the output further
	# saving this setting to a variable (CRT_DEFAULT_cli_prompt) to set it as it
	# was before this change
	CRT_DEFAULT_SESSION = crt.OpenSessionConfiguration("Default")
	CRT_DEFAULT_cli_prompt = CRT_DEFAULT_SESSION.GetOption("Auth Prompts in Window")
	#crt.Dialog.MessageBox(str(CRT_DEFAULT_cli_prompt))
	
	
	if CRT_DEFAULT_cli_prompt == False:
		CRT_DEFAULT_SESSION.SetOption("Auth Prompts in Window", True)
		CRT_DEFAULT_SESSION.Save()
	
	
	file.write("===============================================================\n")
	
	#for x in range(len(TABLE)):
	for x in range(3):
		file.write(str(dev_counter)+") ")
		dev_counter = dev_counter + 1

		DEV_TEST = DEVICE(TABLE[x][0],PASS,USERNAME,file,"test","none",command)
		result = DEV_TEST.f_connect_ssh()

		if result == "SSH-FAILED":
			result = DEV_TEST.f_connect_telnet()
			if result == "TELNET-FAILED":
				crt.Dialog.MessageBox("You have provided wrong password\nTo avoid locking the account\nscript will be terminated")
				file.close()
				quit()
				#DEV_TEST.f_close()
		
		file.write("\n===============================================================\n")
	
	
	file.close()
	
	# setting default option to value from before script execution
	CRT_DEFAULT_SESSION.SetOption("Auth Prompts in Window", CRT_DEFAULT_cli_prompt)
	CRT_DEFAULT_SESSION.Save()

	
main()
Reply With Quote