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-26-2019, 01:59 PM
never2spicy never2spicy is offline
Registered User
 
Join Date: Dec 2019
Posts: 16
Needing help on this script for doing inventory on cisco devices

First off all credit for this goes to u/clearmoon247 on reddit who put this out there 4 years ago.

However I'm running into a line 27 runtime error which is think is : crt.Screen.Send "ssh " & USER & "@" & IP & chr(13)

Now it does prompt me for user/pass/hostname and I do give it an IP. But i get the error.

so first Id like to get this working. Next I'd like an alternate version i can point to a file with the IPs in it for runnign against.

Thanks in advance and happy holidays VanDyke support!


Code:
#$language = "VBScript"
#$interface = "1.0"
 
' chr(13) is "enter"
' chr(27) is "Esc"
' chr(124) is "|"
' chr(32) is "Space to next page"
' chr(3) is "Ctrl + C"
 
Const ForReading = 1
Const ForWriting = 2
crt.Screen.Synchronous = True
 
  Dim logfile
  logfile = "C:\TEMP\mysession.log"
  crt.Session.LogFileName = logfile
  'crt.Session.Log True
 
Sub Main
 USER = crt.Dialog.Prompt("What is your username?")
 Password = crt.Dialog.Prompt("What is your password?", "Login", "", True)
        HOSTS = crt.Dialog.Prompt("What are the Hosts you want to pull the serials for? (List each hostname, followed by a comma)")
        HOP_ARRAY = SPLIT(HOSTS, ",")
        crt.Session.Log True
        For each IP in HOP_ARRAY
                'crt.Screen.WaitForString "$", 5
                crt.Screen.Send "ssh " & USER & "@" & IP & chr(13)
                crt.Screen.WaitForString "*", 1
                Dim Pending
                Pending = crt.Screen.WaitForString("assword:", 3)
                If Pending = 0 Then
                        'crt.Session.Log False
                        'crt.Dialog.MessageBox("Unknown Device")
                  Else '>
                        crt.Screen.Send Password & chr(13)
                                prompt = crt.Screen.WaitForStrings(">", "#", CInt(15))
                                        select case prompt
                                        case 0 'Timeout, Unknown
                                                Error = 1
                                                crt.Dialog.MessageBox("Unknown Prompt")
                                                crt.Session.Log False
                                                exit sub
                                        case 1 '>
                                                crt.Screen.Send "show inventory " & chr(124) & " i SN:" & chr(13)
                                                crt.Screen.WaitForString ">"
                                                crt.Screen.Send "logout" & chr(13)
                                                crt.Screen.WaitForString "$"
                                        case 2
                                                crt.Screen.Send "show inventory " & chr(124) & " i SN:" & chr(13)
                                                crt.Screen.WaitForString "#"
                                                crt.Screen.Send "exit" & chr(13)
                                                crt.Screen.WaitForString "$"
                                        end select
                        End If
        next
        crt.Screen.Send "clear" & chr(13)
        crt.Session.Log False
        crt.Screen.WaitForString "$"
        crt.Screen.Send "cd" & chr(13)
' Creating output for filtering
        crt.Screen.Send "rm results.txt" & chr(13)
        crt.Screen.WaitForString "$"
        crt.Screen.Send "vim results.txt" & chr(13)
        crt.Screen.WaitForString "$", 10
        crt.Screen.Send "i"
          Dim fso, file, str
        Set fso = CreateObject("Scripting.FileSystemObject")
        Set file = fso.OpenTextFile("C:\Temp\mysession.log", ForReading, False)
                Do While file.AtEndOfStream <> True
                str = file.Readline
                crt.Screen.Send str & Chr(13)
                Loop
        crt.Screen.Send chr(27) & "ZZ"
        crt.Screen.WaitForString "$"
        crt.Screen.Send "clear"
        crt.Screen.WaitForString "$"
        MsgBox "Finished gathering information"
        crt.Screen.Send "cat results.txt | grep 'inventory\|SN:' | sed -e 's/V06,//g' | sed -e 's/VID://g' | sed -e 's/ //g' | sed -e 's/ //g' | sed -e 's/show inventory | i SN://g' | sed -e 's/V0[0-9][ ,]//g' | grep -v PWR- | grep -v 1DSU-T1 | grep -v 'FAN\|SFP\|10GB\|7041-E-R\|3000W\|PVC4\|SUP2T\|DFC4\|48-GE\|VTT-E\|1100W\|HWIC\|SX-MMD\|X45-SUP\|RJ45\|MSFC5\|PFC4\|6000W\|48-RJ\|WS-F\|WS-X\|M16UP\|CLK-\|BUILT-IN\|Unspecified' | sed -e 's/PID://g' | sed -e 's/,//g' | sed -e 's/>//g' | sed -e 's/#//g'" & chr(13)
 Dim result
 
