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 10-10-2008, 09:16 AM
nxg nxg is offline
Registered User
 
Join Date: Sep 2008
Posts: 8
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!
Reply With Quote
  #2  
Old 10-10-2008, 09:49 AM
kbarnette kbarnette is offline
VanDyke Technical Support
 
Join Date: Aug 2007
Posts: 587
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?
Attached Files
File Type: txt GetLoginInfoFromFile.vbs.txt (4.9 KB, 443 views)
Reply With Quote
  #3  
Old 10-10-2008, 10:03 AM
nxg nxg is offline
Registered User
 
Join Date: Sep 2008
Posts: 8
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?
Reply With Quote
  #4  
Old 10-10-2008, 10:25 AM
kbarnette kbarnette is offline
VanDyke Technical Support
 
Join Date: Aug 2007
Posts: 587
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?
Reply With Quote
  #5  
Old 10-10-2008, 10:50 AM
nxg nxg is offline
Registered User
 
Join Date: Sep 2008
Posts: 8
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

Last edited by nxg; 10-10-2008 at 10:52 AM.
Reply With Quote
  #6  
Old 10-10-2008, 11:42 AM
kbarnette kbarnette is offline
VanDyke Technical Support
 
Join Date: Aug 2007
Posts: 587
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.
Reply With Quote
  #7  
Old 10-13-2008, 04:05 AM
nxg nxg is offline
Registered User
 
Join Date: Sep 2008
Posts: 8
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"


Last edited by nxg; 10-13-2008 at 08:52 AM.
Reply With Quote
  #8  
Old 10-13-2008, 09:36 AM
kbarnette kbarnette is offline
VanDyke Technical Support
 
Join Date: Aug 2007
Posts: 587
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
Reply With Quote
  #9  
Old 10-13-2008, 10:55 AM
nxg nxg is offline
Registered User
 
Join Date: Sep 2008
Posts: 8
Hi,

I tried that, but don't get any output.
Reply With Quote
  #10  
Old 10-13-2008, 11:04 AM
kbarnette kbarnette is offline
VanDyke Technical Support
 
Join Date: Aug 2007
Posts: 587
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
Reply With Quote
  #11  
Old 10-13-2008, 11:26 AM
nxg nxg is offline
Registered User
 
Join Date: Sep 2008
Posts: 8
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 "#"
Reply With Quote
  #12  
Old 10-13-2008, 11:42 AM
kbarnette kbarnette is offline
VanDyke Technical Support
 
Join Date: Aug 2007
Posts: 587
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?

Last edited by jdev; 10-13-2008 at 02:26 PM.
Reply With Quote
  #13  
Old 10-14-2008, 02:47 AM
nxg nxg is offline
Registered User
 
Join Date: Sep 2008
Posts: 8
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
Attached Images
File Type: jpg test2 error.JPG (37.7 KB, 248 views)
Reply With Quote
  #14  
Old 10-14-2008, 05:11 AM
nxg nxg is offline
Registered User
 
Join Date: Sep 2008
Posts: 8
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
Reply With Quote
  #15  
Old 10-14-2008, 08:21 AM
kbarnette kbarnette is offline
VanDyke Technical Support
 
Join Date: Aug 2007
Posts: 587
Hi nxg,

I am glad to hear the issue is resolved.

Thanks for posting the resolution.
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:35 AM.