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 05-29-2017, 01:51 PM
respite respite is offline
Registered User
 
Join Date: May 2017
Posts: 6
Post Printing only one line from script

Hello All,

Forgive me, I'm a little new to scripting with CRT. I'm trying to connect to a bunch of hosts and return only one line to a log file instead of printing the entire output from my command. I want to be able to return the line that says AP-51xx firmware version : 2.3.2.0-008R
as I'm specifically looking for for the model, in this case "AP-51xx". We have a few more models than that, I'll be scanning a few hundred devices and would like to be able to say "IP 172.xx.yyy.14x has model APxxxx" if that makes sense.

Here is the script..

#$language = "VBScript"
#$interface = "1.0"
crt.Screen.Synchronous = True

on error resume next


' Make sure we are disconnected before attempting a connection
If crt.Session.Connected Then crt.Session.Disconnect
' ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

crt.Session.Connect "/SSH2 /ACCEPTHOSTKEYS /L ****** /PASSWORD ******* 172.26.25.140"
' Do work on the remote machine.

crt.session.logfilename="APType_Poll_Output.log"
crt.session.log TRUE, TRUE
crt.Screen.WaitForString "admin>"
crt.Screen.Send "sum" & chr(13)
crt.Screen.WaitForString "admin>"
crt.Screen.Send "quit" & chr(13)
crt.Session.Disconnect
crt.session.log False

' Disconnect before moving on to the next host
crt.Session.Disconnect

crt.Session.Connect "/SSH2 /ACCEPTHOSTKEYS /L ****** /PASSWORD ****** 172.26.57.140"
' Do work on the remote machine.
crt.session.logfilename="APType_Poll_Output.log"
crt.session.log TRUE, TRUE
crt.Screen.WaitForString "admin>"
crt.Screen.Send "sum" & chr(13)
crt.Screen.WaitForString "admin>"
crt.Screen.Send "quit" & chr(13)
crt.Session.Disconnect
crt.session.log False



Here is the messy output. Note: Irrelevant/Sensitive data has been scrubbed.

admin>sum
AP-51xx firmware version : 2.3.2.0-008R
country code : ca
ap-mode : independent
serial number : **********

WLAN 1:
WLAN name : ######
ESS ID : ######
Radio : 11b/g
VLAN : ######
Security Policy : ######
QoS Policy : Default

WLAN 2:
WLAN name : ######
ESS ID : ######
Radio : 11b/g
VLAN : ######
Security Policy : ######
QoS Policy : Default

WLAN 3:
WLAN name : #######
ESS ID : #######
Radio : 11b/g
VLAN : ######
Security Policy : #######
QoS Policy : Default

WLAN 4:
WLAN name : ######
ESS ID : ######
Radio : 11b/g
VLAN : ######
Security Policy : ######
QoS Policy : Default

LAN1 Name: LAN1
LAN1 Mode: enable
LAN1 IP: 172.26.25.140
LAN1 Mask: 255.255.255.0
LAN1 DHCP Mode: static

LAN2 Name: LAN2
LAN2 Mode: disable
LAN2 IP: 192.168.1.1
LAN2 Mask: 255.255.255.0
LAN2 DHCP Mode: static

WAN Interface IP Address Network Mask Default Gateway DHCP Client
------------------------------------------------------------------------------
disable 10.1.1.1 255.0.0.0 0.0.0.0 disable



admin>sum
AP-51xx firmware version : 2.3.2.0-008R
country code : ca
ap-mode : independent
serial number : ***************

WLAN 1:
WLAN name : ######
ESS ID : ######
Radio : 11b/g
VLAN : ######
Security Policy : ######
QoS Policy : Default

WLAN 2:
WLAN name : ######
ESS ID : ######
Radio : 11b/g
VLAN : ######
Security Policy : ######
QoS Policy : Default

WLAN 3:
WLAN name : ######
ESS ID : ######
Radio : 11b/g
VLAN : ######
Security Policy : ######
QoS Policy : Default

