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 02-13-2018, 03:15 AM
FredD FredD is offline
Registered User
 
Join Date: Feb 2018
Posts: 3
Read input on new line from input file

Hello,

I have 2 input files:
-1 containing the IP'ss from the devices I'd like to configure
-1 containing the IP addresses that I would like to add to each device

I'm able to connect to each device and provide my credentials, for each device in file "RoutersLoopback.txt"
(this files contains an ip address on e ach line)
But after this I'm stuck.
For the 1st router I can configure the IP address, from file "RouterLanIP.txt", but after exiting this router and going to the next router it reads the same line with LAN info again.
However, the goal is that each device gets an other IP on "int vlan 3"

Could anyone help me finding the right track

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

crt.Screen.Synchronous = True

Sub Main
	i = 0
	
	dim str     
	Pass = "TacacsPAssWord" '- Put your tacacs password between the quotes 
	RouterLoopbackInput = "C:\Users\xxx\RoutersLoopback.txt" '- location of your router loopback input file	 
	
	set objFSO = CreateObject("Scripting.FileSystemObject")
	
	set objRouterLoopbackInput = objFSO.OpenTextFile( RouterLoopbackInput,1 )
	Do until objRouterLoopbackInput.AtEndOfStream = True
		strHost = objRouterLoopbackInput.ReadLine
			crt.Screen.Send "ssh " & strHost & chr(13)
		str = crt.Screen.WaitForString ("Password: ",3)
		If str = True Then 
			crt.Screen.Send Pass & chr(13) 
		Else 
			crt.Screen.Send "yes" & chr(13)
			crt.Screen.WaitForString "Password: "
			crt.Screen.Send Pass & chr(13)
		End If
		crt.Screen.WaitForString ">"
		crt.Screen.Send "ena" & chr(13)
		crt.Screen.WaitForString "Password: "
		crt.Screen.Send Pass & chr(13)
		crt.Screen.WaitForString "#"

		'-put your command sequence as of the next line
		
		crt.Screen.Send "conf t" & chr(13)
		crt.Screen.Send "int vlan 3" & chr(13)
		
		'TEST
				
		RouterLanIPInput = "C:\Users\xxx\RouterLanIP.txt" '- location of your router LAN info	 
		set objRouterLanIPInput = objFSO.OpenTextFile( RouterLanIPInput,1 )
		
			'Do While RouterLanIPInput.AtEndOfStream <> True 'extra	
			dump = objRouterLanIPInput.ReadLine
			crt.Screen.Send (dump) & chr(13)
		i = i + 1
		'Loop

		'TEST
		
		'-exit from svi config mode
		crt.Screen.Send "exit" & chr(13)
		'-exit from global config mode
		crt.Screen.Send "exit" & chr(13)
		'-here you'll save the config
		crt.Screen.Send "write" & chr(13)
		crt.Screen.WaitForString "#"
		'-exit from the router
		crt.Screen.Send "exit" & chr(13)
		crt.Screen.WaitForString "closed."
	loop
End Sub
Reply With Quote
  #2  
Old 02-13-2018, 10:53 AM
bgagnon bgagnon is offline
VanDyke Technical Support
 
Join Date: Oct 2008
Posts: 3,496
Hi FredD,

I can see various instances where you are violating what I have come to think of as the Golden Rule of Scripting. (I tried putting that text in Yellow, but it was unreadable.)

Code:
		crt.Screen.Send "conf t" & chr(13)
		crt.Screen.Send "int vlan 3" & chr(13)
To keep your script "in sync", you should always have a corresponding WaitForString() following every Send() as described in Section 4.2 of the scripting manual on our website here.

That's not likely the cause of the issue, though. I presently do not understand what you are trying to accomplish with the part of your script that starts with those two lines and continues to the end.

Quote:
... but after exiting this router and going to the next router it reads the same line with LAN info again.
However, the goal is that each device gets an other IP on "int vlan 3"
Does the RouterLanIP text file contain this info?

And your goal is to iterate over it at the same pace as you iterate over the device file?
__________________
Thanks,
--Brenda

VanDyke Software
Technical Support
support@vandyke.com
(505) 332-5730
Reply With Quote
  #3  
Old 02-14-2018, 02:21 AM
FredD FredD is offline
Registered User
 
Join Date: Feb 2018
Posts: 3
Hi Brenda,

Thx for the reply.

The goal is to log in on several devices, these devices are identified by the file "RoutersLoopback.txt".

The hardcoded "conf t" is used on each device to get to the correct configuration mode (like a "context").

Then each device needs to be configured with "int vlan 3" which is also hardcoded as it is the same for all devices.

After which each "int vlan 3" on each device shoud be configured with a different ip address, which can be found in file "RouterLanIP.txt"

