Jump to content
Sign in to follow this  
DrStankly

Telnet in background

Recommended Posts

DrStankly

I have been trying forever to make a script that will automatically log into a telnet device send a few commands and record the output.

I got one script that uses Putty and a configuration that is saved in Putty. It works great, but since it uses the GUI it can only be run when I am logged in and my comp is not locked.

My goal is to create a script that can do this:

1) Log into a Cisco device via telnet

2) Pull a bunch of show commands

3) Clear the counters

4) log all input and output in a file that is named with the device name, time, and date.

5) Has the ability to run as a service or run as a scheduled task when I am not around.

Share this post


Link to post
Share on other sites
DrStankly

Thank you very much with your help in putting this script together. This morning was the first day I ran this script in production and I noticed an odd problem.

When I run this script manually on my desktop or on the server the output is completely fine. Even when I was testing this script earlier the output was fine.

User Access Verification

Username: ********

Password:

ILWCRTR01#show clock

08:35:35.521 CDT Tue Oct 14 2008

ILWCRTR01#

ILWCRTR01#show int s0/0/0:0

But last night when I let this run automatically it put out this for each one of my T-1s connected. For some odd reason it changed the colon at the end on int s0/0/0:0 to a semi-colon.

User Access Verification

Username: eanderson

Password:

ILWCRTR01#show clock

00:00:02.008 CDT Tue Oct 14 2008

ILWCRTR01#

ILWCRTR01#show int s0/0/0;0

^

% Invalid input detected at '^' marker.

Anyone have any ideas on why this would happen?

This is part of my code:

$Telnetserver = "192.168.251.44"
$Telnetport = "" ; "" for default

; next #include is not nessecary. You may comment it out.
;#include "_SingleScript.au3" ; http://www.autoitscript.com/forum/index.php?showtopic=72070
If Not HotKeySet("{ESC}", "OnAutoItExit") Then MsgBox(48 + 262144, "Warning", "Termination hotkey {ESC} not set for " & @ScriptName & " ", 10)
Func OnAutoItStart()
    ;MsgBox(64, "Start", @ScriptName & " started.", 3)
EndFunc   ;==>OnAutoItStart


Global $Filename = "c:\Logs\ILWCRTR01\" & "Telnet." & $Telnetserver & "[" & @HOUR & "." & @MIN & "." & @SEC & "]" &".log"
$Cmd = "telnet -f " & $Filename & " " & $Telnetserver & " " & $Telnetport
$pid = Run($Cmd, "", @SW_HIDE)
If @error Then Exit MsgBox(0, "Error PID", $pid)
Sleep(1000) ; allow process to start
Global $handle = WinGetHandle("Telnet " & $Telnetserver)
WinActivate($handle)
WinWait($handle)

ControlSend($handle, "", "", "******{enter}")
ControlSend($handle, "", "", "******{enter}")
ControlSend($handle, "", "", "show clock{enter}{enter}")

ControlSend($handle, "", "", "show int s0/0/0:0{enter}{enter}")
ControlSend($handle, "", "", "show int s0/0/1:0{enter}{enter}")
ControlSend($handle, "", "", "show int s0/1/0:0{enter}{enter}")
ControlSend($handle, "", "", "show int s0/1/1:0{enter}{enter}")
ControlSend($handle, "", "", "show int s0/2/0:0{enter}{enter}")
ControlSend($handle, "", "", "show int s0/2/1:0{enter}{enter}")

ControlSend($handle, "", "", "show int MFR0{enter}{space}{enter}{enter}")
ControlSend($handle, "", "", "show int MFR0.501{enter}{enter}")
ControlSend($handle, "", "", "show int MFR0.502{enter}{enter}")

ControlSend($handle, "", "", "show controllers T1{enter}{space}{space}{space}{space}{space}{space}{enter}{enter}")

ControlSend($handle, "", "", "show policy-map interface{enter}{space}{space}{space}{space}{space}{space}{enter}{enter}")

ControlSend($handle, "", "", "show ip int brief{enter}{space}{enter}{enter}")
    
ControlSend($handle, "", "", "show log{enter}{space}{space}{space}{space}{space}{enter}")

ControlSend($handle, "", "", "show clock{enter}{enter}")

ControlSend($handle, "", "", "clear counters{enter}{enter}{enter}")

ControlSend($handle, "", "", "exit{enter}")

Func OnAutoItExit()
    If Not IsDeclared("@exitMethod") Then Exit ;  Exit by Hotkey
    ProcessClose($pid)
    WinClose($handle)
    ;FileDelete($Filename)
    ;MsgBox(64, "", @ScriptName & " ended.", 3)
    Exit
EndFunc   ;==>OnAutoItExit

Share this post


Link to post
Share on other sites
scriptjunkie

In the failure code, you have a ";" semicolon which is not a valid command.

ILWCRTR01#show int s0/0/0;0

Edited by scriptjunkie

Share this post


Link to post
Share on other sites
leos

Why not using native socket connections like following example.

I am using this algorithm to communicate with my router (Linux machine) for sending commands and receiving info from it.

Be sure to replace specific information.

;BEGIN SCRIPT----------------------------------------------

;simulates a TELNET connection

Global $host, $port, $delay, $user, $pass, $sk, $ack

$host = "192.168.1.1"

$port = 23

; miliseconds to wait before sending/receiving something

$delay = 300 ; compensates network/internet delays

$user = ""

$pass = ""

TCPStartup()

$sk = TCPConnect($host, $port)

;if invalid socket

If $sk = -1 Then

MsgBox(0, "", "Cannot connect")

Exit

EndIf

; message from device

Sleep($delay)

$ack = TCPRecv($sk, 150)

TrayTip("", $ack, 10)

; verifying the correct answer

If StringInStr($ack, "wl500gp login") >= 1 Then

; if the string is correct then we send user

Sleep($delay)

TCPSend($sk, $user & @CRLF)

Sleep($delay)

$ack = TCPRecv($sk, 80)

TrayTip("", $ack, 10)

; and password

Sleep($delay)

TCPSend($sk, $pass & @CRLF)

Sleep($delay)

$ack = TCPRecv($sk, 80)

TrayTip("", $ack, 10)

Else

MsgBox(0, "", "Cannot connect")

Exit

EndIf

; verifying the answer of the device (the prompt that it returns after receiving user and password)

If StringInStr($ack, "root]$") = 0 Then ; attention, this string is specific

MsgBox(0, "Error", "Access denied")

Exit

EndIf

; now you can send some commands

Sleep($delay)

TCPSend($sk, "ps" & @CRLF)

Sleep($delay)

$ack = TCPRecv($sk, 2000)

MsgBox(0,"Running processes", $ack)

TCPCloseSocket($sk)

TCPShutdown()

Exit

;END SCRIPT----------------------------------------------

Share this post


Link to post
Share on other sites
DrStankly

Hubertus thank you very much for pointing the obvious that I had missed. A good laugh is always worth while.

Are you suggesting that I will be able to send a shift command with the ; to make sure that a : is put into the command?

Once again thank you for school this newb :P

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×