WLAN 4:
WLAN name : ######
ESS ID : ######
Radio : 11b/g
VLAN : ######
Security Policy : ######
QoS Policy : Default

LAN1 Name: LAN1
LAN1 Mode: enable
LAN1 IP: 172.26.57.140
LAN1 Mask: 255.255.255.0
LAN1 DHCP Mode: static

LAN2 Name: LAN2
LAN2 Mode: disable
LAN2 IP: 192.168.1.1
LAN2 Mask: 255.255.255.0
LAN2 DHCP Mode: static

WAN Interface IP Address Network Mask Default Gateway DHCP Client
------------------------------------------------------------------------------
disable 10.1.1.1 255.0.0.0 0.0.0.0 disable

Help is greatly appreciated
Reply With Quote
  #2  
Old 05-30-2017, 11:57 AM
ekoranyi ekoranyi is offline
VanDyke Technical Support
 
Join Date: Jan 2017
Posts: 654
Hi respite,

I'm glad to hear you are exploring SecureCRT automation abilities.

I've included a brief example of one way this may be accomplished.

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

crt.screen.synchronous = true

Set g_fso = CreateObject("Scripting.FileSystemObject")
Set objFile = g_fso.OpenTextFile("C:\Users\<user>\Desktop\Log.txt", 8, True)

crt.screen.WaitForString("AP-")
strText = crt.screen.ReadString(vbcrlf)

crt.screen.WaitForString("LAN1 IP: ")
strIP = crt.screen.ReadString(vbcrlf)

objFile.Write strIP & " has model AP-" & strText & vbcrlf
objFile.Close
This assumes that every device will have a line that contains "AP-", there may be "hang" in the script if a device does not contain this line. If that is the case we may need to make some modifications.

Does this help?
__________________
Thanks,
--Eric

VanDyke Software
Technical Support
support@vandyke.com
(505) 332-5730
Reply With Quote
  #3  
Old 05-31-2017, 10:20 AM
respite respite is offline
Registered User
 
Join Date: May 2017
Posts: 6
Hey

Thanks for the quick reply! I see the logic but I seem to be missing something...

The script is connecting and executing commands but doesn't seem to be logging/searching the variables correctly. In my logfile "AP_Scan_Type_Log.txt"
It only shows has model AP- with nothing else in the text file. No IPs or model numbers.

Here is the updated code: (I've cleaned up how it connects to the hosts, as I'm reading your manuals lol)

Quote:
# $language = "VBScript"
# $interface = "1.0"

crt.screen.synchronous = true

on error resume next

Set g_fso = CreateObject("Scripting.FileSystemObject")
Set objFile = g_fso.OpenTextFile("C:\Users\nmorra\Documents\Scripts\logs\AP_Scan_Type_Log.txt", 8, True)

crt.screen.WaitForString("AP-")
strText = crt.screen.ReadString(vbcrlf)

crt.screen.WaitForString("LAN1 IP: ")
strIP = crt.screen.ReadString(vbcrlf)

objFile.Write strIP & " has model AP-" & strText & vbcrlf
objFile.Close

Dim vHosts(100)
vHosts(0) = "172.26.25.140"
vHosts(1) = "172.26.37.140"
vHosts(2) = "172.26.24.140"
vHosts(3) = "172.26.94.140"
vHosts(4) = "172.26.203.140"
vHosts(5) = "172.26.100.140"
vHosts(6) = "172.26.103.140"
vHosts(7) = "172.26.56.140"
vHosts(8) = "172.26.132.140"
vHosts(9) = "172.26.60.140"
vHosts(10) = ""

For Each strHost In vHosts
If strHost = "" Then Exit For

' Make sure we are disconnected before attempting a connection
If crt.Session.Connected Then crt.Session.Disconnect

' Connect to the next host
crt.Session.Connect "/SSH2 /ACCEPTHOSTKEYS /L ****** /PASSWORD ****** " & strHost

