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-21-2018, 07:16 AM
iCalid iCalid is offline
Registered User
 
Join Date: Dec 2018
Posts: 6
read output from host, change it and send it back to the host

hello,

I'm fairly new to scripting, I have created a script to run some commands in a router. However, based on the output, I want to send a string to the router.

example:
send string "crt.Screen.Send": show run interface gi1/1
and based on this out put it should print out data which one of them will be something like this:
bundle id 10 mode active

what I want to do is print "no" at the beginning of this output and send string back to the router
send string: no bundle id 10 mode active

in other words:
look for key work "bundle"
copy the whole line
print no then past the line

basically to anyone with networking experience, I would like to remove an interface from an aggregate interface.


I apologies in advanced if this question been answered.
Reply With Quote
  #2  
Old 12-21-2018, 08:32 AM
bgagnon bgagnon is offline
VanDyke Technical Support
 
Join Date: Oct 2008
Posts: 3,652
Hi iCalid,

Is the resulting output only the output you need to prepend "no" to? (Or will there be additional output?)

And afterwards are you returned to some known, static prompt?

If so, I think the easiest way to handle this is to use ReadString() after running your command to save that output to a variable. Then use Send() to prepend "no" to the strOutput variable:
Code:
' There are methods illustrated elsewhere to capture this, if desired, or if it's a known static prompt, it can be hard-coded
strPrompt = "MyShellPrompt"
...
crt.Screen.Send "show run interface gi1/1" & vbcr
' Wait for the command to be echoed back
crt.Screen.WaitForString "show run interface gi1/1" & vbcr
' Use the ReadString() method to get the resulting output
strOutput = crt.Screen.ReadString(strPrompt)
crt.Screen.Send "no " & strOutput & vbcr
There's a scripting guide on our website here. It's written about VBScript, but many of the concepts can be applied to any scripting language.
__________________
Thanks,
--Brenda

VanDyke Software
Technical Support
support@vandyke.com
(505) 332-5730
Reply With Quote
  #3  
Old 12-22-2018, 07:45 AM
iCalid iCalid is offline
Registered User
 
Join Date: Dec 2018
Posts: 6
I believe if I'm reading this correctly, this will save all the output into a variable, then send no before it.
However, there will be multiple output when running the " show run interface <type/number>

so to make this clear, I'll show you how would I do it without a script, and the script that I have

manual configuration without a script:
RP/0/RP0/CPU0:router#show run int TenGigE0/1/0/24
Wed Sep 12 08:52:59.571 EDT
interface TenGigE0/1/0/24
description tge to router tge1/3/0/6
bundle id 2 mode active
lacp period short
load-interval 30
!
RP/0/RP0/CPU0:router#configure exclusive
Wed Sep 12 08:56:42.323 EDT
RP/0/RP0/CPU0:router(config)#no interface TenGigE0/1/0/24 bundle id 2
RP/0/RP0/CPU0:router(config)#commit
Wed Sep 12 08:56:43.287 EDT
RP/0/RP0/CPU0:router(config)#end
RP/0/RP0/CPU0:router#


the script that I wrote:
crt.Screen.Send "show run interface TenGigE0/1/0/24" & vbCr
crt.Screen.WaitForString "#"
crt.Screen.Send "configure exclusive" & vbCr
crt.Screen.WaitForString "config"
crt.Screen.Send "no interface TenGigE0/1/0/24 bundle id 2" & vbCr
crt.Screen.WaitForString "config"
crt.Screen.Send "commit" & vbCr
crt.Screen.WaitForString "config"
crt.Screen.Send "end" & vbCr
End Sub

the issue is that bundle id <number> the number is variable and depends from an interface to interface, that is why I need to make the ReadString only reads that line of output
"bundle id 2 mode active"
Reply With Quote
  #4  
Old 12-24-2018, 11:14 AM
bgagnon bgagnon is offline
VanDyke Technical Support
 
Join Date: Oct 2008
Posts: 3,652
Hi iCalid,

If you have multiple lines of output, but desire to just get at a specific line then you will have to parse the data to attain the info you need. This is fairly easy in VBScript with InStr and Left/Mid/Right.

There's a section on this (Extracting Specific Information) in Chapter 4.3 of the Scripting Essentials manual on our website here.

Is the bundle id number only one digit? Sometimes two? In other words, some known length, or completely dynamic?

