View Single Post
  #3  
Old 10-25-2018, 12:37 PM
yous yous is offline
Registered User
 
Join Date: Oct 2018
Posts: 6
thanks berdmann, here is what my code looks like
Code:
#$language = "Python"
#$interface = "1.0"

# MultiSessionSend.py
#
# Description:
#	 Reads in sessions from a file (one session per line) and
#	 connects to each one (one at a time) and sends a series of
#	 commands to the remote, then disconnects from the session
#	 and moves on to the next session.
#	 
#	 By default, the file containing the sessions is expected to be named
#	 "SessionList.txt", and should be located in the current user's home
#	 directory.  Each line of the file is expected to contain the
#	 path to the session as it appears within the Connect dialog (excluding
#	 the leading "Sessions/" component.  Here is an example file contents:
#		 
#		 redhat
#		 redhat8
#		 192.168.0.123
#		 192.168.0.131
#		 Redhat Sessions\RedHat - XTerm
#
#	 For the sake of simplicity, this example assumes that all
#	 sessions we're connecting to are using the SSH1 and SSH2
#	 protocols, with usernames and passwords saved encrypted
#	 as part of the session configuration so that we don't need
#	 to worry about authentication within this script.


import csv
import os

def Main():
	errorMessages = ""

	# Create an Excel compatible spreadsheet
	#
	
        filename = "\\\RE-NT0001\exchange\chart.csv"
	fileobj = open(filename, 'wb')

	worksheet = csv.writer(fileobj)


	promptStr = "linux$"
	waitStrs = [ "\n", promptStr ]


	sessionsFileName = "\\\RE-NT0001\exchange\SessionList.txt"
	if not os.path.exists(sessionsFileName):
		crt.Dialog.MessageBox(
			"Session list file not found:\n\n" +
			sessionsFileName + "\n\n" +
			"Create a session list file as described in the description of " +
			"this script code and then run the script again.")
		return

	sessionFile = open(sessionsFileName, "r")
	sessionsArray = []

	for line in sessionFile:
		session = line.strip()
		if session:	# Don't add empty lines/sessions
			sessionsArray.append(session)

	sessionFile.close()

	# Connect to each session and issue a few commands, then disconnect.
	for session in sessionsArray:
		#crt.Dialog.MessageBox("Connecting to Session: " + session)
		# Username for authenticating to the remote system
		user = "yous"
		# Password for authenticating to the remote system
		Password = "xxxx#"

		try:
			cmd = "/SSH2 /L %s /PASSWORD %s /C 3DES /M MD5 %s" % (user, Password, session)
		
			crt.Session.Connect(cmd)

			
		except ScriptError:
			error = crt.GetLastErrorMessage()

		# If we successfully connected, we'll do the work we intend to do...
		# otherwise, we'll skip the work and move on to the next session in 
		# the list.
		if crt.Session.Connected:
			crt.Screen.Synchronous = True


			row = 1
			while True:				
				if not crt.Screen.WaitForCursor(1):
					break
			# Once the cursor has stopped moving for about a second, we'll
			# assume it's safe to start interacting with the remote system.

			# Get the shell prompt so that we can know what to look for when
			# determining if the command is completed. Won't work if the prompt
			# is dynamic (e.g. changes according to current working folder, etc)
			
			row = crt.Screen.CurrentRow
			prompt = crt.Screen.Get(row, 0, row, crt.Screen.CurrentColumn - 1)
			prompt = prompt.strip()

			crt.Screen.Send("terminal length 0\n")
			# Wait for the command to complete, by looking for the prompt to
			# appear once again.
			crt.Screen.WaitForString(prompt)
			
			crt.Screen.Send("sh int status\n")
			# Wait for the command to complete, by looking for the prompt to
			# appear once again.
			crt.Screen.WaitForString(prompt)
			
			crt.Screen.Synchronous = False
			# Now disconnect from the remote machine...
			crt.Session.Disconnect()
			# Wait for the connection to close
			while crt.Session.Connected == True:
				crt.Sleep(100)

			screenrow = crt.Screen.CurrentRow - 1
			readline = crt.Screen.Get(screenrow, 1, screenrow, 40)


		# Split the line ( ":" delimited) and put some fields into Excel
		#
		items = prompt.split(":")
		worksheet.writerow(items[:2])

		row = row + 1


	fileobj.close()

			

		

Main()

Last edited by ekoranyi; 10-25-2018 at 02:56 PM.
Reply With Quote