Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Automating a telnet session 1

Status
Not open for further replies.

AntiEarnie

Technical User
May 2, 2002
215
0
0
US
I'm trying to grab some interface statistics off one of our switches. I've tried a few methods so far and have had little luck. At first I thought I could use Exec in WScript.Shell. However when I tried that method I couldn't send any keystrokes to work through the menus. When I tried just using the sendkys command I can get to the section I want but I can't capture the output to parse out the statistics. Does anyone know how you can get VBscript to do this? Its really annoying since I used to have a number of perl scripts that did this stuff just fine.
 
Have you tried to play with the StdIn property of the WshScriptExec object to send your keystrokes ?

Hope This Help
PH.
 
Yeah. No luck though. Its almost like the telnet closes right after its called. This is the current incarnation of my little telnet script.

[blue]
'This section will pass the ping command and address to the host
set oSh=CreateObject("WScript.Shell")
Set oEx=oSh.Exec("telnet 192.168.11.249")
oEx.StdIn.Writeline "?" & vbcrlf
oEx.StdIn.Writeline "interface statistics 13" & vbcrlf
oEx.StdIn.Writeline "exit" & vbcrlf
oEx.StdIn.Writeline "logout" & vbcrlf

do while oEx.Status=0
Wscript.Sleep 100
Loop

'Grabs the results of the Telnet
if not oEx.StdOut.AtEndOfStream Then
buf=oEx.StdOut.ReadAll
End if

msgbox buf
[/blue]

As it is all that I get is a blank message box. If I try adding in some sleep statments I just get a "the pipe is being closed" error. So my script isn't getting killed by sending commands before the telnet is ready.
 
dougcranston,

I've done it with perl scripting in hp unix once upon a time. VBScript however is being troublesome with this.

After reading over the link I decided to give Run a shot again. The following code works for communicating with the switch. However, according to the MS Windows 2000 Scripting Guide the Run method CAN NOT retrieve output from a Command-Line tool. I tried several different ways to pull the Standard Output but all of them generated errors.

[blue]
set oShell=CreateObject("WScript.Shell")

oShell.Run "telnet 192.168.12.247", 9
WScript.sleep 500

oShell.Sendkeys "MONITOR{ENTER}{ENTER}"
WScript.sleep 500

oShell.Sendkeys "INTERFACE STATISTICS 13{ENTER}"
WScript.sleep 500

oShell.Sendkeys " "
WScript.sleep 500

oShell.Sendkeys " "
WScript.sleep 500

oShell.Sendkeys "EXIT{ENTER}"
WScript.sleep 500

oShell.Sendkeys "LOGOUT{ENTER}"
[/blue]

From pretty much all the documentation I found Run seems only useful for commnand line macros since it can not do anything with the output. Exec seems like the only method that will allow collection of the output. However, when using Exec my telnets behave like they are exiting before the next line of code in the script can be executed. According to the documentation Exec does indeed have a StdIn but I can not get it to work.
 
AntiEarnie,

Modified your snippet for my HP Unix system and it works fine.

set oShell=CreateObject("WScript.Shell")

oShell.Run "telnet 192.168.12.247", 9
WScript.sleep 500

oShell.Sendkeys "logonname{ENTER}"
WScript.sleep 500

oShell.Sendkeys "password{ENTER}"
WScript.sleep 500

oShell.Sendkeys "ls -l{ENTER}"
WScript.sleep 2500

oShell.Sendkeys "exit{ENTER}"

Did not recognize your other codes so I only worked some that I could relate too..

HTH
DougCranston
 
The script will run and will indeed telnet to the interface. What I can not do however is GATHER the information that is returned. My objective is to have the script capture the text in a string. Then I can search it for just a couple statistics on the interface and save them off.

I've already done this with pings to critical hardware. However, the only arguments that ping has are command line.

 
AntiEarnie,

Well, I am going to guess you already have considered sending the information by piping it to a file? Then you could dload it and do your stuff their.

Many of the higher end Telnet apps provide scripting that could accommodate this. Procomm Plus, KEAx and others.

Sorry I wasn't much help.

