Welcome to the VanDyke Software Forums

Join the discussion today!


Go Back   VanDyke Software Forums > Scripting

Reply
 
Thread Tools Rate Thread Display Modes
  #1  
Old 12-02-2015, 11:05 PM
andymc andymc is offline
Registered User
 
Join Date: Sep 2015
Posts: 14
Can not disconnect - python scripting

Hello,

I have some questions please ::

1. I am trying to log into lots of modems and get some basic info from them, all i want is the current software version and serial number from them.

When i log into the devices, there is no guarantee that the format of the text will be the same as the last, so i am struggling to use waitForString, because, a certain string might not appear, so is there a way to read all the output from a certain input ????

2. I am trying to use the below code on 200 ish devices, however, on certain devices, it hangs, as in, i log in, the code runs, the display is on the screen, but, nothing happens, it does not disconnect ?? Its always the same devices that it hangs on, yet i can use secureCRT to log into them directly, and log out, however it will no disconnect when i run the code, so i wonder, is there a more forcible way to disconnect other than crt.Session.Disconnect() ???

3. I find it annoying not being able to output 'type' to screen, i.e.

crt.Dialog.MessageBox(type(what_ever_variable))

its got to be a string, so ....

crt.Dialog.MessageBox(str(type(what_ever_variable)))

But, obviusly that just sends String to the screen, there must be a better way to debug the code.

Any help would be greatly appreciated

Code:
# $language = "python"
# $interface = "1.0"

errcode = 0

user1 = "USERNAMEONE"
passwd1 = "PASSWORDONE"

def main():

	crt.Screen.Synchronous = True

	num = 0
	errorMessages = ""
	what_you_looking_for = "enable\rshow version"

	# Open the results.txt file and start writing the XML document 
	file = open('results.xml' , 'w')
	file.write("<?xml version=\"1.0\"?>\n")
	file.write("<results>\n")
	file.close()	

	file = open('hosts.txt', 'r')
	hosts = file.readlines()
	file.close()
	
	file = open('serial_numbers.txt', 'r')
	serial_numbers = file.readlines()
	file.close()

	for each_node in hosts:
	
		connector(each_node)
		
		crt.Screen.Send("enable\rshow version\rHe77")
			
		strResult = crt.Screen.ReadString("He77")
		strResult = str(strResult)	
		
		dummy = serial_numbers[num].rstrip()
		crt.Dialog.MessageBox(str(dummy))
		
		
		file = open('results.xml' , 'a')
		file.write("<output>" + strResult + "</output>\n")
		file.write("</host>")	
		file.close()
		num += 1
			


		disconnect_me()
	
		
def disconnect_me():
	if crt.Session.Connected: 
		crt.Session.Disconnect()
		crt.Dialog.MessageBox("Should now disconnect")


def all_done():
	file = open('results.xml' , 'a')
	file.write("</results>\n")
	file.close()
	
	crt.Dialog.MessageBox("Thats me all done.")

		
def connector(each_node) :

	# try :: try and connect using your TACACS credentials 
	try:

		# Check to see if we are connected, and if we are disconnect the current session 
		if crt.Session.Connected: 
			crt.Session.Disconnect()

		# try and log in with the first credentials
		cmd = "/SSH2 /L %s /ACCEPTHOSTKEYS /PASSWORD %s /C 3DES /M SHA1 %s" % (user1, passwd1, each_node.rstrip())
		crt.Session.Connect(cmd)

		if crt.Session.Connected: 
			return		
		
	except ScriptError:

		error = crt.GetLastErrorMessage()

		
main()
all_done()
Reply With Quote
  #2  
Old 12-03-2015, 10:07 AM
bgagnon bgagnon is offline
VanDyke Technical Support
 
Join Date: Oct 2008
Posts: 4,024
Hi andymc,

What version of SecureCRT are you using?

On what platform/OS?

I will try to address some of your inquiries in my next post, but for sure I can see instances where you have forgotten the best practice of always having a WaitForString() following a Send():

Code:
	crt.Screen.Send("enable\rshow version\rHe77")
In the above line, you are sending three commands without an intervening WaitForString().

That can definitely cause issues and even though our scripting manual is for VBScript, the concept is still applicable. See section 4.2.
__________________
Thanks,
--Brenda

VanDyke Software
Technical Support
support@vandyke.com
(505) 332-5730
Reply With Quote
  #3  
Old 12-03-2015, 01:48 PM
andymc andymc is offline
Registered User
 
Join Date: Sep 2015
Posts: 14
Hello,

What version of SecureCRT are you using? - Version 7.2.5 (x64 build 550) - Official Release - June 5, 2014

On what platform/OS? - Windows 7 Professional

Code:
crt.Screen.Send("enable\rshow version\rHe77")
The above line is a bit over the top, however, i get the same results with

Code:
crt.Screen.Send("show version")
With respect to the

Code:
rHe77
What i was thinking there was, if i sent a nonsense command that does nothing, and then wait until i see it with

Code:
strResult = crt.Screen.ReadString("He77")
Then i will be able to capture all the data from the screen.

Andy
Reply With Quote
  #4  
Old 12-03-2015, 02:19 PM
bgagnon bgagnon is offline
VanDyke Technical Support
 
Join Date: Oct 2008
Posts: 4,024
Hi andymc,

Even if you break that out to 3 lines, the point is there should be a WaitFor between each. Usually you wait for the shell prompt. You stated in the earlier post that there was not really any common text you could use in the WaitFors.

Is there not even some pattern, such as the hostname you connect to being part of the prompt, or the device name?

I would definitely recommend you look at our example script that illustrates how to connect to many hosts and send commands.

Where your objective differs is that you need to get certain pieces of data from the output but there have been a few posts of a similar nature in the forums that illustrate that.

Quote:
What i was thinking there was, if i sent a nonsense command that does nothing, and then wait until i see it with
strResult = crt.Screen.ReadString("He77")
Then i will be able to capture all the data from the screen.
I am not sure what you mean by the above. Are you referring to this feature?

Changes in SecureCRT 7.2 (Beta 1) -- October 8, 2013
----------------------------------------------------

New features:

  • The script function Screen.Send() can be used to send text to the screen only by using a new optional parameter.
__________________
Thanks,
--Brenda

VanDyke Software
Technical Support
support@vandyke.com
(505) 332-5730
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -6. The time now is 04:37 AM.