What you will need to do (in pseudo-code) is:
  • For each line in my ReadString() array
  • Check if "bundle id" is found in the line
  • If so, parse out bundle id ###
  • Use that info to build next command
__________________
Thanks,
--Brenda

VanDyke Software
Technical Support
support@vandyke.com
(505) 332-5730
Reply With Quote
  #5  
Old 12-25-2018, 05:10 AM
iCalid iCalid is offline
Registered User
 
Join Date: Dec 2018
Posts: 6
the bundle id is a unknown number and it is variable, it can be one, two, three or four digits (range from 1 to 4096)

what you mentioned here would work:
What you will need to do (in pseudo-code) is:
For each line in my ReadString() array
Check if "bundle id" is found in the line
If so, parse out bundle id ###
Use that info to build next command

However, I'm not familiar with pseudo-code.
can you point me to the right direction or post the syntax that I can use to create that For loop

I appreciate your help
Reply With Quote
  #6  
Old 12-25-2018, 11:21 AM
iCalid iCalid is offline
Registered User
 
Join Date: Dec 2018
Posts: 6
to make it easier, the router will accept either "no bundle id" or "no bundle id <number> mode active"

So all I want is, once the out put is saved to the variable strPrompt

crt.Screen.Send "show run interface gi1/1" & vbcr
crt.Screen.WaitForString "show run interface gi1/1" & vbcr
strOutput = crt.Screen.ReadString(strPrompt)

is to search within that "strPrompt" for word bundle
if the word bundle exists 'if strPrompt.contains("bundle") something like that, I'm not sure of the syntax

then
crt.Screen.Send "no bundle id" & vbcr
else
end 'or print a print message box, this part I can do

thanks
Reply With Quote
  #7  
Old 12-26-2018, 10:52 AM
bgagnon bgagnon is offline
VanDyke Technical Support
 
Join Date: Oct 2008
Posts: 3,652
Hi iCalid,
Quote:
to make it easier, the router will accept either "no bundle id" or "no bundle id <number> mode active"
Great! If you don't need the exact number then you won't have to get into more complicated concepts such as regular expression.

So, the code would simply be:
Code:
If InStr(strOutput, "bundle id") > 0 Then
    crt.Screen.Send "no bundle id" & vbcr
Else 
    end 'or print a print message box, this part I can do
End If
__________________
Thanks,
--Brenda

VanDyke Software
Technical Support
support@vandyke.com
(505) 332-5730
Reply With Quote
  #8  
Old 12-26-2018, 02:24 PM
iCalid iCalid is offline
Registered User
 
Join Date: Dec 2018
Posts: 6
I did that. However, it didn't work, it went a head to the else if statement and sent a msg box. I'm not sure what I'm missing

here is the code that I have:
PS: channel-group is the same as bundle id, it depends on the software that I'm running on the router.


Sub Main
crt.Screen.Send "show run interface FastEthernet2/0/1" & vbcr
crt.Screen.WaitForString "#"
If InStr(strOutput, "channel-group") > 0 Then
crt.Screen.Send "configure t" & vbCr
crt.Screen.WaitForString "config"
crt.Screen.Send "interface FastEthernet2/0/1" & vbCr
crt.Screen.WaitForString "config"
crt.Screen.Send "no channel group 2" & vbCr
crt.Screen.WaitForString "config"
crt.Screen.Send "end" & vbCr
crt.Screen.WaitForString "#"
crt.Screen.Send "end" & vbCr
Else
MsgBox "no bundle found"
End if

End Sub
Reply With Quote
  #9  
Old 12-26-2018, 03:04 PM
bgagnon bgagnon is offline
VanDyke Technical Support
 
Join Date: Oct 2008
Posts: 3,652
Hi iCalid,

You have not used the ReadString() method to capture the output into the strOutput variable:
Code:
Sub Main
  crt.Screen.Send "show run interface FastEthernet2/0/1" & vbcr
  strOutput = crt.Screen.ReadString("#")
 If InStr(strOutput, "channel-group") > 0 Then
...
__________________
Thanks,
--Brenda

VanDyke Software
Technical Support
support@vandyke.com
(505) 332-5730
Reply With Quote
  #10  
Old 12-26-2018, 03:46 PM
iCalid iCalid is offline
Registered User
 
Join Date: Dec 2018
Posts: 6
Brenda,

that one worked, thank you so much for your help
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 07:14 PM.