Thx
FredD
Reply With Quote
  #4  
Old 02-14-2018, 08:32 AM
bgagnon bgagnon is offline
VanDyke Technical Support
 
Join Date: Oct 2008
Posts: 3,496
Hi FredD,

Quote:
After which each "int vlan 3" on each device shoud be configured with a different ip address, which can be found in file "RouterLanIP.txt"
And to do that you just "send" that IP address to the remote, as you are doing with this line?:
Code:
crt.Screen.Send (dump) & chr(13)
__________________
Thanks,
--Brenda

VanDyke Software
Technical Support
support@vandyke.com
(505) 332-5730
Reply With Quote
  #5  
Old 02-14-2018, 09:18 AM
bgagnon bgagnon is offline
VanDyke Technical Support
 
Join Date: Oct 2008
Posts: 3,496
Hi FredD,

A colleague pointed out you are seeing the reported behavior because you have the code to open the file within the outer Do Loop. So it is ALWAYS being opened and ReadLine() is only ever reading the first line each time.

What are the results if you move that line of code outside the loop (where your other OpenTextFile() call is)?

If you are still having difficulty, please post updated code.
__________________
Thanks,
--Brenda

VanDyke Software
Technical Support
support@vandyke.com
(505) 332-5730
Reply With Quote
  #6  
Old 02-15-2018, 12:25 AM
FredD FredD is offline
Registered User
 
Join Date: Feb 2018
Posts: 3
Thumbs up

Hello Brenda,

I've modified my script according to the "Golden rules".
Afterwards I did the modification like you suggested and this works!

Thank you!
FredD

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

crt.Screen.Synchronous = True

Sub Main

	dim str     
	Pass = "tacacs_password" '- Put your tacacs password between the quotes 
	
	set objFSO = CreateObject("Scripting.FileSystemObject")
	
	RouterLoopbackInput = "C:\your_folder\RoutersLoopback.txt" '- location of your device loopback input file	 
	set objRouterLoopbackInput = objFSO.OpenTextFile( RouterLoopbackInput,1 )

	RouterLanIPInput = "C:\your_folder\RouterLanIP.txt" '- location of your router LAN info	 
	set objRouterLanIPInput = objFSO.OpenTextFile( RouterLanIPInput,1 )	
		
	Do until objRouterLoopbackInput.AtEndOfStream = True
		strHost = objRouterLoopbackInput.ReadLine
			crt.Screen.Send "ssh " & strHost & chr(13)
		str = crt.Screen.WaitForString ("Password: ",3)
		If str = True Then 
			'-Next line sends your password
			crt.Screen.Send Pass & chr(13) 
		Else 
			crt.Screen.Send "yes" & chr(13)
			crt.Screen.WaitForString "Password: "
			'-Next line sends your password
			crt.Screen.Send Pass & chr(13)
		End If
		crt.Screen.WaitForString ">"
		'-Next line sends "ena" whis is the abbreveation for "enable" and permits you to go to "exec" mode
		crt.Screen.Send "ena" & chr(13)
		crt.Screen.WaitForString "Password: "
		'-Next line sends your password
		crt.Screen.Send Pass & chr(13)
		crt.Screen.WaitForString "#"
		'-you're now logged in to the router
		crt.Screen.Send "conf t" & chr(13)
		crt.Screen.WaitForString "(config)#"
		'-now you've entered global config mode
		crt.Screen.Send "int vlan 6" & chr(13)
		crt.Screen.WaitForString "(config-if)#"
		'-now you've entered interface config mode
		'-read info from input file 
		strLanIP = objRouterLanIPInput.ReadLine
		'-send the read info to the device
		crt.Screen.Send (strLanIP) & chr(13)
		crt.Screen.WaitForString "(config-if)#"
		'-exit from interface config mode
		crt.Screen.Send "exit" & chr(13)
		crt.Screen.WaitForString "(config)#"
		'-exit from global config mode
		crt.Screen.Send "exit" & chr(13)
		crt.Screen.WaitForString "#"
		'-here you'll save the config
		crt.Screen.Send "write" & chr(13)
		crt.Screen.WaitForString "#"
		'-exit from the device
		crt.Screen.Send "exit" & chr(13)
		crt.Screen.WaitForString "closed."
	loop
End Sub
Reply With Quote
  #7  
Old 02-15-2018, 07:51 AM
bgagnon bgagnon is offline
VanDyke Technical Support
 
Join Date: Oct 2008
Posts: 3,496
Hi FredD,

Thanks for the update. Great! Adhering to that "best practice" will ensure the script stays "in sync".

The credit goes to my manager, Jake, for spotting the issue.

Have a great weekend!
__________________
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 04:24 PM.