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 11-19-2020, 10:15 AM
mohenoun mohenoun is offline
Registered User
 
Join Date: Nov 2020
Posts: 21
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.

Last edited by bgagnon; 12-01-2020 at 11:55 AM. Reason: Added reference to script in another post.
Reply With Quote
  #2  
Old 11-19-2020, 10:46 AM
bgagnon bgagnon is offline
VanDyke Technical Support
 
Join Date: Oct 2008
Posts: 4,495
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.
__________________
Thanks,
--Brenda

VanDyke Software
Technical Support
support@vandyke.com
(505) 332-5730
Reply With Quote
  #3  
Old 11-20-2020, 09:39 AM
mohenoun mohenoun is offline
Registered User
 
Join Date: Nov 2020
Posts: 21
Quote:
Originally Posted by bgagnon View Post
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.
Reply With Quote
  #4  
Old 11-20-2020, 10:10 AM
bgagnon bgagnon is offline
VanDyke Technical Support
 
Join Date: Oct 2008
Posts: 4,495
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.
__________________
Thanks,
--Brenda

VanDyke Software
Technical Support
support@vandyke.com
(505) 332-5730
Reply With Quote
  #5  
Old 11-20-2020, 11:36 AM
mohenoun mohenoun is offline
Registered User
 
Join Date: Nov 2020
Posts: 21
I canít forward

Quote:
Originally Posted by bgagnon View Post
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.
Reply With Quote
  #6  
Old 11-20-2020, 12:41 PM
mohenoun mohenoun is offline
Registered User
 
Join Date: Nov 2020
Posts: 21
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.
Reply With Quote
  #7  
Old 11-20-2020, 12:53 PM
bgagnon bgagnon is offline
VanDyke Technical Support
 
Join Date: Oct 2008
Posts: 4,495
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.
__________________
Thanks,
--Brenda

VanDyke Software
Technical Support
support@vandyke.com
(505) 332-5730
Reply With Quote
  #8  
Old 11-20-2020, 01:32 PM
mohenoun mohenoun is offline
Registered User
 
Join Date: Nov 2020
Posts: 21
Quote:
Originally Posted by bgagnon View Post
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#

Last edited by mohenoun; 11-20-2020 at 01:39 PM.
Reply With Quote
  #9  
Old 11-20-2020, 01:49 PM
mohenoun mohenoun is offline
Registered User
 
Join Date: Nov 2020
Posts: 21
Quote:
Originally Posted by mohenoun View Post
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
Reply With Quote
  #10  
Old 11-20-2020, 01:52 PM
mohenoun mohenoun is offline
Registered User
 
Join Date: Nov 2020
Posts: 21
Quote:
Originally Posted by mohenoun View Post
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 ?
Reply With Quote
  #11  
Old 11-20-2020, 02:52 PM
bgagnon bgagnon is offline
VanDyke Technical Support
 
Join Date: Oct 2008
Posts: 4,495
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,
--Brenda

VanDyke Software
Technical Support
support@vandyke.com
(505) 332-5730
Reply With Quote
  #12  
Old 11-20-2020, 03:12 PM
mohenoun mohenoun is offline
Registered User
 
Join Date: Nov 2020
Posts: 21
Quote:
Originally Posted by bgagnon View Post
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
Reply With Quote
  #13  
Old 11-20-2020, 03:14 PM
mohenoun mohenoun is offline
Registered User
 
Join Date: Nov 2020
Posts: 21
Quote:
Originally Posted by mohenoun View Post
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.
Reply With Quote
  #14  
Old 11-20-2020, 03:40 PM
bgagnon bgagnon is offline
VanDyke Technical Support
 
Join Date: Oct 2008
Posts: 4,495
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.
__________________
Thanks,
--Brenda

VanDyke Software
Technical Support
support@vandyke.com
(505) 332-5730
Reply With Quote
  #15  
Old 11-23-2020, 01:59 AM
mohenoun mohenoun is offline
Registered User
 
Join Date: Nov 2020
Posts: 21
Thanks :)

Quote:
Originally Posted by bgagnon View Post
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
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:45 PM.