Jump to content

Telnet login to HP switch


svss
 Share

Recommended Posts

Hi there,

i have the following script whci works most of the time for me...

;##################################

;LOGIN Telnet

;#################################

Dim $delay = 0, $ip = "x.x.x.x", $passwd = "xxxxxx"

FileDelete("C:\xxxx.txt")

$delay = Ping($ip) + Ping($ip) + Ping($ip) ;this delay is to be sure that it is enough time to get the prompt back from the switch

Run("telnet "&$ip)

Sleep(100 + $delay)

ControlSend("Telnet", "", "","admin"&@CR)

Sleep(100 + $delay)

ControlSend("Telnet", "", "",$passwd&@CR)

ControlSend("Telnet", "", "",'copy command-output'&@CR)

ControlSend("Telnet", "", "","exit"&@CR)

ControlSend("Telnet", "", "","yes"&@CR)

ControlSend("Telnet", "", "","exit"&@CR)

ControlSend("Telnet", "", "","yes"&@CR)

WinClose("Telnet")

But sometimes the switch comes back with

Connection to host lost

so the script won`t work anymore i have to manually login to the box and

close the cmd box.

Is there a way to react to this message?

Like sending a "enter" command in this case and start the telnet session again?

Thanks for your help

Sven

Link to comment
Share on other sites

Dont use the telnet from windows, build you own client.

#Include <INet.Au3>
TcpStartUp ()

$remoteserver = tcpconnect(TCPNameToIP("www.yourveryniceserver.com"), "23") ;client 2 TELNETserver, leave "TCPNameToIP" out if you have IP only. 23 is telnetport.
Do
    ; do till connected to that server.
sleep(100)
until $remoteserver <> "-1"
While 1
Sleep ('100');
$Recv1 = TCPRecv($remoteserver, "1000");1000 means max stringlen. 

;KnockKnock Who's There?
$Username = "Boo"
$password = "BooWho?" ; Cry baby :D

consolewrite($recv1);what the server replies
if stringinstr($recv1, "Enter your username") > 0 Then ;Depending what the server replies.. will probably be different in your case.
    tcpsend($remoteserver, $Username & @crlf)
elseif stringinstr($recv1, "Enter your password") > 0 then
    tcpsend($remoteserver, $password & @crlf)
elseif stringinstr($recv1, "Connection to host lost") > 0 then
    msgbox(16,"error","Connection lost to host");or any other code what to do.
EndIf
Wend

I think this is more the way you want to code this. If you have questions or something, shoot. I'm very experienced with network programming. This is alot better coz you can manipulate the in- and catch the output alot more accurate.

Edited by notsure
Link to comment
Share on other sites

Dont use the telnet from windows, build you own client.

I think this is more the way you want to code this. If you have questions or something, shoot. I'm very experienced with network programming. This is alot better coz you can manipulate the in- and catch the output alot more accurate.

I think this is wrong (can be neverending loop):

$remoteserver = tcpconnect(TCPNameToIP("www.yourveryniceserver.com"), "23") 
Do
  ; do till connected to that server.
  sleep(100)
until $remoteserver <> "-1"

It should be

$remoteserver = tcpconnect(TCPNameToIP("www.yourveryniceserver.com"), "23") 
if $remoteserver = -1 Then Exit

EDIT: here is more accurate example

#737516

Edited by Zedna
Link to comment
Share on other sites

Thanks a lot that is almost working for me.

I altered a wee bit since somtimes the switch asked

for username and password :mellow:

Ok is the Exit the right way to close the socket?

Also when the message comes up 'Connection to host lost'

how can i loop to the initial login again?

Thanks a million

Sven

#Include <INet.Au3>

TcpStartUp ()

$remoteserver = tcpconnect("X.X.X.X", "23") ;client 2 TELNETserver, leave "TCPNameToIP" out if you have IP only. 23 is telnetport.

Do

; do till connected to that server.

sleep(100)

until $remoteserver <> "-1"

While 1

Sleep ('100');

$Recv1 = TCPRecv($remoteserver, "1000");1000 means max stringlen.

;KnockKnock Who's There?

$Username ='xxxx'

$password ='xxxx' ; Cry baby :(

consolewrite($Recv1);what the server replies

if stringinstr($Recv1, "Username:") > 0 Then ;Depending what the server replies.. will probably be different in your case.

;msgbox(16,"Username","Username prompt received")

TCPSend($remoteserver, $Username & @CRLF)

TCPSend($remoteserver, $password & @CRLF)

Sleep(500)

TCPSend($remoteserver, 'copy command-output ' & @CRLF)

Exit

ElseIf stringinstr($Recv1, "Password:") > 0 then

;msgbox(16,"passwd","passwd prompt received")

Tcpsend($remoteserver, $password & @CRLF)

Sleep(500)

TCPSend($remoteserver, 'copy command-output' & @CRLF)

Exit

elseif stringinstr($Recv1, "Connection to host lost") > 0 then

msgbox(16,"error","Connection lost to host");or any other code what to do.

EndIf

Wend

Link to comment
Share on other sites

Glad you are progressing mate.

You should put your code inside tags.

Quote any reply where you see this to see how

#include "AutoItTags"
If _Include_tags() Then
   _code_looks_like_this()
EndIf
Exit

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Link to comment
Share on other sites

EDIT: here is more accurate example

http://www.autoitscript.com/forum/index.php?showtopic=104198&view=findpost&p=737516

Lol, that's also my post.

You're right about the loop tho, but i was only making a fast example for this :mellow:

Thanks a lot that is almost working for me.

I altered a wee bit since somtimes the switch asked

for username and password :(

Ok is the Exit the right way to close the socket?

Also when the message comes up 'Connection to host lost'

how can i loop to the initial login again?

Thanks a million

Sven

TCPCloseSocket ( socket )

And NP, glad i can help.

Edited by notsure
Link to comment
Share on other sites

I'm just going to point this out because it's bugging me.

This is wrong.

elseif stringinstr($Recv1, "Connection to host lost") > 0 then

If you lose connection to the host how is the host going to send you "Connection to host is lost"? When you see "Connection to host is lost" in telnet that's literally telnet telling you that it lost connection to the host.

[size="1"]Please stop confusing "how to" with "how do"[/size]

Link to comment
Share on other sites

I'm just going to point this out because it's bugging me.

This is wrong.

elseif stringinstr($Recv1, "Connection to host lost") > 0 then

If you lose connection to the host how is the host going to send you "Connection to host is lost"? When you see "Connection to host is lost" in telnet that's literally telnet telling you that it lost connection to the host.

Hehe, you're right... ill reply later to this.

I'm trying to make them too fast, those examples, which gives errors :mellow:

Edited by notsure
Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...