crt.session.logfilename="APType_Poll_Output.log"
crt.session.log TRUE, TRUE
crt.Screen.WaitForString "admin>"
crt.Screen.Send "sum" & chr(13)
crt.Screen.WaitForString "admin>"
crt.Screen.Send "quit" & chr(13)
crt.Session.Disconnect
crt.session.log False

next
Reply With Quote
  #4  
Old 05-31-2017, 11:06 AM
ekoranyi ekoranyi is offline
VanDyke Technical Support
 
Join Date: Jan 2017
Posts: 654
Hi respite,

I found a few timing issues but with a little modification it looks like you may have a solution. With the modification I've made does it run as expected?

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

Sub Main()

crt.screen.synchronous = true
on error resume next

Dim vHosts(100)
vHosts(0) = "172.26.25.140"
vHosts(1) = "172.26.37.140"
vHosts(2) = "172.26.24.140"
vHosts(3) = "172.26.94.140"
vHosts(4) = "172.26.203.140"
vHosts(5) = "172.26.100.140"
vHosts(6) = "172.26.103.140"
vHosts(7) = "172.26.56.140"
vHosts(8) = "172.26.132.140"
vHosts(9) = "172.26.60.140"
vHosts(10) = ""

For Each strHost In vHosts

If strHost = "" Then 
	Exit Sub
End If

' Make sure we are disconnected before attempting a connection
If crt.Session.Connected Then crt.Session.Disconnect

' Connect to the next host
crt.Session.Connect "/SSH2 /ACCEPTHOSTKEYS /L ******* /PASSWORD ******* " & strHost 

crt.Screen.WaitForString "admin>"
crt.Screen.Send "sum" & chr(13)

Set g_fso = CreateObject("Scripting.FileSystemObject")
Set objFile = g_fso.OpenTextFile("C:\Users\nmorra\Documents\Scripts\logs\AP_Scan_Type_Log.txt", 8, True)

crt.screen.WaitForString("AP-")
strText = crt.screen.ReadString(vbcrlf)

crt.screen.WaitForString("LAN1 IP: ")
strIP = crt.screen.ReadString(vbcrlf)

objFile.Write strIP & " has model AP-" & strText & vbcrlf
objFile.Close

crt.Screen.WaitForString "admin>"
crt.Screen.Send "quit" & chr(13)
crt.Session.Disconnect

Next 

End Sub
__________________
Thanks,
--Eric

VanDyke Software
Technical Support
support@vandyke.com
(505) 332-5730
Reply With Quote
  #5  
Old 05-31-2017, 12:45 PM
respite respite is offline
Registered User
 
Join Date: May 2017
Posts: 6
Eric, my man!

Works perfect! Thank you so much.

I have one last question.

I have a line that says "on error resume next"
I'm looking for a way to continue on to the next host but to write a log of failures with which host failed.
This way I can look at them individually.

Is that easy, or?

Thanks again!
Reply With Quote
  #6  
Old 06-01-2017, 09:03 AM
ekoranyi ekoranyi is offline
VanDyke Technical Support
 
Join Date: Jan 2017
Posts: 654
Hi respite,

Glad to hear that is working for you so far.

Our scripting manual covers some of the considerations when trying to handle errors. I suggest reviewing the "helper functions".

I believe the best way of handling errors would require modifying the code quite a bit.

If you have any specific questions please feel free to let me know.
__________________
Thanks,
--Eric

VanDyke Software
Technical Support
support@vandyke.com
(505) 332-5730
Reply With Quote
  #7  
Old 06-01-2017, 01:21 PM
respite respite is offline
Registered User
 
Join Date: May 2017
Posts: 6
Thanks I got my error logging working!
Reply With Quote
  #8  
Old 06-01-2017, 02:11 PM
ekoranyi ekoranyi is offline
VanDyke Technical Support
 
Join Date: Jan 2017
Posts: 654
respite,

Glad to hear it.

Have a great rest of the day.
__________________
Thanks,
--Eric

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

Tags
log file , output , ssh


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 10:53 AM.