VanDyke Software Forums

VanDyke Software Forums (https://forums.vandyke.com/index.php)
-   Scripting (https://forums.vandyke.com/forumdisplay.php?f=14)
-   -   Trying to tweak the script to no avail (https://forums.vandyke.com/showthread.php?t=14350)

mohenoun 11-19-2020 10:15 AM

Trying to tweak the script to no avail
 
Hello,

I'm really new to scripting.

I have to audit several equipment by sending a command line.

However, I'm not able to use the script.

Forum Moderator:
[Referenced script is here.]

To connect to an equipment I need to connect to a rebound server which I can easly do if I use :

def main():
crt.Session.Connect("/s name of the session")

main()


Then I have to connect to the quipment using the specific command :

ssh -l <Myusername> <IPaddress>

then I get prompted to enter a password.

I tried to tweak the script so that I could use it with this constraints, however it doesn't work.

I was wondering if a charitable soul could help me.

Thank you in advance.

bgagnon 11-19-2020 10:46 AM

Hi mohenoun,

First, please clarify if by "the script", you were referencing sranbala's posted script here?

Or VanDyke's Read Data example script that was referenced in the thread?

By "rebound server", it sounds like you are going through a jump host. If the jump host is capable of port forwarding, I would recommend you make the connection using SecureCRT's dependent session feature.

mohenoun 11-20-2020 09:39 AM

Quote:

Originally Posted by bgagnon (Post 54325)
Hi mohenoun,

First, please clarify if by "the script", you were referencing sranbala's posted script here?

Or VanDyke's Read Data example script that was referenced in the thread?

By "rebound server", it sounds like you are going through a jump host. If the jump host is capable of port forwarding, I would recommend you make the connection using SecureCRT's dependent session feature.

Hello,

Sorry my question wasn't clear.

I'll try to structure it more :)

Context :
=========

I have to do an audit in my company for several cisco equipments, my idea was to launch a script in order to be able to do it automatically while going through a list of Ips vs commands.

Constraint :
------------

To connect to equipment I need to connect to a rebound equipment. Basically if I wanted to connect to equipment A, in scripting it would be :

crt.Session.Connect("/s rebound.win")
crt.Screen.Send "ssh -l userid " & equipment A & chr(13)
crt.Screen.WaitForString "Password:"
crt.Screen.Send "Mypassword" & chr(13)
crt.Screen.WaitForString "#"
crt.Screen.Send chr(13)
crt.Screen.Send "show ip bgp vpnv4 all summ | inc Idle" & chr(13)
crt.Screen.Send "exit" & chr(13)

Then connect again with
crt.Screen.Send "ssh -l userid " & equipment B & chr(13)
crt.Screen.WaitForString "Password:"
crt.Screen.Send "Mypassword" & chr(13)
crt.Screen.WaitForString "#"
crt.Screen.Send chr(13)
crt.Screen.Send "show ip bgp vpnv4 all summ | inc Idle" & chr(13)
crt.Screen.Send "exit" & chr(13)

If I keep my session in the rebound.

If I open in different tabs I just use :

crt.Session.Connect("/s rebound.win")
crt.Screen.Send "ssh -l userid " & equipment A & chr(13)
crt.Screen.WaitForString "Password:"
crt.Screen.Send "Mypassword" & chr(13)
crt.Screen.WaitForString "#"
crt.Screen.Send chr(13)
crt.Screen.Send "show ip bgp vpnv4 all summ | inc Idle" & chr(13)
crt.Screen.Send "exit" & chr(13)

And I log the session.

This is how I'm doing it right now archaically ! Because my coding is limited.

Script:
========

I found this on the website :https://forums.vandyke.com/showpost....94&postcount=1

RunCommandsOnMultipleHostsAndLogResults.vbs.txt

Need:
=====

I want to modify the script so that it connects the way I need to connect and the runs the commands using the hosts and commands files.


I hope it is clear :)

Thank you.

bgagnon 11-20-2020 10:10 AM

Hi mohenoun,

Did you read the information about dependent session?

Are you unable (jump server does not support port forwarding) to use this feature? Or just unwilling to try it? :)

It's going to simplify things before we get into your actual script, which, by the way, includes multiple Send() calls without an intervening WaitForString().

This goes against the "best practice" outlined in the scripting manual (section 4.2) that there should be a WaitForString() following each Send() command.

mohenoun 11-20-2020 11:36 AM

I canít forward
 
Quote:

Originally Posted by bgagnon (Post 54329)
Hi mohenoun,

Did you read the information about dependent session?

Are you unable (jump server does not support port forwarding) to use this feature? Or just unwilling to try it? :)

It's going to simplify things before we get into your actual script, which, by the way, includes multiple Send() calls without an intervening WaitForString().

Hello Brenda,

This goes against the "best practice" outlined in the scripting manual (section 4.2) that there should be a WaitForString() following each Send() command.

I canít port forward unfortunatelly. I work for a bank and Iím quite limited in my options.

I just put forward the script as an exemple, Iím really new to this. Thus I wanted to use a script that was following best practices. But I got lost in the coding and couldnít figure out what to change and where and how to do it in a clean way.

Thank you for your help.

mohenoun 11-20-2020 12:41 PM

