|
#1
|
|||
|
|||
Logging Device from script and save to text file
I have a script with VBscript that reads the ip names/IP from a text file (IOS-Device.txt) and write to a text file in a directory (S:\Riggs\IOS Test directory).
The device text file (IOS Device.txt) contains the following: Device1;10.10.10.1;Telnet;Test1 Device2;10.10.10.2;Telnet;Test2 Device3;10.10.10.3;Telnet;Test3 This is what I want the script to do: 1. read device from file 2. log into device 3. perform commands 4. write file 5 go to next device and repeat steps. What I see is that it accesses all the devices from the file and perform the commands, I can see this on the screen scrolling through. It also saves the different file names to the proper directory. But the problem is that even though the file names are different, each file contains the commands performed on the last device in the list. For example, the saved text files 1, 2 and 3 have the same command results from Device 3. Below is my code. code: Code:
# $language = "VBScript" # $interface = "1.0" Sub Main strUsername = "admin1" 'not real username strPassword = "getin" 'not real password strPrompt = ">" Set fso = CreateObject("Scripting.FileSystemObject") strDeviceList = "s:\riggs\IOS-Device.txt" Set fil = fso.OpenTextFile(strDeviceList) ' Start loop to iterate over all devices defined in file While Not fil.AtEndOfStream ' Process a line at a time vLine = Split(fil.ReadLine, ";") strHostname = vLine(0) strIP = vLine(1) On Error Resume Next crt.Session.Connect cnxnString strErr = Err.Description nError = Err.Number On Error Goto 0 If crt.Session.Connected = False Then crt.Dialog.MessageBox "The following host cannot be reached: " &_ vbcrlf & vbtab & strHostname & vbcrlf & vbcrlf &_ "With error: " & vbcrlf & vbtab & strErr Else ' Enable Synchronous which is necessary to avoid missing data from ' remote crt.Screen.Synchronous = True crt.Screen.WaitForString "Username:" crt.Screen.Send strUsername & vbcr crt.Screen.WaitForString "Password:" crt.Screen.Send strPassword & vbcr ' Confirm that remote is ready crt.Screen.WaitForString strPrompt ' Start logging to new file strLogFile = "S:\Riggs\IOS Test\" & strHostname & "-%Y-%M-%D--%h-%m-%s.%t.txt" crt.Session.LogFileName = strLogFile crt.Session.Log True ' send deive command crt.Screen.Send "term length 0" & vbcr crt.Screen.Send "show ver" & vbcr crt.Screen.WaitForString strPrompt ' Send device commands crt.Screen.Send "show proc cpu | inc CPU" & vbcr crt.Screen.WaitForString strPrompt crt.Screen.Send "show version" & vbcr crt.Screen.WaitForString strPrompt crt.Screen.Send "show ip route summary" & vbcr crt.Screen.WaitForString strPrompt ' Stop logging crt.Session.Log False ' Disconnect from device If crt.Session.Connected Then crt.Session.Disconnect End If End If Wend ' Close device file fil.Close End Sub Last edited by rtb; 10-24-2012 at 11:32 AM. |
#2
|
|||
|
|||
Hi stuartX,
Thanks for posting the problem. What version of SecureCRT are you using? |
#3
|
|||
|
|||
I believe it 6.0 or later.
|
#4
|
|||
|
|||
Its version 6.7.4
|
#5
|
|||
|
|||
Hi stuartX,
Thanks for the data. I don't see in your posted code where you define cnxnString. I am not sure what your script is doing, but it seems unlikely that the code you posted is actually connecting to multiple remote devices. As a note, if you pass empty data to the Connect method, the Connect method will just reconnect the last session that was connected in the tab where you are running the script. If you were to run your script in a fresh instance that has no previously connected session in a tab, I suspect you would find your script failing when it gets to the point where it needs to connect. Would you confirm that you posted the exact script you are using (with the exception of sensitive data)? Last edited by jdev; 10-24-2012 at 05:28 PM. |
#6
|
|||
|
|||
Yes Sir, it the one I'm using.
how do I use the cnxnstring? |
#7
|
|||
|
|||
Logging Device from script and save to text file and update in exel steste done / not
i want script for below mention command and update in exel file status done / not done i want scrip pls help me i have 3000 router
![]() ssh 192.168.5.1 password :- abc en password :- abc copy running-config tftp: Address or name of remote host []? 192.168.1.1 Destination filename exit i am using below scrip manual work but its so difficult ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() crt.Screen.Send "ssh 192.168.5.1" & chr(13) crt.Screen.WaitForString "Password: " crt.Screen.Send "cisco" & chr(13) crt.Screen.Send "en" & chr(13) crt.Screen.Send "cisco" & chr(13) crt.Screen.WaitForString "#" crt.Screen.Send "copy running-config tftp:" & chr(13) crt.Screen.WaitForString "Address or name of remote host []? " crt.Screen.Send "192.168.1.1" & chr(13) crt.Screen.WaitForString "Destination filename " crt.Screen.Send chr(13) crt.Screen.WaitForString "#" crt.Screen.Send "q" & chr(13) crt.Screen.WaitForString "#" |
#8
|
|||
|
|||
Hello ganeshfarande,
I am not sure this is the appropriate thread for your post, but since the information I plan to share can already be found in other threads, I will leave it here. As a note, we do not write custom scripts, but can assist if you need help with a cohesive task. While you say the script example posted works, you are taking a risk in that you have three "sends" in a row, without any intervening "waitforstrings". Section 4.2 of the Scripting Essentials manual available on our website states that best practice is that there should always be a WaitForString() following each Send() command. The above is probably a moot point, as it sounds like you want to replace that script anyway. ![]() Is the connection information for your 3000 routers already in an Excel spreadsheet? If so, see the example script in another thread, which I think will help you to accomplish your objective.
__________________
Thanks, --Brenda VanDyke Software Technical Support support@vandyke.com (505) 332-5730 |
#9
|
|||
|
|||
![]()
Hi,
I would like some direction on how to accomplish the following, i do not have prior experience in VB however I have done Java and C++ some years ago so I can learn VB with some direction. I would like to be able to have a script prompt the user asking what interface to search for current status ( i.e. - interface ge-1/4/2/1 ) and to look up the shw interface command to check if the link state is up or down. If the state is down to then check the log (i.e.- shw log ) to check when the interface last went down and to output the results in a text file or on the current screen. In essence i would like to have a generic script that can do the following : 1) check the current state of the interface (Link up or Link down) 2) check how long the interface has been up for 3) if link to a specific interface is currently down to grab the exact date/time it went down 4) If link to specific interface went down and then came back up within a period of less than 10min to grab these dates and times Output the results of the above commands to the screen or text file. I deal with both Juniper and Cisco routers so I am aware both use slighly different commands to get the specified result but really need help on how to overall accomplish the above as it requires to "check for specific strings ( is link down or up) and ( look for the date and times it went down and back up) |
#10
|
|||
|
|||
Hi kmusawi,
To familiarize yourself with common scripting concepts as it relates to SecureCRT, I recommend the Scripting Essentials manual available on our website. Quote:
If you are not adverse to using Excel, this post by Jake includes a script example that seems like it might be a good starting point.
__________________
Thanks, --Brenda VanDyke Software Technical Support support@vandyke.com (505) 332-5730 |
#11
|
|||
|
|||
Thanks Brenda, will have a look at that.
|
![]() |
Tags |
log , router , textfile |
Thread Tools | |
Display Modes | Rate This Thread |
|
|