End Sub

Last edited by berdmann; 01-09-2020 at 05:22 PM. Reason: Wrap [code] [/code] tags around code to preserve indentation and spacing
Reply With Quote
  #2  
Old 12-27-2019, 07:44 AM
dracot's Avatar
dracot dracot is offline
Registered User
 
Join Date: Nov 2007
Location: Sevilla , España (Spain)
Posts: 2
Do you have an established session before running the script? If not, the Send method will fail. On the other hand, although it should not be a cause for failure, I recommend using the vbs variable VbCr (or VbCrLf) instead of chr (13).

As for using files for IPs, I give you examples that ping instead of SSH.

By text file:

Const ForReading = 1
Const file = "F:\IpsPing.txt"
Set fso = CreateObject("Scripting.FileSystemObject")

Set file = fso.OpenTextFile(file, ForReading, False)

Do while file.AtEndOfStream <> True
Ip_Gest=file.Readline
crt.Screen.Send "ping " & Ip_Gest & VbCr
Loop



It is also easy from an excel file or a database, if you need it I give you some example.
Reply With Quote
  #3  
Old 12-27-2019, 10:14 AM
bgagnon bgagnon is offline
VanDyke Technical Support
 
Join Date: Oct 2008
Posts: 4,163
Hi never2spicy,

To get your example working would require more info.

Please check these examples in our forums to see if they will accomplish the objective.

Happy Holidays to you also!
__________________
Thanks,
--Brenda

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

Last edited by bgagnon; 12-27-2019 at 10:15 AM. Reason: added holiday response
Reply With Quote
  #4  
Old 12-27-2019, 10:15 AM
never2spicy never2spicy is offline
Registered User
 
Join Date: Dec 2019
Posts: 16
Ive tried both with an established and no established sessions, same results. My end goal is to grab the IPs from a list, connect, get the data, and export it out to a page in html, xml, etc if you have pieces for that. Basically trying to make something like a homemade Cisco Smartnet Collector.
Reply With Quote
  #5  
Old 12-27-2019, 01:05 PM
bgagnon bgagnon is offline
VanDyke Technical Support
 
Join Date: Oct 2008
Posts: 4,163
Hi never2spicy,

There is no mechanism in SecureCRT to export data in html/xml.

I have added this thread to a feature request in our product enhancement database for the ability to log/export in html/xml. Should a future release of SecureCRT include this feature, notification will be posted here.

If you prefer direct email notification, send an email to support@vandyke.com and include Feature Request - Forum Thread #14022 in the subject line or use this form from the support page of our website.


Quote:
However I'm running into a line 27 runtime error which is think is : crt.Screen.Send "ssh " & USER & "@" & IP & chr(13)
Please post a screenshot of the error.

Quote:
so first Id like to get this working. Next I'd like an alternate version i can point to a file with the IPs in it for runnign against.
It's actually easier (IMHO) to start out with the more difficult project than to try to add the functionality later. But first we need a better starting script. Without indentation or color for the comments, I am completely lost as far as the example code you posted.

We do not write custom scripts, but I've incorporated reading hosts from a file with your prior posted code in the attached example. I was not quite sure how the editing of the log via vim was going to happen because of the multiple hosts. (I included some inquiries in the comments, just look for Never2Spicy.)