Actually, I just want to know what to change in https://forums.vandyke.com/showpost....94&postcount=1

Since my jump server doesn't seem to allow port forwarding.

bgagnon 11-20-2020 12:53 PM

Hi mohenoun,

For your objective, I think you will be better off using this jump host script example.

Give it a shot and let me know what obstacles you run into.

mohenoun 11-20-2020 01:32 PM

Quote:

Originally Posted by bgagnon (Post 54332)
Hi mohenoun,

For your objective, I think you will be better off using this jump host script example.

Give it a shot and let me know what obstacles you run into.

EDIT:

I changed the ssh connection line to :
strCommand = "ssh -l d91910 " & vHosts(nIndex)
crt.Screen.Send strCommand & vbcr

I'm getting :



Jumpserver#ssh -l d91910 PE2
Password:

Enter Old Password:

Password:
[Connection to PE2 aborted: error status 0]
jumpserver#
jumpserver#
jumpserver#

mohenoun 11-20-2020 01:49 PM

Quote:

Originally Posted by mohenoun (Post 54333)
EDIT:

I changed the ssh connection line to :
strCommand = "ssh -l d91910 " & vHosts(nIndex)
crt.Screen.Send strCommand & vbcr

I'm getting :



Jumpserver#ssh -l d91910 PE2
Password:

Enter Old Password:

Password:
[Connection to PE2 aborted: error status 0]
jumpserver#
jumpserver#
jumpserver#

after editing the password string to my password I get

PE2#

But it doesn't go to the following device, I tried changing $ to # as a successful indicator

mohenoun 11-20-2020 01:52 PM

Quote:

Originally Posted by mohenoun (Post 54334)
after editing the password string to my password I get

PE2#

But it doesn't go to the following device, I tried changing $ to # as a successful indicator

*

It's working !

Is there a way to make it save results in separate files ?

bgagnon 11-20-2020 02:52 PM

Hi mohenoun,

I am glad to hear of your progress.

So is logging configured in your rebound server session?

If so, I would suggest you just set a log file name that includes the host as part of it and stop/start logging within the Do Loop/Select Case construct, before sending commands. Maybe something like this:

Code:

crt.Session.LogFileName = "C:\Logs\" & vHosts(nIndex) & "-%M%D%y.txt"
crt.Session.Log True
crt.Screen.Send "Command1" & vbcr
crt.Screen.WaitForString "$"
crt.Session.Log False

The only downside to that script is the username/password for that ssh connection will be exposed in it. If there's any way at all for you to setup public-key auth, that would be more secure. :)

mohenoun 11-20-2020 03:12 PM

Quote:

Originally Posted by bgagnon (Post 54336)
Hi mohenoun,

I am glad to hear of your progress.

So is logging configured in your rebound server session?

If so, I would suggest you just set a log file name that includes the host as part of it and stop/start logging within the Do Loop/Select Case construct, before sending commands. Maybe something like this:

Code:

crt.Session.LogFileName = "C:\Logs\" & vHosts(nIndex) & "-%M%D%y.txt"
crt.Session.Log True
crt.Screen.Send "Command1" & vbcr
crt.Screen.WaitForString "$"
crt.Session.Log False

The only downside to that script is the username/password for that ssh connection will be exposed in it. If there's any way at all for you to setup public-key auth, that would be more secure. :)

Thanks a lot :)

The computer is literally a black box so it's OK without AUTH key :)

I can't configure it anyway.

I added crt.Session.Connect("/s jumpstartserver")
crt.Screen.WaitForString "Jumpstartserver#"

And it's working wonders.

The only thing that I can't seem to figure out how to do, is to log a seperate file for each machine I connect to in order to use them afterward in excel.

Any example on a code I could add to log just the results of my commands in files named according to the host list ?

thanks :)

mohenoun 11-20-2020 03:14 PM

Quote:

Originally Posted by mohenoun (Post 54337)
Thanks a lot :)

The computer is literally a black box so it's OK without AUTH key :)

I can't configure it anyway.

I added crt.Session.Connect("/s jumpstartserver")
crt.Screen.WaitForString "Jumpstartserver#"

And it's working wonders.

The only thing that I can't seem to figure out how to do, is to log a seperate file for each machine I connect to in order to use them afterward in excel.

Any example on a code I could add to log just the results of my commands in files named according to the host list ?

thanks :)

it would alos be nice to figure out a way to use an external list in a txt file so that I modify the txt file only and not the script each time for the hosts list and the commands.

bgagnon 11-20-2020 03:40 PM

Hi mohenoun,

I provided an example for logging.

If I have any suggestion for reading a file, it won't be until Monday at the earliest. If feeling ambitious, you might just look at how it was done in the RunCommands example and try to add that code to this script. :)

mohenoun 11-23-2020 01:59 AM

Thanks :)
 
Quote:

Originally Posted by bgagnon (Post 54339)
Hi mohenoun,

I provided an example for logging.

If I have any suggestion for reading a file, it won't be until Monday at the earliest. If feeling ambitious, you might just look at how it was done in the RunCommands example and try to add that code to this script. :)

thank you :)

I'll try it today


All times are GMT -6. The time now is 09:36 PM.