svss Posted February 16, 2010 Posted February 16, 2010 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
Steveiwonder Posted February 16, 2010 Posted February 16, 2010 (edited) You should take a look at StdinWrite() StdoutRead() These will allow you to interract with your CMD window Edited February 16, 2010 by Steveiwonder They call me MrRegExpMan
svss Posted February 16, 2010 Author Posted February 16, 2010 Thanks, had a look at these but so far nothing worked for me.First time i touched them Cheers Sven
Steveiwonder Posted February 16, 2010 Posted February 16, 2010 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
svss Posted February 17, 2010 Author Posted February 17, 2010 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
notsure Posted February 17, 2010 Posted February 17, 2010 (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 February 17, 2010 by notsure
Zedna Posted February 17, 2010 Posted February 17, 2010 (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 February 17, 2010 by Zedna Resources UDF ResourcesEx UDF AutoIt Forum Search
svss Posted February 17, 2010 Author Posted February 17, 2010 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 #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
JohnOne Posted February 17, 2010 Posted February 17, 2010 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.
notsure Posted February 17, 2010 Posted February 17, 2010 (edited) EDIT: here is more accurate examplehttp://www.autoitscript.com/forum/index.php?showtopic=104198&view=findpost&p=737516Lol, that's also my post.You're right about the loop tho, but i was only making a fast example for this Thanks a lot that is almost working for me.I altered a wee bit since somtimes the switch askedfor 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 millionSvenTCPCloseSocket ( socket )And NP, glad i can help. Edited February 17, 2010 by notsure
Delta Posted February 18, 2010 Posted February 18, 2010 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]
notsure Posted February 18, 2010 Posted February 18, 2010 (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 Edited February 18, 2010 by notsure
jackylee0908 Posted 19 hours ago Posted 19 hours ago (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. 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 19 hours ago by jackylee0908
argumentum Posted 17 hours ago Posted 17 hours ago I answered back in your thread Follow the link to my code contribution ( and other things too ). FAQ - Please Read Before Posting.
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now