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 08-23-2016, 02:44 AM
andymc andymc is offline
Registered User
 
Join Date: Sep 2015
Posts: 14
running secure crt from command line

Hello good Peoeple of VanDyke,

I want to run a python script from the command line, i will then eventually schedule this task to run every X minutes.

I can start SecureCRT from the command line with

C:\PATH_TO_INSTALL_FOLDER\SecureCRT.exe

And i thought to run a script to include a script i would write

C:\PATH_TO_INSTALL_FOLDER\SecureCRT.exe C:\PATH_TO_SCRIPT

However when i do, i get the following error message.

"The Port Number supplied was invalid"

What port is being referred to here ?

The script i am trying to run, can be run by opening the SecureCRT GUI and running it from there.

Any ideas would be appreciated

Andt
Reply With Quote
  #2  
Old 08-23-2016, 07:21 AM
bgagnon bgagnon is offline
VanDyke Technical Support
 
Join Date: Oct 2008
Posts: 4,424
Hi Andy,

You need to include the /SCRIPT option.

See the following topics in SecureCRT's Help file. They fully document all available command-line options:
  • Using the Software / Command-Line Options / Overview of Command-Line Options
  • Using the Software / Command-Line Options / Table of Standard Command-Line Options
  • Using the Software / Command-Line Options / Table of Protocol-Specific Command-Line Options

The one you need is considered a standard option:
Code:
Option                       Arguments                     Description

/SCRIPT          filename                      Runs script contained in filename.
__________________
Thanks,
--Brenda

VanDyke Software
Technical Support
support@vandyke.com
(505) 332-5730
Reply With Quote
  #3  
Old 08-24-2016, 02:53 AM
kingboli kingboli is offline
Registered User
 
Join Date: Aug 2016
Posts: 5
Smile how to use /SERIAL /BAUD and /SCRIPT in one command line?

Hi all,

I want to use command line to auto create a session and then run a script in this session. And there is an error about parameter "/script" when I use below command in a bat file:

"C:\Users\AppData\Local\VanDyke Software\SecureCRT\SecureCRT.EXE" /SERIAL COM5 /BAUD 115200 /T /SCRIPT test.py


How to handle this issue?

Thanks.
Reply With Quote
  #4  
Old 08-24-2016, 08:52 AM
bgagnon bgagnon is offline
VanDyke Technical Support
 
Join Date: Oct 2008
Posts: 4,424
Hi kingboli,

As shown in the prior response, the order of the command-line options matter.

Standard options (such as /T and /SCRIPT) should come before protocol-specific options (such as /SERIAL and /BAUD).

C:\Users\AppData\Local\VanDyke Software\SecureCRT\SecureCRT.EXE" /T /SCRIPT test.py /SERIAL COM5 /BAUD 115200
__________________
Thanks,
--Brenda

VanDyke Software
Technical Support
support@vandyke.com
(505) 332-5730
Reply With Quote
  #5  
Old 08-24-2016, 09:18 AM
andymc andymc is offline
Registered User
 
Join Date: Sep 2015
Posts: 14
OK, my bad .... i was using the incorrect CLi

Below is one of many script i have written, and it works fine when i open Secure CRT GUI and then run a script from Script >> Run >> and then select my script, but when i run from the command line i always get an error ???

I tried running the command from line like below

Code:
C:\Program Files\VanDyke Software\SecureCRT\SecureCRT.exe /Script "C:\001\inspect.py"
I get an IO error on results.xml

So i thought i would specify the exact path with

Code:
file = open('C:\001\results.xml' , 'w')
But same error?

Any ideas

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

import time

user1 = "xxx"
passwd1 = "xxx"

user2 = "xxx"
passwd2 = "xxx"

user3 = "xxx"
passwd3 = "xxx"

global objTab
objTab = crt.GetScriptTab()

# not ticked = good authentication
# ticked = prompted for authentication

