VanDyke Software Forums

VanDyke Software Forums (https://forums.vandyke.com/index.php)
-   Scripting (https://forums.vandyke.com/forumdisplay.php?f=14)
-   -   Run commands on list of IPs (https://forums.vandyke.com/showthread.php?t=3235)

nxg 10-10-2008 09:16 AM

Run commands on list of IPs
 
Hi,

I've been looking through examples and the responses to some other user's questions but being VBS blind haven't been able to get anything working.

I need to run 5 commands on each of 250 devices. I'd like to put the commands in the script and have the devices sourced from a text file that contains the list of IP addresses. I need to connect by SSH2, accept the key if required and enter a username and password, then run a series of commands. It would be nice but not essential to log the results.

Another option with the device/command combo is to have the IP address and command on the same line, comma (or other) delimited.

Any help much appreciated!

kbarnette 10-10-2008 09:49 AM

1 Attachment(s)
Hi nxg,

Could you post your script as you have it so far?

Along with your script, could you provide details as to where/how the script is failing?

Also, although you may have already reviewed this example script, I have attached an example which demonstrates the process of reading login information from a file.

Is this example script helpful?

nxg 10-10-2008 10:03 AM

Thanks for the quick reply. I tried altering a script I found on the forum to do what I required but it failed at the first line (not including the VBS and ver statement) when it couldn't find the file! I had to give up as like I said, I have no idea about VBS.

Are there any examples available that show reading from a txt file list, logging in over SSH2, sending a command etc?

kbarnette 10-10-2008 10:25 AM

Hi nxg,

We do not have an example script that accomplishes exactly what it is you are looking to do.

Since you found an example that does what you require, perhaps I can provide information about that script to help you modify the script to fit your needs.

Can you post the script that demonstrates what it is you are are trying to accomplish?

nxg 10-10-2008 10:50 AM

I get a Connection Failed at line 32 message appear after a moment, though it does open a window... The sessionlist.txt file just contains one IP address on the first line at the moment.

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

Sub Main

Const username = "xxxxx" ' Username to use for login
Const password = "xxxxx" ' Password for corresponding user

Const DEVICE_FILE_PATH = "C:\Program Files\VanDyke Software\SecureCRT\Scripts\sessionlist.txt"

Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")

Dim fil
Set fil = fso.OpenTextFile(DEVICE_FILE_PATH)

Dim ip
Dim cnxnString

'On Error Resume Next

While Not fil.AtEndOfStream

ip = fil.ReadLine

cnxnString = "/SSH2 /L " & username & " /PASSWORD " & password & ip & " /AcceptHostKeys"

' Connect

crt.Screen.Synchronous = True

crt.Session.Connect cnxnString

crt.Screen.WaitForString "#"

crt.Screen.Send "dir " & vbCr

crt.Screen.WaitForString "#"

crt.Screen.Synchronous = False

crt.Session.Disconnect

Wend

fil.Close

End Sub

kbarnette 10-10-2008 11:42 AM

Hi nxg,

Is the connection failed dialog indicating the correct IP address?

Also, can you verify that you can connect to this IP address manually from SecureCRT?
This test can be performed by select 'Quick Connect' from the SecureCRT 'File' menu, entering the IP address and clicking the 'Connect' button.

nxg 10-13-2008 04:05 AM

The tab says "PS; 10.250.86.11" which is the correct IP address. I can connect to this host fine using Quick Connect (with the same credentials).

The error says:
CRT Scripting Runtime Error
Error: Connection Failed
File: [PATH]\test2.vbs
Line: 32

Line 32 is: crt.Session.Connect cnxnString

and cnxnString is defined as: cnxnString = "/SSH2 /L " & username & " /PASSWORD " & password & ip & " /AcceptHostKeys"

:confused:

kbarnette 10-13-2008 09:36 AM

Hi nxg,

I have not been able to reproduce the behavior you have described.

Could you enable Trace Options output in the default session, run the script again, and send me the entire output of the connection attempt?

Steps to enable Trace Options output in the default session are below:
1. Browse to the SecureCRT Config folder
The SecureCRT Config folder location is found in the 'General/Configuration folder' field of the Global Options dialog
2. Close all instances of SecureCRT

3. Edit a line in the Session/Default.ini file to reflect the line below:
D:"Trace Level"=00000005
4. Save and close the Default.ini file

nxg 10-13-2008 10:55 AM

Hi,

I tried that, but don't get any output.

kbarnette 10-13-2008 11:04 AM

Hi nxg,

I am curious about the 'PS;' characters that you reported as showing up in the tab caption.

Can you add the following line after you execute the ip=fil.ReadLine method, run your script again, and send me the results of the MsgBox?

Code:

MsgBox "IP address is: " & ip

nxg 10-13-2008 11:26 AM

I added it as shown below, but it still fails at the same place and doesn't popup a message box.


ip = fil.ReadLine
MsgBox "IP address is: " & ip
cnxnString = "/SSH2 /L & /AcceptHostKeys " & username & " /PASSWORD " & password & ip

' Connect

crt.Screen.Synchronous = True
'31
crt.Session.Connect cnxnString

crt.Screen.WaitForString "#"

kbarnette 10-13-2008 11:42 AM

Hi nxg,

It seems strange that the script is not displaying the MsgBox but is making it to the connection call.

Can you verify that the MsgBox line was added and saved into your script?

Also, can you post your complete modified script, and provide the exact error message you are seeing?

nxg 10-14-2008 02:47 AM

1 Attachment(s)
OK, here's the script (username and password obscured, but have been checked as correct). I've re-installed the program from scratch, having cleared out all files and registry entries from the previous install. A screenshot of the error message is attached.

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

Sub Main

Const username = "xxxxxx" ' Username to use for login
Const password = "xxxxxx" ' Password for corresponding user

Const DEVICE_FILE_PATH = "C:\Program Files\SecureCRT\Scripts\sessionlist.txt"
'10
Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")

Dim fil
Set fil = fso.OpenTextFile(DEVICE_FILE_PATH)

Dim ip
Dim cnxnString

'On Error Resume Next

While Not fil.AtEndOfStream

ip = fil.ReadLine
MsgBox "IP address is: " & ip
cnxnString = "/SSH2 /L & /AcceptHostKeys " & username & " /PASSWORD " & password & ip

' Connect

crt.Screen.Synchronous = True
' 31
crt.Session.Connect cnxnString

crt.Screen.WaitForString "#"

crt.Screen.Send "dir " & vbCr

crt.Screen.WaitForString "#"

crt.Screen.Synchronous = False

crt.Session.Disconnect

Wend

fil.Close

End Sub

nxg 10-14-2008 05:11 AM

Hi,

You'll be glad to know I fixed it! :) As I've seen a lot of 'it's fixed' replies in the forum without people actually saying what they've done (which can be really frustrating to newbies with the same problem), I'll post what was wrong.

1. My sessionlist.txt file had ';' delimited site name and IP in it, which is where the 'PS' was coming from. The txt file now just has an IP in it.

2. The cnxnString was wrong. The correct one is in the working script below.

This now connects to the IP address, sends the 'dir' command then disconnects. Not very useful in itself but is the proof of concept I needed to sequentially send multiple commands to multiple devices, while automatically accepting SSH keys if required.

Thanks for your help, and I hope this comes in useful for other 'newbies'.

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

Sub Main

Const username = "xxxxx" ' Username to use for login
Const password = "xxxxx" ' Password for corresponding user

Const DEVICE_FILE_PATH = "C:\Program Files\SecureCRT\Scripts\sessionlist.txt"

Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")

Dim fil
Set fil = fso.OpenTextFile(DEVICE_FILE_PATH)

Dim ip
Dim cnxnString

'On Error Resume Next

While Not fil.AtEndOfStream

ip = fil.ReadLine

cnxnString = "/SSH2 /L " & username & " /PASSWORD " & password & " /C 3DES /M MD5 " & ip & " /AcceptHostKeys"

' Connect

crt.Screen.Synchronous = True

crt.Session.Connect cnxnString

crt.Screen.WaitForString "#"

crt.Screen.Send "dir " & vbCr

crt.Screen.WaitForString "#"

crt.Screen.Synchronous = False

crt.Session.Disconnect

Wend

fil.Close

End Sub

kbarnette 10-14-2008 08:21 AM

Hi nxg,

I am glad to hear the issue is resolved.

Thanks for posting the resolution.


All times are GMT -6. The time now is 01:53 AM.