Sign in to follow this  
Followers 0
svss

Telnet login to HP switch

12 posts in this topic

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

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

You should take a look at

StdinWrite()
StdoutRead()

These will allow you to interract with your CMD window

Edited by Steveiwonder

They call me MrRegExpMan

Share this post


Link to post
Share on other sites

Thanks,

had a look at these but so far nothing worked

for me.First time i touched them :mellow:

Cheers

Sven

Share this post


Link to post
Share on other sites

I don't know how to use this and fail each time, maybe some of the guru's here can help you out


They call me MrRegExpMan

Share this post


Link to post
Share on other sites

thanks for the reply...

seems we are in the same boat.

I don`t really understand how to use STDOUT in this case.

Hope someone can give me some clues.

Thanks

Sven

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

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

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

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

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

#10 ·  Posted (edited)

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

Share this post


Link to post
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]

Share this post


Link to post
Share on other sites

#12 ·  Posted (edited)

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

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  
Followers 0