def main():

	# these two lines make sure that the authentication gets handled inside the window instead of being prompted 
	# you also no longer need to go into the Global Options / Default Options anymore
	
	start = time.time()
	crt.Screen.Synchronous = True
	hosts = readHostsAndPrepareXMLfile()
	
	num = 0
	
	for each_node in hosts:
	
		errcode = connector(each_node)														# establish connection
		standby_final = ""
		int_des_final = ""
		crt.Window.Caption = str(len(hosts) - num)											# purley visual
		
		if errcode == 0 :
																# if we are connected
			crt.Screen.Send("terminal length 0\n")
			hostname 		= GetTextLeftOfCursor()
			standby 		= captureMutipleLines("show standby")
			int_des 		= captureMutipleLines("show int des")

			
			# this takes out the random empty lines from the returned output
			for line in standby.splitlines():												# for each line in the string
				if line != "" :																		# if the line is not empty 
					standby_final = standby_final + line + "\n"						# add the line to my new string plus a new line 
					
			for line in int_des.splitlines():												# for each line in the string
				if line != "" :																		# if the line is not empty 
					int_des_final = int_des_final + line + "\n"						# add the line to my new string plus a new line 

				
					

			#split1 = default_route_command.splitlines()
			#numberOfDefaultRoutes = len(split1)	

			with open("results.xml", "a") as yourfile:

				yourfile.write("<host id=\"" + hosts[num].rstrip() + "\">")
				yourfile.write("<hostname> " + hostname + " </hostname>\n")
				yourfile.write("<standby> " + standby_final + " </standby>\n")
				yourfile.write("<description> " + int_des_final + " </description>\n")
				yourfile.write("</host>")
				yourfile.close()
					
				num += 1
				if crt.Session.Connected: 
					crt.Session.Disconnect()		
			
		elif errcode == 333 :		

			with open("results.xml", "a") as yourfile:

				yourfile.write("<host id=\"" + hosts[num].rstrip() + "\">")
				yourfile.write("<show_ver> Node down </show_ver>\n")
				yourfile.write("</host>")
				yourfile.close()
				
			num += 1
			if crt.Session.Connected: 
				crt.Session.Disconnect()
	
		elif errcode == 666 :							# Oh oh !
			crt.Dialog.MessageBox("All passwords have been tried ! Call an engineer ! ")			

	file = open('results.xml' , 'a')
	file.write("</results>\n")
	file.close()
	
	crt.Dialog.MessageBox("Thats me all done.")
	end = time.time()
	crt.Dialog.MessageBox(str(end - start))


def readHostsAndPrepareXMLfile():


	# 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()	
	return hosts

def GetTextLeftOfCursor():
    global objTab
    nRow = objTab.Screen.CurrentRow
    nCol = objTab.Screen.CurrentColumn - 1
    strTextLeftOfCursor = objTab.Screen.Get(nRow, 1, nRow, nCol)
    return strTextLeftOfCursor		

def captureMutipleLines(caputreMe) :

	# Here is where we will set the value of the string that will indicate that
	# we have reached the end of the data that we wanted capture with the
	# ReadString method.
	szPrompt = GetTextLeftOfCursor()

	# Using GetScriptTab() will make this script 'tab safe' in that all of the
	# script's functionality will be carried out on the correct tab. From here
	# on out we'll use the objTab object instead of the crt object.
	objTab = crt.GetScriptTab()
	objTab.Screen.Synchronous = True

	# Instruct WaitForString and ReadString to ignore escape sequences when
	# detecting and capturing data received from the remote (this doesn't
	# affect the way the data is displayed to the screen, only how it is handled
	# by the WaitForString, WaitForStrings, and ReadString methods associated
	# with the Screen object.
	objTab.Screen.IgnoreEscape = True

	# We begin the process by sending some a command. In this example script,
	# we're simply getting a file listing from a remote UNIX system using the
	# "ls -l" command.
	szCommand = caputreMe
	objTab.Screen.Send(szCommand + "\r\n")

	# 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.
	objTab.Screen.WaitForString(szCommand + "\r\n")

	# This will cause ReadString() to capture data until we see the szPrompt
	# value.
	szResult = objTab.Screen.ReadString(szPrompt)

	# Display the results in a messagebox
	# crt.Dialog.MessageBox(szResult)		
	return (szResult)				
	
