Jump to content

Recommended Posts

Posted

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

Posted

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

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
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
Posted

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

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
Posted

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]

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
  • 14 years later...
Posted (edited)

@notsureThanks your example code helps me a lot, I am trying to create a code to remote control APC PDU to turn specific outlet port on/off, so I just based on your code to modify:

#Include <INet.Au3>
TcpStartUp ()

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

While 1
    sleep ('100');
    $Recv1 = TCPRecv($remoteserver, "1000");1000 means max stringlen.

    $Username = "apc"
    $password = "apc"

    consolewrite($recv1);what the server replies
    If stringinstr($recv1, "User Name :") > 0 Then
        tcpsend($remoteserver, $Username & @crlf)
    Elseif stringinstr($recv1, "Password  :") > 0 then
        tcpsend($remoteserver, $password & @crlf)
    Elseif stringinstr($recv1, "apc>") > 0 then
        tcpsend($remoteserver, "olOff 5" & @crlf)
        sleep ('100')
        tcpsend($remoteserver, "olOff 6" & @crlf)
        sleep ('100')
        tcpsend($remoteserver, "quit" & @crlf)
    Elseif stringinstr($recv1, "Connection to host lost") > 0 then
        msgbox(16,"error","Connection lost to host")
    EndIf
Wend

It works for me, but the question is, how to jump out from the 'While' loop when it is done?

I have tried the 'Exit' and 'ExitLoop' but it would make the code failed to run.

Say that, after 'quit' command executed from console then the telnet connection will be terminated with 'Connection Closed - Bye' message.

image.png.3a3f9a6228d45c0c762e3b64b602be58.png

So I'm trying to add a code that if the 'Connection Closed - Bye' detected then exit the loop, but tried the ExitLoop or Exit, both are failed.

#Include <INet.Au3>
TcpStartUp ()

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

While 1
    sleep ('100');
    $Recv1 = TCPRecv($remoteserver, "1000");1000 means max stringlen.

    $Username = "apc"
    $password = "apc"

    consolewrite($recv1);what the server replies
    If stringinstr($recv1, "User Name :") > 0 Then
        tcpsend($remoteserver, $Username & @crlf)
    ElseIf stringinstr($recv1, "Password  :") > 0 then
        tcpsend($remoteserver, $password & @crlf)
    ElseIf stringinstr($recv1, "apc>") > 0 then
        tcpsend($remoteserver, "olOn 5" & @crlf)
        sleep ('100')
        tcpsend($remoteserver, "olOn 6" & @crlf)
        sleep ('100')
        tcpsend($remoteserver, "quit" & @crlf)
    ElseIf stringinstr($recv1, "Connection Closed - Bye") > 0 then
        ????? ; What command to exit the loop???
    #ElseIf stringinstr($recv1, "Connection to host lost") > 0 then
    #   msgbox(16,"error","Connection lost to host")
    EndIf
Wend

 

Edited by jackylee0908

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
  • Recently Browsing   0 members

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