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 10-25-2018, 12:25 PM
yous yous is offline
Registered User
 
Join Date: Oct 2018
Posts: 6
Post writing ssh result to CSV file

Hi There, thanks in advance for your help
I'm using one of the py script (getdata_to_excel.py) along with MultiSessionsSend.py. in my ssh result I have multiple lines but this script only write one line to CSV file. any idea on how to transfer al lines to CSV

Code:
screenrow = crt.Screen.CurrentRow 
		readline = crt.Screen.Get(screenrow, 1, screenrow, 100)


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

		row = row + 1
Reply With Quote
  #2  
Old 10-25-2018, 01:22 PM
berdmann berdmann is offline
VanDyke Technical Support
 
Join Date: Aug 2017
Posts: 89
Hi yous,

The example code for get-data-to-excel.py.txt already writes out multiple lines of result data to the CSV file.

The script runs a command that generates output.

1) Then the script waits for either a new line to be received or for the shell prompt to appear (so that it knows when the command output has been received).

2) If a new line appears, (that's the \n), SecureCRT uses crt.Screen.Get() to "get" the line of text above the cursor (because a new line has just been received, the new data is expected to be above the line where the cursor currently exists).

2a) That line is then "split" into components, and the first 2 components are written out to the CSV file.

2b) If the shell prompt appears, the while loop is exited.

3) If the shell prompt didn't appear, the script then loops up to step 1

If you're not getting multiple lines of output from your shell command written to the resulting CSV file, you'll need to provide more context as to the command you're running, the example output that shows up on the screen, and the resulting CSV file.

Note that if the remote app/cmd you're running does note provide output terminated with a \n (if instead, it drives the screen by moving the cursor around with escape sequences), you'll have to take a different approach to getting each "line"; such an approach will involve a variable number of factors each of which are dependent upon knowing exactly what data is being received and how -- something which a raw log can typically reveal.
__________________
Thanks,
--Brittney

VanDyke Software
Technical Support
support@vandyke.com
(505) 332-5730
Reply With Quote
  #3  
Old 10-25-2018, 01: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 03:56 PM.
Reply With Quote
  #4  
Old 10-25-2018, 01:46 PM
yous yous is offline
Registered User
 
Join Date: Oct 2018
Posts: 6
I forgot to add that when I enter command "sh int status" in ssh prompt, I get about 20 lines, only the last line shows on the csv file. thanks again
Reply With Quote
  #5  
Old 10-26-2018, 10:00 AM
berdmann berdmann is offline
VanDyke Technical Support
 
Join Date: Aug 2017
Posts: 89
Hi yous,

You modified the script such that you are no longer waiting for the "/n" that signifies the end of each line of output. With your modifications to the script, the prompt is being waited for and then you are parsing only the line above the prompt, thus resulting in the single line of data written to the csv file.

We made a couple modifications to your code:

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.

             while True:
                 # Wait for either a new line of data to appear ("\n") or the
                 # shell prompt to appear (which signifies there isn't any more
                 # lines of data to process.
                 crt.Screen.WaitForStrings([  "\n",    prompt])
                 #  MatchIndex wil be either   1   or     2

                 # If we found the prompt, let's bail from our loop
                 if crt.Screen.MatchIndex == 2:
                     break

                 # otherwise, we're still in the loop, and we have to proces
                 # the new row of data that we just received.
                 screenrow = crt.Screen.CurrentRow - 1
                 # get the line above where the screen's cursor currently is:
                 readline = crt.Screen.Get(screenrow, 1, screenrow, 40)

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

                 # write out the first two elements of the array to the CSV file:
                 worksheet.writerow(items[:2])

     fileobj.close()
Does the modified script work better for you?
__________________
Thanks,
--Brittney

VanDyke Software
Technical Support
support@vandyke.com
(505) 332-5730
Reply With Quote
  #6  
Old 10-26-2018, 12:25 PM
yous yous is offline
Registered User
 
Join Date: Oct 2018
Posts: 6
thanks again berdmann.
unfortunately still showing only the hostname(one line :ASAs-XXX-XXX-xx-01).
As you can see below I have more than one line
ASAs-XXX-XXX-xx-01#sh int status