DougCranston
 
Yep. Pipe does not seem to work with anything besides basic command line stuff (i.e. ping, ipconfig, etc).

I am starting to wonder if VBscript can do this with telnets. I've been piddleing with this on and off for about a week and all I can even find online is extra librarys and apps that do nothing but this. It just seems strange that I can do everything from firing a bat file to updating and modifying the regestry and Active Directories. Yet can not grab the info a telnet seession gives.
 
AntiEarnie,

Have you looked at trying to script HyperTerminal?

Standard with 95, NT and 2k.

Has a capture to disk feature.

Look at HyperTerminal.

Just a thought.
DougCranston
 
Some of the alternatives listed look interesting, but in many ways they fall short.

The big problem you have is that the telnet.exe supplied by Microsoft does not do its console I/O via stdin and stdout. I found another "telnet" program at SourceForge called "Console Telnet" or "Telnet-Win32" (aliases) that promises this capability, but in my tests fails miserably to deliver... i.e. I couldn't get it to work.

Some of the tools at the links above are downright ludricrous (they don't seem to have any idea what Telnet really is). Other show some promise (such as the one referenced at the dbforums link above), but clearly were written by a non-native English speaker and have nearly unintelligible documentation. Might be great, but I haven't the patience to decipher them right now.

Your best bet might indeed be some 3rd party scriptable Telnet application, if you aren't set on running things from a WSH script.

Otherwise you will need to do this via some ActiveX component. But as I said, the ones above are either just fancy Winsock components in some cases, or possibly just what you need (but hard to figure out quickly) in other cases.

There are several commercial Telnet components however. Dart, Distinct, and Catalyst are well-known brands.

It appears that a script component could be written based upon Microsoft's Winsock control, the free SocketWrench control from Catalyst, or any of several other free Winsock controls. This would take some effort to do a good job though: a halfway-decent Telnet implementation involves dealing with option negotiation at least in a passive manner. For some applications you might get by with a Winsock component and just "hope for the best" (i.e. that the server you want to talk to is really dumb too). In your case the "server" is a switch.

You'd be home free if only you had a Telnet application that truly used stdin/stdout for console I/O. I think that's where we're stuck here.

Don't feel too bad, I don't think the problem is you (or us responding). See for an idea how badly others hunger for the same capability. The answer given there might be just the ticket, I just don't see any followups beyond where somebody posted there erroneous attempt to use the "solution."

This is the telscript solution that dougcranston's last link points (ultimately) to. It's also the one that might work fine, except I can't make any sense out of its documentation. It looks more like an application supporting an ActiveX Automation interface (like Word, etc.) than an ActiveX component.

Maybe the lack of followups means this is a dead-end as well?
 
WOO HOO!

Thanks guys. After reading over the links both of you sent I went and tracked down the Toolsack library. That appears to do everything I was currently looking to do and even provides some very useful objects to flesh out some of my other diagnostic tools. It would have been nice to stick to native VBScript but it doesn't look like it can do it.

Here is my basic code that does a telnet and captures the result in a var for a multitude of uses. Please note that this will require Toolsack which was available for free from
[blue]
''''''''''''''''''''''''''''''''''''''
''
'' Bert Rhoads Jr. December 10, 2003
''
'' This script uses the sockets operator
'' from Toolsack to communicate with a
'' network device.
''
'' Toolsack can be found at:
'' ''
''''''''''''''''''''''''''''''''''''''

'The target device
cHost = "192.168.11.249"
'The port to connect to. Telnet is 23
nPort = 23

'This script will not run withought the Toolsack
'library installed.
set s = CreateObject("Toolsack.Socket")

s.Connect cHost, nPort

'sequence of commands, they dont seem to need
'a sleep time like sendkeys does.
s.Write "monitor" & vbCR
s.Write "interface statistics 13" & vbcrlf
s.Write " "
s.Write "exit" & vbcrlf
s.Write "logout" & vbcrlf

'Need a sleep statment or script will try and read
'the buffer before its full.
wscript.sleep 800

'Save the output to a string for general use
sBuf = s.read
wscript.echo sBuf

s.close()
[/blue]
 
Anyway, to do I/O redirection with the Run method you have to play with the command interpreter:
Code:
obj.Run "%COMSPEC% /C " & CmdLine & " >Myout"

Hope This Help
PH.
 
PHV -
You can't redirect a command line telnet session. At least in Win2k. Run will not work with telnet anyway. It seems to kill the telnet session before the next line of code can be executed.
 
I agree, it was just a general note.

Hope This Help
PH.
 
AntiEarnie,

If Toolsack's socket control works for you, great!

For many of us this is still a problem at times though. As I alluded to ealier, a TCP socket connection is NOT a Telnet session. What I mean is, Telnet operates over a TCP session on port 23, but THERE IS A TELENET PROTOCOL THAT OPERATES AT A LAYER ABOVE TCP, i.e. the Telnet application layer protocol.

Some devices supporting "Telnet" access are truly "dumb" enough to let you talk to them over a simple TCP socket. But many will lock up on you because they are expecting things like "Telnet option negotiation" to take place, and they won't just ignore your socket's non-response to session attribute negotiation.

This is activity that normally is strictly between the Telnet client program and the Telnet server. The user never sees it.

As an example, I've captured a very short session between a Windows Telnet client and a Windows Telnet server. This text seems to cause Tek-Tips' TGML processor grief, so I've had to present it here in plain text. It'll make more sense if you copy it from here and paste it into NotePad, where you can see it in a monospaced font (and therefore columns will align properly).


TCPTap - TCP Traffic Capture Tool (0.10.1 beta)
Copyright © 2001 Robert D. Riemersma, Jr.

Capture Mode: ASCII
Client Port: 23
Server Port: 23
Server Host: BigDog
Capture appended to: C:\Documents and Settings\George\Desktop\BigDog.log

Capture initiated: 12/10/2003 9:27:14 PM


21:27:21 Client connected. Port 23 Host [IP 127.0.0.1]


21:27:21 Server connected. Port 23 Host "BigDog" [IP 192.168.123.162]

21:27:21 <- 0000 FF FB 01 FF FB 03 FF FD 27 FF FD 1F FF FD 00 FF ÿû.ÿû.ÿý'ÿý.ÿý.ÿ
0010 FB 00 û.
21:27:21 -> 0000 FF FD 01 ÿý.
21:27:21 <- 0000 57 65 6C 63 6F 6D 65 20 74 6F 20 4D 69 63 72 6F Welcome to Micro
0010 73 6F 66 74 20 54 65 6C 6E 65 74 20 53 65 72 76 soft Telnet Serv
0020 69 63 65 20 0D 0A ice ..
21:27:21 -> 0000 FF FD 03 ÿý.
21:27:21 -> 0000 FF FB 27 ÿû'
21:27:21 -> 0000 FF FB 1F ÿû.
21:27:21 -> 0000 FF FA 1F 00 50 00 19 FF F0 ÿú..P..ÿð
21:27:21 -> 0000 FF FB 00 ÿû.
21:27:21 -> 0000 FF FD 00 ÿý.
21:27:21 <- 0000 FF FA 27 01 FF F0 FF FA 27 01 03 53 46 55 54 4C ÿú'.ÿðÿú'..SFUTL
0010 4E 54 56 45 52 03 53 46 55 54 4C 4E 54 4D 4F 44 NTVER.SFUTLNTMOD
0020 45 FF F0 Eÿð
21:27:21 -> 0000 FF FA 27 00 FF F0 ÿú'.ÿð
21:27:21 <- 0000 0A 0D 6C 6F 67 69 6E 3A 20 ..login:
21:27:21 -> 0000 FF FA 27 00 03 53 46 55 54 4C 4E 54 56 45 52 01 ÿú'..SFUTLNTVER.
0010 32 03 53 46 55 54 4C 4E 54 4D 4F 44 45 01 43 6F 2.SFUTLNTMODE.Co
0020 6E 73 6F 6C 65 FF F0 nsoleÿð
21:27:25 -> 0000 47 G
21:27:25 <- 0000 47 G
21:27:27 -> 0000 65 e
21:27:27 <- 0000 65 e
21:27:27 -> 0000 6F o
21:27:27 <- 0000 6F o
21:27:27 -> 0000 72 r
21:27:27 <- 0000 72 r
21:27:28 -> 0000 67 g
21:27:28 <- 0000 67 g
21:27:28 -> 0000 65 e
21:27:28 <- 0000 65 e
21:27:28 -> 0000 0D .
21:27:28 <- 0000 0A 0D 70 61 73 73 77 6F 72 64 3A 20 ..password:
21:27:29 -> 0000 78 x
21:27:30 -> 0000 78 x
21:27:30 -> 0000 78 x
21:27:31 -> 0000 0D .
21:27:31 <- 0000 FF FD 18 ÿý.
21:27:31 -> 0000 FF FB 18 ÿû.
21:27:31 <- 0000 FF FA 18 01 FF F0 ÿú..ÿð
21:27:31 -> 0000 FF FA 18 00 41 4E 53 49 FF F0 ÿú..ANSIÿð
21:27:31 <- 0000 1B 5B 31 3B 31 48 2A 3D 3D 3D 3D 3D 3D 3D 3D 3D .[1;1H*=========
0010 3D 3D 3D 3D 3D 3D 3D 3D 3D 3D 3D 3D 3D 3D 3D 3D ================
0020 3D 3D 3D 3D 3D 3D 3D 3D 3D 3D 3D 3D 3D 3D 3D 3D ================
0030 3D 3D 3D 3D 3D 3D 3D 3D 3D 3D 3D 3D 3D 3D 3D 3D ================
0040 3D 3D 3D 3D 3D 3D 20 20 20 20 20 20 20 20 20 20 ======
0050 20 20 20 20 20 20 1B 5B 32 3B 31 48 57 65 6C 63 .[2;1HWelc
0060 6F 6D 65 20 74 6F 20 4D 69 63 72 6F 73 6F 66 74 ome to Microsoft
0070 20 54 65 6C 6E 65 74 20 53 65 72 76 65 72 2E 20 Telnet Server.
0080 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
0090 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
00A0 20 20 20 20 20 20 20 20 20 20 20 20 1B 5B 33 3B .[3;
00B0 31 48 2A 3D 3D 3D 3D 3D 3D 3D 3D 3D 3D 3D 3D 3D 1H*=============
00C0 3D 3D 3D 3D 3D 3D 3D 3D 3D 3D 3D 3D 3D 3D 3D 3D ================
00D0 3D 3D 3D 3D 3D 3D 3D 3D 3D 3D 3D 3D 3D 3D 3D 3D ================
00E0 3D 3D 3D 3D 3D 3D 3D 3D 3D 3D 3D 3D 3D 3D 3D 3D ================
00F0 3D 3D 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ==
0100 20 20 1B 5B 34 3B 31 48 43 3A 5C 44 6F 63 75 6D .[4;1HC:\Docum
0110 65 6E 74 73 20 61 6E 64 20 53 65 74 74 69 6E 67 ents and Setting
0120 73 5C 41 64 6D 69 6E 69 73 74 72 61 74 6F 72 3E s\Administrator>
0130 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
0140 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
0150 20 20 20 20 20 20 20 20 1B 5B 35 3B 31 48 1B 5B .[5;1H.[
0160 4B 1B 5B 36 3B 31 48 1B 5B 4B 1B 5B 37 3B 31 48 K.[6;1H.[K.[7;1H
0170 1B 5B 4B 1B 5B 38 3B 31 48 1B 5B 4B 1B 5B 39 3B .[K.[8;1H.[K.[9;
0180 31 48 1B 5B 4B 1B 5B 31 30 3B 31 48 1B 5B 4B 1B 1H.[K.[10;1H.[K.
0190 5B 31 31 3B 31 48 1B 5B 4B 1B 5B 31 32 3B 31 48 [11;1H.[K.[12;1H
01A0 1B 5B 4B 1B 5B 31 33 3B 31 48 1B 5B 4B 1B 5B 31 .[K.[13;1H.[K.[1
01B0 34 3B 31 48 1B 5B 4B 1B 5B 31 35 3B 31 48 1B 5B 4;1H.[K.[15;1H.[
01C0 4B 1B 5B 31 36 3B 31 48 1B 5B 4B 1B 5B 31 37 3B K.[16;1H.[K.[17;
01D0 31 48 1B 5B 4B 1B 5B 31 38 3B 31 48 1B 5B 4B 1B 1H.[K.[18;1H.[K.
01E0 5B 31 39 3B 31 48 1B 5B 4B 1B 5B 32 30 3B 31 48 [19;1H.[K.[20;1H
01F0 1B 5B 4B 1B 5B 32 31 3B 31 48 1B 5B 4B 1B 5B 32 .[K.[21;1H.[K.[2
0200 32 3B 31 48 1B 5B 4B 1B 5B 32 33 3B 31 48 1B 5B 2;1H.[K.[23;1H.[
0210 4B 1B 5B 32 34 3B 31 48 1B 5B 4B 1B 5B 32 35 3B K.[24;1H.[K.[25;
0220 31 48 1B 5B 4B 1B 5B 34 3B 34 31 48 1H.[K.[4;41H
21:27:33 -> 0000 64 d
21:27:34 <- 0000 64 d
21:27:34 -> 0000 69 i
21:27:34 <- 0000 69 i
21:27:34 -> 0000 72 r
21:27:34 <- 0000 72 r
21:27:34 -> 0000 0D .
21:27:35 <- 0000 1B 5B 35 3B 32 48 56 6F 6C 75 6D 65 20 69 6E 20 .[5;2HVolume in
0010 64 72 69 76 65 20 43 20 69 73 20 53 79 73 74 65 drive C is Syste
0020 6D 1B 5B 36 3B 32 48 56 6F 6C 75 6D 65 20 53 65 m.[6;2HVolume Se
0030 72 69 61 6C 20 4E 75 6D 62 65 72 20 69 73 20 39 rial Number is 9
0040 43 36 46 2D 37 44 37 42 1B 5B 38 3B 32 48 44 69 C6F-7D7B.[8;2HDi
0050 72 65 63 74 6F 72 79 20 6F 66 20 43 3A 5C 44 6F rectory of C:\Do
0060 63 75 6D 65 6E 74 73 20 61 6E 64 20 53 65 74 74 cuments and Sett
0070 69 6E 67 73 5C 41 64 6D 69 6E 69 73 74 72 61 74 ings\Administrat
0080 6F 72 1B 5B 31 30 3B 31 48 30 38 2F 32 31 2F 32 or.[10;1H08/21/2
0090 30 30 33 20 20 30 38 3A 33 30 20 50 4D 20 20 20 003 08:30 PM
00A0 20 3C 44 49 52 3E 20 20 20 20 20 20 20 20 20 20 <DIR>
00B0 2E 1B 5B 31 31 3B 31 48 30 38 2F 32 31 2F 32 30 ..[11;1H08/21/20
00C0 30 33 20 20 30 38 3A 33 30 20 50 4D 20 20 20 20 03 08:30 PM
00D0 3C 44 49 52 3E 20 20 20 20 20 20 20 20 20 20 2E <DIR> .
00E0 2E 1B 5B 31 32 3B 31 48 31 30 2F 31 30 2F 32 30 ..[12;1H10/10/20
00F0 30 33 20 20 30 36 3A 32 33 20 50 4D 20 20 20 20 03 06:23 PM
0100 3C 44 49 52 3E 20 20 20 20 20 20 20 20 20 20 44 <DIR> D
0110 65 73 6B 74 6F 70 1B 5B 31 33 3B 31 48 30 38 2F esktop.[13;1H08/
0120 30 38 2F 32 30 30 33 20 20 30 36 3A 33 33 20 50 08/2003 06:33 P
0130 4D 20 20 20 20 3C 44 49 52 3E 20 20 20 20 20 20 M <DIR>
0140 20 20 20 20 46 61 76 6F 72 69 74 65 73 1B 5B 31 Favorites.[1
0150 34 3B 31 48 30 37 2F 32 37 2F 32 30 30 33 20 20 4;1H07/27/2003
0160 31 30 3A 31 35 20 41 4D 20 20 20 20 3C 44 49 52 10:15 AM <DIR
0170 3E 20 20 20 20 20 20 20 20 20 20 4D 79 20 44 6F > My Do
0180 63 75 6D 65 6E 74 73 1B 5B 31 35 3B 31 48 30 37 cuments.[15;1H07
0190 2F 32 37 2F 32 30 30 33 20 20 30 35 3A 34 35 20 /27/2003 05:45
01A0 41 4D 20 20 20 20 3C 44 49 52 3E 20 20 20 20 20 AM <DIR>
01B0 20 20 20 20 20 53 74 61 72 74 20 4D 65 6E 75 1B Start Menu.
01C0 5B 31 36 3B 31 48 30 37 2F 32 37 2F 32 30 30 33 [16;1H07/27/2003
01D0 20 20 30 35 3A 35 30 20 41 4D 20 20 20 20 20 20 05:50 AM
01E0 20 20 20 20 20 20 20 20 20 20 20 30 20 53 74 69 0 Sti
01F0 5F 54 72 61 63 65 2E 6C 6F 67 1B 5B 31 37 3B 31 _Trace.log.[17;1
0200 36 48 31 20 46 69 6C 65 28 73 29 20 20 20 20 20 6H1 File(s)
0210 20 20 20 20 20 20 20 20 20 30 20 62 79 74 65 73 0 bytes
0220 1B 5B 31 38 3B 31 36 48 36 20 44 69 72 28 73 29 .[18;16H6 Dir(s)
0230 20 20 20 31 2C 31 38 37 2C 35 39 30 2C 31 34 34 1,187,590,144
0240 20 62 79 74 65 73 20 66 72 65 65 1B 5B 32 30 3B bytes free.[20;
0250 31 48 43 3A 5C 44 6F 63 75 6D 65 6E 74 73 20 61 1HC:\Documents a
0260 6E 64 20 53 65 74 74 69 6E 67 73 5C 41 64 6D 69 nd Settings\Admi
0270 6E 69 73 74 72 61 74 6F 72 3E nistrator>
21:27:36 -> 0000 65 e
21:27:36 <- 0000 65 e
21:27:36 -> 0000 78 x
21:27:36 <- 0000 78 x
21:27:36 -> 0000 69 i
21:27:37 <- 0000 69 i
21:27:37 -> 0000 74 t
21:27:37 <- 0000 74 t
21:27:37 -> 0000 0D .

21:27:37 Server port 23 closed.


21:27:40 Client port 23 closed.


Capture terminated: 12/10/2003 9:27:40 PM

Client Port Traffic Totals: 33 sent 107 bytes.
Server Port Traffic Totals: 22 xcvd 1,324 bytes.


All of that activity was just &quot;George&quot; the admin of &quot;BigDog&quot; logging on with his password &quot;xxx&quot; and then entering &quot;dir&quot; to see his directory, and finally entering &quot;exit&quot; to log off. Most of the weird-looking stuff is Telnet handshake going back and forth between the client program and the server. It is a discussion of emulation types to support and so on.

Some of the other &quot;gingerbread&quot; shown there is the NEXT layer protocol above even Telnet itself. This is mostly emulation control sequences, cursor control, etc. In this case I let the client default to ANSI, so we see a lot of ANSI control sequences, typically beginning ESC [ ...

What most of us want when we ask for a Telnet we can use from WSH is an ActiveX component that will deal with this background chatter with the server. We just want to set some properties and then pass text back and forth.

This is what some of the commercial component suites do. Alas, I haven't found a free one yet.


As I said, glad you have things working though. And good luck!
 
dilettante -
I think I see what you mean. I think I need to do a little quick research on just what the Telnet protocol does vs. TCP sockets. I just assumend since I was talking over port 23 that is was a telnet session. The sample code failed on the other ports I tried at least.

So far all our old DEC networking hardware seems happy with the TCP Socket communication. What sort of things have you found TCP sockets to not work on so far?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top