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 09-01-2020, 10:11 AM
knightmare knightmare is offline
Registered User
 
Join Date: Sep 2020
Posts: 2
Script to execute a command based on ping result

Hi,

I'm new here and I have no experience with programming.
I'm trying to create my first script to be used with switch/routers.

This script should connect to the device (router/switch), ping an IP address and depending on the result/reply, it execute a command.

Example:
> If ping fails (100% packet loss) = execute command "Y" (in the script, it sends a command to "reset slot 5").
> If ping is successful (0% packet loss) = execute command "X" and exit.

For now, I tried this script below. It worked when the ping fails. When the string "100% packet loss" is matched, it executes the reset.

I suppose I have to make a IF/THEN condition to first to check the ping result and then it decides to run command X or command Y. How can I implement that?

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

crt.Screen.Synchronous = True

Sub Main
	crt.Screen.WaitForString "name:"
	crt.Screen.Send "username" & vbcr
	crt.Screen.WaitForString "password:"
	crt.Screen.Send "password" & vbcr
	crt.Screen.WaitForString ">"
	crt.Screen.Send "enable" & vbcr
	crt.Screen.WaitForString "#"
	crt.Screen.Send "show date" & vbcr
	crt.Screen.WaitForString "#"
	crt.Screen.Send "ping 10.0.0.1" & vbcr
	crt.Screen.WaitForString "100% packet loss"
	crt.Screen.WaitForString "#"
	crt.Screen.Send "reset slot 5" & vbcr
	crt.Screen.WaitForString "confirm (y) or (n)"
	crt.Screen.Send "y" & vbcr
	crt.Screen.WaitForString "#"
	crt.Screen.Send "show date" & vbcr
End Sub
My SecureCRT is version 8.7.0

Thanks

Last edited by cboyack; 09-01-2020 at 10:19 AM. Reason: Please use [CODE] and [/CODE] tags to denote code
Reply With Quote
  #2  
Old 09-01-2020, 10:43 AM
bgagnon bgagnon is offline
VanDyke Technical Support
 
Join Date: Oct 2008
Posts: 4,449
Hi Knightmare,

You would want to use ReadString() instead of WaitForString() following sending the ping command so you actually capture the output so you can check it for the expected phrase.
Code:
crt.Screen.Send "ping 10.0.0.1" & vbcr
strResult = crt.Screen.ReadString("#")

If InStr(strResult, "100% packet loss") Then
    ' Run Command X
Else
    ' Run Command Y
End If
If your browser supports the site command (I know Edge does), you might run that with the search term ping because I think there are a few examples of this sprinkled throughout the forums (ie: site:forums.vandyke.com ping).
__________________
Thanks,
--Brenda

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

Last edited by bgagnon; 09-01-2020 at 10:46 AM. Reason: Added additional info
Reply With Quote
  #3  
Old 09-01-2020, 12:43 PM
knightmare knightmare is offline
Registered User
 
Join Date: Sep 2020
Posts: 2
Thank you very much Brenda.

It worked perfectly !

Best regards
Reply With Quote
  #4  
Old 09-01-2020, 01:29 PM
bgagnon bgagnon is offline
VanDyke Technical Support
 
Join Date: Oct 2008
Posts: 4,449
Hi knightmare,

You are quite welcome. I am glad to hear it worked well for you.
__________________
Thanks,
--Brenda

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

Tags
ping , result


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:32 PM.