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 03-29-2009, 12:25 PM
linuxtesting linuxtesting is offline
Registered User
 
Join Date: Mar 2009
Posts: 7
Talking two files to make the script more flexible

Hi,

I am writing a script to get the router information

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

Const ForReading = 1
Const ForWriting = 2

Sub Main
Dim fso, file, str

Set fso = CreateObject("Scripting.FileSystemObject")

Set file = fso.OpenTextFile("c:\script\router.txt", ForReading, False)
crt.Screen.Synchronous = True
Do While file.AtEndOfStream <> True
Dim logfile
Dim commm
commm = "sh ver"
str = file.Readline
' crt.Screen.WaitForString str & "$"
crt.Screen.Send "telnet " & str & VbCr
crt.Screen.WaitForString "sername:"
crt.Screen.Send "" & VbCr
crt.Screen.WaitForString "assword:"
crt.Screen.Send "" & VbCr
crt.Screen.WaitForString ">"
crt.Screen.Send "term len 0" & VbCr
crt.Screen.WaitForString ">"
logfile = "C:\script\" & str & commm &".txt"
crt.session.LogFileName = logfile
crt.Session.Log True
crt.Screen.Send strend & VbCr
crt.Screen.WaitForString ">"
crt.Session.Log False
crt.Screen.Send "exit" & VbCr
crt.Screen.WaitForString "."
Loop
crt.Screen.Synchronous = False
End Sub

From the script, it will get the router.txt infomation and preform and log the "sh ver" one by one. Is there any way to make the Dim commm more flexible such as put in more commands in a single file "command.txt" and the script will follow the commands in "command.txt" to generate the log according to the routers listed on router.txt? It will help and I just need to put command in command.txt rather than change the script. Thanks
Reply With Quote
  #2  
Old 03-30-2009, 01:28 PM
miked's Avatar
miked miked is offline
Registered User
 
Join Date: Feb 2004
Posts: 2,040
Hello,

A command file can be a little more versatile and convenient. We have a sample script available here which shows how to send the contents of a file line by line using VBScript.