Port Name Status Vlan Duplex Speed Type
Gi1/0/1 Wireless ( Acti connected 999 a-full a-1000 10/100/1000BaseTX

Also, only one session can be opened. I can only open the CSV file in read only as the process still running. if I cancel the session, I get an error WaitForString : Not connected
Code:
rt.Screen.WaitForStrings([  "\n",    prompt])
thanks

Last edited by yous; 10-26-2018 at 12:30 PM.
Reply With Quote
  #7  
Old 10-26-2018, 01:09 PM
berdmann berdmann is offline
VanDyke Technical Support
 
Join Date: Aug 2017
Posts: 89
Hi yous,

Can you please post the exact code that you are running now? (Less any passwords or sensitive data)
__________________
Thanks,
--Brittney

VanDyke Software
Technical Support
support@vandyke.com
(505) 332-5730
Reply With Quote
  #8  
Old 10-26-2018, 06:45 PM
yous yous is offline
Registered User
 
Join Date: Oct 2018
Posts: 6
Hi berdmann,
I used the same code as you provided above with password and session file location. all I'm trying to do is open an ssh session for each site as I have 100 of them. Issue a command at each ssh session and have the result transfered to csv file. I can do this with Log session function, but then I need to convert this log file to a csv file.
thanks

Last edited by yous; 10-26-2018 at 06:48 PM.
Reply With Quote
  #9  
Old 10-29-2018, 10:39 AM
berdmann berdmann is offline
VanDyke Technical Support
 
Join Date: Aug 2017
Posts: 89
Hi yous,

In order to get a better understanding of the behavior it would be helpful to review a raw log of the connection up to the point when the script fails.

Can you please take the following steps to generate a raw log?

1. Before connecting with the session, select "Raw Log Session" from SecureCRT's "File" menu.

2. In the "Select Log File" dialog, choose a folder and filename in which you would like the log text to be stored and press the "Save" button. At this point, if you open the "File" menu, you will notice that the "Raw Log Session" menu item now has a check-mark next to it; this indicates that raw logging is activated.

3. Now connect to the remote machine and run the script until it fails.

4. Disconnect the session and select "Raw Log Session" from the "File" menu to turn off the raw logging feature.

5. Browse to the location of the raw log file and add the file as an attachment to your email reply.

Once you have successfully generated the raw log please attach it to an email to support@vandyke.com, with ATTN: Brittney, Forum Post #13298 in the subject field.
__________________
Thanks,
--Brittney

VanDyke Software
Technical Support
support@vandyke.com
(505) 332-5730
Reply With Quote
  #10  
Old 10-31-2018, 12:05 PM
yous yous is offline
Registered User
 
Join Date: Oct 2018
Posts: 6
Hi,
Unfortunately Raw Log session came out empty 0KB, same as the chart.csv. the code ends at the line below withing for a new line data to appear. Even though I have a disconnect statement at the end, it doesn't reach it. The session get disconnected only if I manually hit the disconnect button. when I do that, I get an error " connection lost" with the code " crt.Screen.WaitForStrings "\n", prompt ".

thanks
Code:
# 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 = "\\\xxxxxxxxxx\xxxxxxxxxxxxx\chart.csv"
	fileobj = open(filename, 'wb')

	worksheet = csv.writer(fileobj)


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


	sessionsFileName = "\\\xxxx\xxxxx\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 = "xxxxxxx"
		# Password for authenticating to the remote system
		Password = "xxxxx"

		try:
			cmd = "/SSH2 /L %s /PASSWORD %s /C AES-192 /M SHA1 %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

			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)

			
		while True:
                     # Wait for either a new line of data to appear ("\n") or the
                 # shell prompt to appear (which signifies there isn't any more
                 # lines of data to process.
                      crt.Screen.WaitForStrings([  "\n",    prompt])
                 #  MatchIndex wil be either   1   or     2

                 # If we found the prompt, let's bail from our loop
                      if crt.Screen.MatchIndex == 2:
                          break

                 # otherwise, we're still in the loop, and we have to proces
                 # the new row of data that we just received.
                      screenrow = crt.Screen.CurrentRow - 1
                 # get the line above where the screen's cursor currently is:
                      readline = crt.Screen.Get(screenrow, 1, screenrow, 40)

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

                 # write out the first two elements of the array to the CSV file:
                      worksheet.writerow(items[:2])

		#row = row + 1


		      fileobj.close()
		      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)

			


Main()

Last edited by yous; 10-31-2018 at 12:10 PM.
Reply With Quote
  #11  
Old 10-31-2018, 12:24 PM
bgagnon bgagnon is offline
VanDyke Technical Support
 
Join Date: Oct 2008
Posts: 3,570
Hi yous,

At the time when the script hangs, what output do you have in the terminal window?

I don't want any specific, sensitive data revealed, but a screenshot showing what's in the terminal window might help.

If the script has run the two prior commands (terminal length 0 and sh int status), then there should be *something* captured to the raw log.
__________________
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 09:23 AM.