Let me know what sort of issues occur. It would be best to first test just with a couple hosts in your hosts.txt file.
Attached Files
File Type: txt ReadDataFromHostFile_SendCommands_GetSerials.vbs.txt (9.2 KB, 56 views)
__________________
Thanks,
--Brenda

VanDyke Software
Technical Support
support@vandyke.com
(505) 332-5730
Reply With Quote
  #6  
Old 12-30-2019, 12:07 PM
never2spicy never2spicy is offline
Registered User
 
Join Date: Dec 2019
Posts: 16
Ok I think maybe I'm over complicating this so let me redefine what i want to do. I know you cant write code for me but perhaps you have an old example that might help get me part of the way. I'll break this into pieces in case some parts can be shown separately and I can work on integration.

- I want to prompt the user for a list of IP addresses
- From this list it will SSH into these devices
- Once connected it will run a command to gather some data
- Store all the output from these sessions in a unified document for be to manually convert to use on a webpage since its not an option

Heres an example of the current output. I'd like to capture the hostname,


<hostname># show inventory
NAME: "Chassis", DESCR: "Nexus 3048 Chassis"
PID: N3K-C3048TP-1GE , VID: V01 , SN: <serial # 1>

NAME: "Slot 1", DESCR: "48x1GE + 4x10G Supervisor"
PID: N3K-C3048TP-1GE , VID: V01 , SN: <serial # 2>

NAME: "Power Supply 1", DESCR: "Nexus 3048 Chassis Power Supply"
PID: N2200-PAC-400W , VID: V06 , SN: <serial # 3>

NAME: "Power Supply 2", DESCR: "Nexus 3048 Chassis Power Supply"
PID: N2200-PAC-400W , VID: V06 , SN: <serial # 4>

NAME: "Fan 1", DESCR: "Nexus 3048 Chassis Fan Module"
PID: N3K-C3048-FAN-F , VID: V00 , SN: <serial # 5>
Reply With Quote
  #7  
Old 12-30-2019, 12:17 PM
bgagnon bgagnon is offline
VanDyke Technical Support
 
Join Date: Oct 2008
Posts: 4,163
Hi never2spicy,

I previously gave you a link to an example that does provide all that functionality.

But you ignored that, signifying to me that you wanted to continue using the example you got from u/clearmoon247 on reddit.

So then I provided an example that was a combination of the two. Did you try either example script? What were the results?
__________________
Thanks,
--Brenda

VanDyke Software
Technical Support
support@vandyke.com
(505) 332-5730
Reply With Quote
  #8  
Old 12-31-2019, 11:35 AM
never2spicy never2spicy is offline
Registered User
 
Join Date: Dec 2019
Posts: 16
I acknowledge that you sent this. I tried it. As I said its overly complex and does not prompt the user. It requires multiple files in set locations instead of being a self contained script. I was also wanting to just glean the data I needed without extra fields.
Reply With Quote
  #9  
Old 12-31-2019, 12:10 PM
bgagnon bgagnon is offline
VanDyke Technical Support
 
Join Date: Oct 2008
Posts: 4,163
Hi never2spicy,

You have clearly illustrated you know how to prompt the user, so I thought you could make those replacements in Jake's script as needed.

But still, you only answered half the question.

What about the example script attached to the prior post?


It's not complete by any means but we have to have a starting point.

Quote:
It requires multiple files in set locations instead of being a self contained script.
And I thought you said you wanted to read hosts from a file? So I am not sure what you mean by the above.
__________________
Thanks,
--Brenda

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

Last edited by bgagnon; 12-31-2019 at 12:28 PM. Reason: Clarification
Reply With Quote
  #10  
Old 01-09-2020, 04:20 PM
never2spicy never2spicy is offline
Registered User
 
Join Date: Dec 2019
Posts: 16
First off hope you had a good holiday! Thanks in advance for all your help, really enjoying playing with this.

So i scaled this waaaay back dropping all the creating files and catting them for results. Now only trying to prompt the user for user/pass/hosts which should ssh to those hosts and run a single command and exit. I think the other example you gave will be helpful soon but right now thats alot more than i need for this particular task.

Couple issues:

1) getting error and End Sub, think im missing something closing my statements.

2) Originally crt.Screen.Send was used but thinking crt.Session.Connect might be the better way to go, just need some assistance in how to syntax the HOSTS and USER variables in place.

Feel im really close on this. Might need to tweak the log settings to dump these into the same log file but once i do i'll have everything i need.


Code:
Sub Main
 USER = crt.Dialog.Prompt("Username?")
 Password = crt.Dialog.Prompt("Password?", "Login", "", True)
        HOSTS = crt.Dialog.Prompt("What are the Hosts you want to pull the serials for? (List each hostname, followed by a comma)")
        HOP_ARRAY = SPLIT(HOSTS, ",")
        For each IP in HOP_ARRAY
                'crt.Screen.WaitForString "$", 5
                crt.Screen.Send "ssh " & USER & "@" & IP & chr(13)
                crt.Screen.WaitForString "*", 1
                Dim Pending
                Pending = crt.Screen.WaitForString("assword:", 3)
                If Pending = 0 Then
                        'crt.Session.Log False
                        'crt.Dialog.MessageBox("Unknown Device")
                  Else '>
                        crt.Screen.Send Password & chr(13)
                                prompt = crt.Screen.WaitForStrings(">", "#", CInt(15))
                                        select case prompt
                                        case 0 'Timeout, Unknown
                                                Error = 1
                                                crt.Dialog.MessageBox("Unknown Prompt")
                                                crt.Session.Log False
                                                exit sub
                                        case 1 '>
                                                crt.Screen.Send "show inventory " & chr(124) & " i SN:" & chr(13)
                                                crt.Screen.WaitForString ">"
                                                crt.Screen.Send "logout" & chr(13)
                                        case 2
                                                crt.Screen.Send "show inventory " & chr(124) & " i SN:" & chr(13)
                                                crt.Screen.WaitForString "#"
                                                crt.Screen.Send "exit" & chr(13)
                                        end select
                        End If
	crt.Session.LogFileName = "\\NAUSPLNNMD001\Network Share Root\Logs\Inventory\" & strRFC & "\" & strRFC & "-ShowInventory-%Y-%M-%D--%h-%m-%s.%t-%S(%H).txt"
	crt.Session.LogUsingSessionOptions
	crt.Session.Log False
	crt.Session.Log True, True
	crt.Screen.Send chr(13)
	crt.Screen.Send chr(13)
	crt.Screen.Send chr(13)
	crt.Screen.Send chr(13)
	crt.Screen.Send chr(13)
	crt.Screen.Send "show inv " & chr(124) & " no-more" & chr(13)
 MsgBox "Script complete. Please see the \\NAUSPLNNMD001\Network Share Root\Logs directory for the log."
End Sub

Last edited by jdev; 01-09-2020 at 05:10 PM. Reason: Always use [code][/code] tags around your code for preserving indentation/spacing
Reply With Quote
  #11  
Old 01-10-2020, 10:37 AM
bgagnon bgagnon is offline
VanDyke Technical Support
 
Join Date: Oct 2008
Posts: 4,163
Hi never2spicy,
Quote:
1) getting error and End Sub, think im missing something closing my statements.
Please be more specific/elaborate. What error? Please post a screenshot. And I don't understand the phrase "getting error and End Sub"??


Quote:
2) Originally crt.Screen.Send was used but thinking crt.Session.Connect might be the better way to go, just need some assistance in how to syntax the HOSTS and USER variables in place.
I need some context. The methods do two different things. Send() is used to simulate typing a command in the terminal window. Connect() is used to connect a session. They would not really be interchangeable.

What do you mean by "how to syntax the HOSTS and USER variables in place"?

Again, be specific and elaborate.
__________________
Thanks,
--Brenda

VanDyke Software
Technical Support
support@vandyke.com
(505) 332-5730
Reply With Quote
  #12  
Old 01-13-2020, 01:35 PM
never2spicy never2spicy is offline
Registered User
 
Join Date: Dec 2019
Posts: 16
I'll email you. This exchange over forum too much is getting lost in the conversation.
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 11:42 PM.