Does this script do what you're wanting?
__________________
Mike
VanDyke Software
Technical Support
[http://www.vandyke.com/support]
Reply With Quote
  #3  
Old 03-31-2009, 12:10 AM
linuxtesting linuxtesting is offline
Registered User
 
Join Date: Mar 2009
Posts: 7
Hi Mike,

From the above script, I have already make one "router.txt" file to do the readline function. But what I mean is to add an extra file called "command.txt" to make the script more flexible. I would like to know is there any way for the script to read the first router name from router.txt and then run through the commands from command.txt and start the next router one by one until loop through the routers from router.txt?

Thx
Reply With Quote
  #4  
Old 03-31-2009, 06:42 AM
WesR WesR is offline
Registered User
 
Join Date: Mar 2005
Location: USA
Posts: 132
Hi,

In concept you should just be able to open another file and then add another loop to loop through the commands in that file. Something like this:

Code:
Set file = fso.OpenTextFile("c:\script\router.txt", ForReading, False)
Set file2 = fso.OpenTextFile("c:\script\command.txt", ForReading, False)

Do While file.AtEndOfStream <> True
	str = file.Readline
	'---- telnet and log in here

	Do While file2.AtEndOfStream <> True
		Command = file2.Readline
		crt.Screen.Send Command & VbCr
		crt.Screen.WaitForString ">"
	Loop
	
	'Disconnect from telnet here
Loop
Note, I made no attempt to make this executable code, just wanted to give you the idea.

Wes
Reply With Quote
  #5  
Old 04-01-2009, 12:12 PM
robertwlane robertwlane is offline
Registered User
 
Join Date: Mar 2009
Posts: 9
Question

I have a similar need to take instructions from two different files to complete a process. I incorporated the above example of a loop within a loop and its only partially successful. It connects to all of the devices listed in the router.txt file in sequence, so that loop works. However, it never accesses the command.txt file listed in the second loop. So it connects, and then disconnects. The logging works fine as well and just shows the device banner and prompt.

Does anyone see anything that I'm missing? I'm pulling my hair out and any help would be greatly appreciated.

With the script, not pulling my hair out.


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


Sub main 

Const user = "user" 
Const pass = "pass"
Const ForReading = 1

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

Dim file
Dim file2
Dim cmd
Dim str
Dim command

Set file = fso.OpenTextFile("C:\Putty\router.txt", ForReading, False)
Set file2 = fso.OpenTextFile("C:\Putty\command.txt", ForReading, False)

Do While file.AtEndOfStream <> True
	Dim logfile
	str = file.Readline
	logfile = ("C:\Putty\SecureCRT scripts\" & str & ".txt")
    	cmd = "/SSH2 /L " & user & " /PASSWORD " & pass & " /C 3DES /M MD5 " & str
    	crt.Session.Connect cmd
	crt.Session.LogFileName = logfile
	crt.Session.Log True
	crt.Screen.Synchronous = True
	crt.Screen.WaitForString "#"
        crt.Screen.Send "term length 0" & vbCr

		Do While file2.AtEndOfStream <> True
			command = file2.Readline
			crt.Screen.WaitForString "#"
			crt.Screen.Send command & VbCr
		Loop
	
        crt.Screen.Send "exit" & vbCr
	crt.Session.Log False
	crt.Session.Disconnect

Loop

End Sub
Reply With Quote
  #6  
Old 04-01-2009, 01:39 PM
WesR WesR is offline
Registered User
 
Join Date: Mar 2005
Location: USA
Posts: 132
Ah,

What you need to do is to close the command file and re-open it each time so that it starts over. There might be a way to reset the pointer instead of doing the open/close over and over, but this is the easiest way I know to solve your problem. If you move the open down so it is in the outer loop and add a close like this, it should work:


Code:
	Set file2 = fso.OpenTextFile("C:\command.txt", ForReading, False)
		Do While file2.AtEndOfStream <> True
			command = file2.Readline
			crt.Screen.Send command & VbCr
			crt.Screen.WaitForString "#"
		Loop
	file2.close
Wes
Reply With Quote
  #7  
Old 04-01-2009, 03:00 PM
robertwlane robertwlane is offline
Registered User
 
Join Date: Mar 2009
Posts: 9
Thanks!

I tried the script changes that you suggested but it still did not entirely work. It was much, much closer, since it would now read and run the first command from the file, but it would log off before the second. So I made a couple of adjustments and it now works perfectly. Thanks!

Code:
Do While file.AtEndOfStream <> True
	Dim logfile
	str = file.Readline
	logfile = ("C:\Putty\SecureCRT scripts\" & str & ".txt")
    	cmd = "/SSH2 /L " & user & " /PASSWORD " & pass & " /C 3DES /M MD5 " & str
    	crt.Session.Connect cmd
	crt.Session.LogFileName = logfile
	crt.Session.Log True
	crt.Screen.Synchronous = True
        crt.Screen.Send "term length 0" & vbCr
	crt.Screen.WaitForString "" & str & "#"
	Set file2 = fso.OpenTextFile("C:\Putty\command.txt", ForReading, False)

		Do 
			command = file2.Readline
			crt.Screen.Send command & VbCr
			crt.Screen.WaitForString "" & str & "#"
		Loop While file2.AtEndOfStream <> True

	crt.Screen.WaitForString "" & str & "#"
        crt.Screen.Send "exit" & vbCr
	crt.Session.Disconnect

	file2.close
	crt.Session.Log False

Loop
Reply With Quote
  #8  
Old 04-01-2009, 03:40 PM
bgagnon bgagnon is offline
VanDyke Technical Support
 
Join Date: Oct 2008
Posts: 4,424
WesR, thank you for the help!

robertwlane, thank you for posting your success here.
__________________
Thanks,
--Brenda

VanDyke Software
Technical Support
support@vandyke.com
(505) 332-5730
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 08:08 PM.