def connector(each_node) :

	
	try:
		# Make sure we are not connected to anything before we try and connect to something
		if objTab.Session.Connected: 
			objTab.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())
		objTab.Session.Connect(cmd, False)

		# This will return true even if the username/password fails, it just means that we are connected, not authenticated
		if objTab.Session.Connected:
			# we should now start looking for the strings 
			result = objTab.Screen.WaitForStrings(["Password authentication failed", "#"], 30)
			if result == 1:
				objTab.Session.Disconnect()
			if result == 2:
				return 0		
			
	except ScriptError:
		# This will be called when the node is down
		return 333

	
	try:
		# Make sure we are not connected to anything before we try and connect to something
		if objTab.Session.Connected: 
			objTab.Session.Disconnect()

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

		# This will return true even if the username/password fails, it just means that we are connected, not authenticated
		if objTab.Session.Connected:
			# we should now start looking for the strings 
			result = objTab.Screen.WaitForStrings(["Password authentication failed", "#"], 30)
			if result == 1:
				objTab.Session.Disconnect()
			if result == 2:
				return 0	
				
	except ScriptError:
		return 333
		
	try:
		# Make sure we are not connected to anything before we try and connect to something
		if objTab.Session.Connected: 
			objTab.Session.Disconnect()

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

		# This will return true even if the username/password fails, it just means that we are connected, not authenticated
		if objTab.Session.Connected:
			# we should now start looking for the strings 
			result = objTab.Screen.WaitForStrings(["Password authentication failed", "#"], 30)
			if result == 1:
				objTab.Session.Disconnect()
			if result == 2:
				return 0	
	
	except ScriptError:
		return 333

		
main()
Reply With Quote
  #6  
Old 08-24-2016, 02:59 PM
bgagnon bgagnon is offline
VanDyke Technical Support
 
Join Date: Oct 2008
Posts: 4,424
Hi Andy,

Quote:
I get an IO error on results.xml

So i thought i would specify the exact path with

Code:
file = open('C:\001\results.xml' , 'w')

But same error?
What is the specific error message you are getting?
__________________
Thanks,
--Brenda

VanDyke Software
Technical Support
support@vandyke.com
(505) 332-5730
Reply With Quote
  #7  
Old 08-25-2016, 01:37 AM
andymc andymc is offline
Registered User
 
Join Date: Sep 2015
Posts: 14
Reply With Quote
  #8  
Old 08-25-2016, 10:00 AM
bgagnon bgagnon is offline
VanDyke Technical Support
 
Join Date: Oct 2008
Posts: 4,424
Hi Andy,

I get the same error if I am running the script (thus writing the results.xml file) to a directory where I do *not* have write permissions.

Since the paths are not fully-qualified, C:\001\ is where it will attempt to write the results.xml file. Do you have write permissions on that directory?
__________________
Thanks,
--Brenda

VanDyke Software
Technical Support
support@vandyke.com
(505) 332-5730
Reply With Quote
  #9  
Old 08-26-2016, 06:55 AM
andymc andymc is offline
Registered User
 
Join Date: Sep 2015
Posts: 14
Yes, i do have permissions.

When i open SecureCRT GUI and run the Script from the drop down menus, it works and i can write to C:\001\

When i run it from the command line i get the IO Error
Reply With Quote
  #10  
Old 08-26-2016, 10:34 AM
bgagnon bgagnon is offline
VanDyke Technical Support
 
Join Date: Oct 2008
Posts: 4,424
Hi andymc,

What user context do you run SecureCRT GUI under? (Is it admin level?)

What user are you logged on as when running SecureCRT via the command-line?

This *is* an issue of denied access and is outside of SecureCRT's control. You might try using Process Monitor to capture a log of both situations and evaluate how SecureCRT is running differently in each scenario.
__________________
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 07:45 PM.