Solved

How to send commands via telnet session to a network switch in wndows?

Posted on 2009-07-02
5
2,854 Views
Last Modified: 2012-05-07
Hi,

In any UNIX/LINUX box I can use the telnet client in combination with echo to create scripts and send commands to a Cisco router redirecting the console output to a file, for example:

     echo "user";\
     echo "password";\
     echo "term len 0";\
     echo "show version";\
     echo "q";\
     |telnet 10.10.10.10 > output.txt

According to two threads that I found here it seems that it is not possible to do this with MS telnet client so they used VB scripts. For example:

set WshShell = WScript.CreateObject("WScript.Shell")
WScript.Sleep 100
WshShell.AppActivate "C:\Windows\system32\cmd.exe"
WScript.Sleep 100
WshShell.SendKeys "telnet 10.10.10.10{ENTER}"
WScript.Sleep 100
WshShell.SendKeys "abc{ENTER}" 'Username
WshShell.SendKeys "abc{ENTER}" 'Password
WScript.Sleep 100
WshShell.SendKeys "display ver{ENTER}"
WScript.Sleep 200
WshShell.SendKeys "exit{ENTER}" 'close telnet session'

The problem is that when I use the greater than character (">") to redirect the console output to a file, the file gets created but it remains empty after the session is closed. So how can I redirect the output? Also I was planning to use some kind of "command substitution" with some sort of loop in the script since I need to execute the commands with many network switches but again I don't know how to do it within the VB script file. Any ideas?
0
Comment
Question by:ecajigas
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
5 Comments
 
LVL 5

Expert Comment

by:boowhup
ID: 24768537
This code will go through a list of switches in a file (should be ok to be either IP's or DNS names). And perform "SendCommand(switchname)" on each entry. (SendCommand performs your above script, but will add the switch name/ip each loop.

'Declare pulbic variables
strSwitchFile = ".\servers.txt"
strLogFile = ".\log-TEXTFILE.csv"
'============================================================
'Start
WriteToTxt Now & ",====================,Script Started,====================", strLogFile
' open server file for reading, get each server name and perform tests.
	ReadFileList
WriteToTxt Now & ",====================,Script Finished,====================", strLogFile
'Finish
'=============================================================
 
Sub ReadFileList
	Set objFSO = CreateObject("Scripting.FileSystemObject")
	'On error resume next
	Set objSwitchFile = objFSO.OpenTextFile(strSwitchFile, 1)
	if err.number <> 0 then
		WriteToTxt(Now & ":  !ERROR!, Error opening " & strSwitchFile)
	else	
		' iterates through server list, then do Service checks if ping successful against PC name.
		Do Until objSwitchFile.AtEndOfStream
			strSwitch = trim(objSwitchFile.Readline)
			SendCommands(strSwitch)
		Loop
	end if
	on error goto 0
End Sub
 
Sub WriteToTxt(strWrite,strLogFile)
	'Appends lines to Log File
	Set FSO = CreateObject("Scripting.FileSystemObject")
	Set objOutputFile = FSO.OpenTextFile(strLogFile, 8, True)
	objOutputFile.WriteLine(strWrite)
	objOutputFile.close
End Sub
 
Sub SendCommands (strSwitch)
	set WshShell = WScript.CreateObject("WScript.Shell") 
	WScript.Sleep 100 
	WshShell.AppActivate "C:\Windows\system32\cmd.exe" 
	WScript.Sleep 100 
	WshShell.SendKeys "telnet " & strSwitch & "{ENTER}" 
	WScript.Sleep 100 
	WshShell.SendKeys "abc{ENTER}" 'Username
	WshShell.SendKeys "abc{ENTER}" 'Password
	WScript.Sleep 100 
	WshShell.SendKeys "display ver{ENTER}"
	WScript.Sleep 200 
	WshShell.SendKeys "exit{ENTER}" 'close telnet session'
End Sub

Open in new window

0
 
LVL 5

Accepted Solution

by:
boowhup earned 500 total points
ID: 24768672
This one will use a command line argument I(so you can use another script/batch file to do the looping)
'	Declare pulbic variables
strLogFile = ".\log-TEXTFILE.csv"
'============================================================
'Start
WriteToTxt Now & ",====================,Script Started,====================", strLogFile
' open server file for reading, get each server name and perform tests.
	GetSwitch
WriteToTxt Now & ",====================,Script Finished,====================", strLogFile
'Finish
'=============================================================
 
 
Sub GetSwitch
	if Wscript.Arguments.Count = 1 then
		strSwitch = trim(Wscript.Arguments.item(0) & "")
		SendCommands(strSwitch)
	end if
 
	if Ping(strComputer) then
		GetServiceState strComputer,strServiceName
	else
		WriteToTxt Now & "," & strComputer & ",No Ping Response, ENDRESULT" ,strLogFile
	end if
End Sub
 
Sub WriteToTxt(strWrite,strLogFile)
	'Appends lines to Log File
	Set FSO = CreateObject("Scripting.FileSystemObject")
	Set objOutputFile = FSO.OpenTextFile(strLogFile, 8, True)
	objOutputFile.WriteLine(strWrite)
	objOutputFile.close
End Sub
 
Sub SendCommands (strSwitch)
	set WshShell = WScript.CreateObject("WScript.Shell") 
	WScript.Sleep 100 
	WshShell.AppActivate "C:\Windows\system32\cmd.exe" 
	WScript.Sleep 100 
	WshShell.SendKeys "telnet 10.10.10.10{ENTER}" 
	WScript.Sleep 100 
	WshShell.SendKeys "abc{ENTER}" 'Username
	WshShell.SendKeys "abc{ENTER}" 'Password
	WScript.Sleep 100 
	WshShell.SendKeys "display ver{ENTER}"
	WScript.Sleep 200 
	WshShell.SendKeys "exit{ENTER}" 'close telnet session'
 
End Sub

Open in new window

0
 
LVL 5

Expert Comment

by:boowhup
ID: 24768691
I'm not too sure on the file output issue , but you could try this as an alternative to the code you ahve.


set WshShell = WScript.CreateObject("WScript.Shell") 
WScript.Sleep 100 
WshShell.AppActivate "C:\Windows\system32\telnet.exe " & strSwitch & " > C:\Folder\output.txt"
WScript.Sleep 100 
WshShell.SendKeys "abc{ENTER}" 'Username
WshShell.SendKeys "abc{ENTER}" 'Password
WScript.Sleep 100 
WshShell.SendKeys "display ver{ENTER}"
WScript.Sleep 200 
WshShell.SendKeys "exit{ENTER}" 'close telnet session'

Open in new window

0
 
LVL 5

Expert Comment

by:boowhup
ID: 24768695
Oh and a caveat - I haven't tested this code , ther's porbably some syntax errors and spelling mistakes.
0
 
LVL 3

Expert Comment

by:astroviper
ID: 24769839
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

AutoHotkey is an excellent, free, open source programming/scripting language for Windows. It started out as a keyboard/mouse macros product, but has expanded into a robust language. This article provides an introduction to it, with links to addition…
Hello to you all, I hear of many people congratulate AWS (Amazon Web Services) on how easy it is to spin up and create new EC2 (Elastic Compute Cloud) instances, but then fail and struggle to connect to them using simple tools such as SSH (Secure…
Viewers will learn how to properly install and use Secure Shell (SSH) to work on projects or homework remotely. Download Secure Shell: Follow basic installation instructions: Open Secure Shell and use "Quick Connect" to enter credentials includi…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question