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 07-13-2017, 12:38 AM
jatcase jatcase is offline
Registered User
 
Join Date: Jul 2017
Posts: 9
How to read a previous line and apply an if

Hello guys, im really noob in this scripting world but basically i want to read a previous line after applying a show command in a cisco device, if the output is true then do something, if dont then do something else. I tried the following:

screenrow = crt.screen.CurrentRow - 1
Dim result
result = crt.Screen.Get(screenrow, 1, screenrow, 40 )

but is continuing reading the line below of the result of the show command.
Reply With Quote
  #2  
Old 07-13-2017, 09:16 AM
bgagnon bgagnon is offline
VanDyke Technical Support
 
Join Date: Oct 2008
Posts: 3,066
Hi jatcase,

Quote:
... basically i want to read a previous line after applying a show command in a cisco device ...
So are you saying you want to read only the last line of output from the command?

And there is not possibly a blank line at the end of that output?

If you change the line you are capturing, do you get the correct data?
screenrow = crt.screen.CurrentRow - 2
I know with all the devices that may not be the solution, but it's a data point to gather at this stage.
__________________
Thanks,
--Brenda

VanDyke Software
Technical Support
support@vandyke.com
(505) 332-5730
Reply With Quote
  #3  
Old 07-14-2017, 03:42 AM
jatcase jatcase is offline
Registered User
 
Join Date: Jul 2017
Posts: 9
Indeed, i want to do a show host | include "device name" and if i get an output do telnet to that host, else apply a configuration. here is an example of what i want:

do
test-002#show host | include cisco
cisco None (perm, OK) 2 IP 10.0.0.1

here if i get an output do:

telnet cisco

else

test-002(config)#configure t
test-002(config)#ip host cisco 10.0.0.1
test-002(config)#exit
test-002#wr
test-002#telnet cisco

Br
Reply With Quote
  #4  
Old 07-14-2017, 08:37 AM
bgagnon bgagnon is offline
VanDyke Technical Support
 
Join Date: Oct 2008
Posts: 3,066
Hi jatcase,

I don't think I have enough info yet to assist.

Please list the *explicit steps* of how you would accomplish this task manually.

Also, if you already have script code, please post it (with any sensitive data redacted). With respect to your efforts to-date (if applicable), you failed to answer these inquiries:

Quote:
And there is not possibly a blank line at the end of that output?

If you change the line you are capturing, do you get the correct data?
screenrow = crt.screen.CurrentRow - 2
__________________
Thanks,
--Brenda

VanDyke Software
Technical Support
support@vandyke.com
(505) 332-5730
Reply With Quote
  #5  
Old 07-15-2017, 12:51 PM
jatcase jatcase is offline
Registered User
 
Join Date: Jul 2017
Posts: 9
Ok i solve 1 problem, the thing was that i first connect to a router via ssh (using the script) then i did telnet to another router and apply the show host | include, the problem was that there is a delay between the ssh session and the telnet session, so thats why when the script read
screenrow = crt.screen.CurrentRow - 1
it showed me a blank space, i just apply a crt.Screen.WaitForString "linux$", 2 and is working fine.

now i want to do an if statement that if show host | include "device name" return nothing then apply a configuration else do a telnet to that device.

thank for the help
Reply With Quote
  #6  
Old 07-17-2017, 10:04 AM
bgagnon bgagnon is offline
VanDyke Technical Support
 
Join Date: Oct 2008
Posts: 3,066
Hi jatcase,

Ah, so it's a jumphost scenario. So I think what you would want to do is use ReadString() instead.

If "linux$" is the prompt you would return to whether there's output or not:

Code:
strResult = crt.Screen.ReadString("linux$", 2)
Then:
Code:
If strResult = "" Then
    ' Do something
Else
    ' Do something else
End If
__________________
Thanks,
--Brenda

VanDyke Software
Technical Support
support@vandyke.com
(505) 332-5730
Reply With Quote
  #7  
Old 07-19-2017, 05:39 AM
jatcase jatcase is offline
Registered User
 
Join Date: Jul 2017
Posts: 9
i have this script for now, i configure an ip host but in any case (exist or dont) i get the elseif option (strresult <> " " then)

Sub Main

'variables devices/ports'
Dim hl, rglobal, local, puertosl, puertosg, resultadoshow, screenrow, currentrow
'show command'
show = "show host | include "
'variables username/pass'
Dim password
username = "cisco"
crt.Screen.Synchronous = True




'Telnet the device'

crt.Session.Connect "/TELNET 127.0.0.1 5000"
' Wait for the device's login prompt or for 2 seconds whichever comes first.'
crt.Screen.WaitForString "linux$", 2
crt.Screen.Send "telnet 2.2.2.2" & vbCr
crt.Screen.WaitForString "linux$", 2
crt.Screen.Send "cisco" & vbCr
crt.Screen.WaitForString "linux$", 2
crt.Screen.Send "cisco" & vbCr
crt.Screen.WaitForString "linux$", 2
crt.Screen.Send "enable" & vbCr
crt.Screen.WaitForString "linux$", 2
crt.Screen.Send "cisco" & vbCr
crt.Screen.WaitForString "linux$", 2

'name local device'
local = InputBox("Por favor ingresar el nombre del remotizador local")
If local = "" or local = " " or local = "\" then
MsgBox "Nombre no valido, operacion cancelada."
Exit Sub
Elseif local <> "" or local <> " " or local <> "\" then
CreateObject("WScript.Shell").Popup "Procesando "+local, 0.5, "Mensaje"
End If

'lookup local'
'Check show'
crt.Screen.Send show+local & vbCr
strresult = crt.Screen.WaitForString (local, 2)

If strresult = " " then
crt.Screen.Send "conf t" & vbCr
crt.Screen.WaitForString "linux$", 2
Elseif strresult <> " " then
crt.Screen.Send "telnet 3.3.3.3" & vbCr
End if

End Sub
Reply With Quote
  #8  
Old 07-19-2017, 07:47 AM
bgagnon bgagnon is offline
VanDyke Technical Support
 
Join Date: Oct 2008
Posts: 3,066
Hi jatcase,

You have this as a WaitForString():

Code:
strresult = crt.Screen.WaitForString (local, 2)
WaitForString() does not *capture*. You need to use ReadString() to capture data.
__________________
Thanks,
--Brenda

VanDyke Software
Technical Support
support@vandyke.com
(505) 332-5730
Reply With Quote
  #9  
Old 07-19-2017, 08:03 AM
jatcase jatcase is offline
Registered User
 
Join Date: Jul 2017
Posts: 9
i modified the script


strresult = crt.Screen.ReadString (local)

If strresult = " " then
crt.Screen.Send "conf t" & vbCr
crt.Screen.WaitForString "linux$", 2
Elseif strresult <> " " then
crt.Screen.Send "telnet 3.3.3.3" & vbCr
End if

End Sub

but i still get the same result
Reply With Quote
  #10  
Old 07-19-2017, 08:13 AM
bgagnon bgagnon is offline
VanDyke Technical Support
 
Join Date: Oct 2008
Posts: 3,066
Hi jatcase,

Have you "message boxed" strresult to see what it contains?

Code:
crt.Dialog.MessageBox "<" & strresult & ">"
We recommend placing angle brackets around the info so you can clearly see when there are non-printing characters such as tabs, spaces, end of line characters, etc.
__________________
Thanks,
--Brenda

VanDyke Software
Technical Support
support@vandyke.com
(505) 332-5730
Reply With Quote
  #11  
Old 07-19-2017, 09:42 AM
jatcase jatcase is offline
Registered User
 
Join Date: Jul 2017
Posts: 9
i tried it and got the show host | include instead of the value of variable "local".
there is another way to use strresult = crt.Screen.ReadString "variable" to capture the variable value?

best regards.
Reply With Quote
  #12  
Old 07-19-2017, 10:52 AM
bgagnon bgagnon is offline
VanDyke Technical Support
 
Join Date: Oct 2008
Posts: 3,066
Hi jatcase,

We need to back up a minute.

What is the prompt that results after sending the command show host | include "device name" when there is no output?

What is the prompt that results after sending the command show host | include "device name" that does result in output?
__________________
Thanks,
--Brenda

VanDyke Software
Technical Support
support@vandyke.com
(505) 332-5730
Reply With Quote
  #13  
Old 07-19-2017, 01:49 PM
jatcase jatcase is offline
Registered User
 
Join Date: Jul 2017
Posts: 9
hello again, righ now my script looks like this:

crt.Screen.Send show+local & vbCr
strresult = crt.Screen.ReadString (local)
MsgBox strresult
If strresult = " " then
crt.Screen.Send "conf t" & vbCr
crt.Screen.WaitForString "linux$", 2
Elseif strresult <> " " then
crt.Screen.Send "telnet 3.3.3.3" & vbCr
End if

im using the MsgBox strresult to see what is capturing the previous line, im getting "show host | include "....
if the host is found or not is applying the telnet to 3.3.3.3
Reply With Quote
  #14  
Old 07-19-2017, 02:45 PM
bgagnon bgagnon is offline
VanDyke Technical Support
 
Join Date: Oct 2008
Posts: 3,066
Hi jatcase,

Please answer the questions I posed.

If you run show host | include "device name" command manually, what is the specific prompt that is displayed once the command executes? (In other words, how does an "enabled mode prompt" read on this device?)

How does this relate to the local variable which is set according to user input?

Code:
local = InputBox("Por favor ingresar el nombre del remotizador local")
__________________
Thanks,
--Brenda

VanDyke Software
Technical Support
support@vandyke.com
(505) 332-5730
Reply With Quote
  #15  
Old 07-20-2017, 04:58 AM
jatcase jatcase is offline
Registered User
 
Join Date: Jul 2017
Posts: 9
i have the same result in both scenarios

Quote:
Originally Posted by bgagnon View Post
What is the prompt that results after sending the command show host | include "device name" when there is no output?
show host | include is shown

What is the prompt that results after sending the command show host | include "device name" that does result in output?
show host | include is shown
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 02:37 AM.