Jump to content

Launch Telnet and log in


 Share

Recommended Posts

I'm trying to launch a Telnet window, automatically log in, and then leave the Telnet window running. Here's my code:

$pTelnet = Run( "telnet " & $Server, @SystemDir, @SW_SHOW, $STDIN_CHILD + $STDOUT_CHILD )
$TelnetOutput = ""
While StringInStr( $TelnetOutput, "login: " ) = 0
   $TelnetOutput = StdoutRead( $pTelnet, True )
WEnd
StdinWrite( $pTelnet, $User & @CRLF )
sleep(500)
StdinWrite( $pTelnet, $Pass & @CRLF )

The Telnet windows just flashes up and disappears again, leaving the application in an infinite loop. I tried adding $RUN_CREATE_NEW_CONSOLE but that made no difference, and my AU3 application isn't a console application anyway. Any suggestions?

Update: I also tried getting the window handle of the process ID and using Send, but that didn't work either, the window handle that I found didn't have a title so I don't think it was the right handle.

Edited by PhilHibbs
Link to comment
Share on other sites

Oh, if you want to do it your way... try this :

run("cmd /c telnet [iP-ADDRESS] && pause")

Edit; But i'd really advise you to connect to the server with autoit instead of the DOS-prompt. If you want to automate things it wont be any different for the user.

Edited by notsure
Link to comment
Share on other sites

  • 3 weeks later...

Edit; But i'd really advise you to connect to the server with autoit instead of the DOS-prompt. If you want to automate things it wont be any different for the user.

No, I just want AutoIt to launch Telnet, log the user in, and leave them with a running Telnet window in a Command Prompt.
Link to comment
Share on other sites

I'm still stuck on this. Does Send() or SendKeepActive() work on command prompt windows? The following does not seem to work, I've tried using the window title instead of the handle and it's no better.

$pTelnet = run("telnet " & $Server, @SystemDir)
Sleep( 1000 )
$hTelnet = GetProcessWin( $pTelnet )
;MsgBox(0, "Done", "Handle = " & $pTelnet & ", win = " &  $hTelnet )
SendKeepActive( $hTelnet, $User & @CRLF )
Sleep( 1000 )
SendKeepActive( $hTelnet, $Pass & @CRLF )

Update: Also, "cmd /c" makes no difference

Edited by PhilHibbs
Link to comment
Share on other sites

Are you aware of the idioticness of your idea considering capabilities of the language you use?

Are you aware of what I am trying to do? I am not trying to automate an entire Telnet session. I am trying to automate the log-in step of a Telnet prompt that the user can then carry on using after my AutoIt program has terminated.

Please ignore the fact that the program that I am running is called "telnet.exe", imagine it's called "debug.exe" or "uvsh.exe" if that helps.

Link to comment
Share on other sites

  • 2 weeks later...

I successfully use this small to telnet to my routers and change passwords. I am looking for a way to handle the errors when login fails.

Dim $IP

DO

$IP=Inputbox("Input IP","IP address")

if @error = 1 then Exit

Run("cmd.exe")

sleep(1000)

send("telnet ")

sleep(1000)

Send($IP)

Send("{Enter}")

sleep(1000)

send("admin")

sleep(1000)

Send("{enter}")

sleep(1000)

send("xxxx")

sleep(1000)

Send("{enter}")

I don't know if you still need help but if you know how to handle errors please respond.

Link to comment
Share on other sites

I successfully use this small to telnet to my routers and change passwords. I am looking for a way to handle the errors when login fails.

$IP=Inputbox("Input IP","IP address")
    if @error = 1 then Exit
    Run("cmd.exe")
    sleep(1000)
    send("telnet ")
    sleep(1000)
    Send($IP)
    Send("{Enter}")
    sleep(1000)
    send("admin")
    sleep(1000)
    Send("{enter}")
    sleep(1000)
    send("xxxx")
    sleep(1000)
    Send("{enter}")

I don't know if you still need help but if you know how to handle errors please respond.

That's a bit fragile - what happens if another window pops up in that sleep(1000) delay? I can't always rely on the window title being unique, and I can't work out how to get the window handle of a Command Prompt process. This doesn't work:

$pTelnet = run("telnet " & $Server, @SystemDir)
    Sleep( 1000 )
    $hTelnet = GetProcessWin( $pTelnet )

The return value doesn't match any of the windows that WinList() returns, and nothing happens if I use it in a SendKeepActive().

Does anyone know how to launch a Command Prompt application, get the handle of the window, and send keystrokes to it? Please ignore the fact that this is Telnet and that AutoIt can do Telnet another way, this is a question about Command Prompts.

*Edit:* I think I've found a way - run WinList(), and go through all the windows looking for one whose WinGetProcess() matches the $pTelnet!

$pTelnet = run("telnet " & $Server, @SystemDir)
    Sleep( 1000 )
    $WinList = WinList()
    For $i = 1 to $WinList[0][0]
        If $WinList[$i][0] <> "" And WinGetProcess( $WinList[$i][1] ) = $pTelnet Then
            $hTelnet = $WinList[$i][1]
            ExitLoop
        EndIf
    Next
    SendKeepActive( $hTelnet )
    Send( $User & "{Enter}" )
    Sleep( 1000 )
    Send( $Pass & "{Enter}" )

*Edit:* This doesn't work either :(

*Edit:* Got it! I need to check that the WinList() entry has a window title. Code updated.

Edited by PhilHibbs
Link to comment
Share on other sites

I am looking for a way to handle the errors when login fails.

I think you are best off following notsure's advice and do the whole telnet session natively in AutoIt rather than driving a Command Prompt. Follow the link he posted earlier in this thread, that shows you how to open up a TCP session to the server which will allow you to receive and process the responses to your commands, which will contain the error messages.
Link to comment
Share on other sites

Hm, I've just had another idea on how to handle the output of a Command Prompt application... copy it to the clipboard. Alt+Space brings up the system menu, 'e' activates the Edit menu, 's' picks the "Select All" option, and {Enter} copies it all to the clipboard. Put it in a loop, and you have a "WaitFor" function! No more arbitrary delays, just tell it what to wait for, give it a timeout, and if it fails to find it, look for the error message.

$Server = "server"
$User = "user"
$Pass = "pass"
$pTelnet = run("telnet " & $Server, @SystemDir)
Sleep( 1000 )
$hTelnet = _CmdGetWindow( $pTelnet )
SendKeepActive( $hTelnet )
_CmdWaitFor( $hTelnet, "login:" )
Send( $User & "{Enter}" )
_CmdWaitFor( $hTelnet, "Password:" )
Send( $Pass & "{Enter}" )
If _CmdWaitFor( $hTelnet, $User & "@", 5000 ) Then
    Send( "cd{Enter}" )
Else
    If _CmdWaitFor( $hTelnet, "invalid login name or password", 0 ) Then
        Send( "^c" )
        MsgBox(0, "Error", "Incorrect user name or password")
    EndIf
EndIf

Func _CmdGetWindow( $pCmd )
    Local $WinList, $i
    $WinList = WinList()
    For $i = 1 to $WinList[0][0]
        If $WinList[$i][0] <> "" And WinGetProcess( $WinList[$i][1] ) = $pTelnet Then
            Return $WinList[$i][1]
        EndIf
    Next
    Return 0
EndFunc

Func _CmdWaitFor( $hWin, $text, $timeout = -1, $prefix = "" )
    Local $timer, $con

    SendKeepActive( $hWin )

    $timer = TimerInit()
    While ($timeout <= 0 Or TimerDiff($timer) < $timeout) And WinExists( $hWin )
        Send( "! es{Enter}" )
        $con = ClipGet()
        If $prefix <> "" Then
            $con = StringMid( $con, StringInStr( $con, $prefix, False, -1 ) + StringLen( $prefix ) )
        EndIf
        If StringInStr( $con, $text ) > 0 Then
            Return True
        EndIf
        If $timeout = 0 Then ExitLoop
        Sleep(1000)
    WEnd
    Return False
EndFunc

*Update:* Maybe this could be enhanced to accept an array of strings to search for, and return the index number of the one found. Oh, and it should allow regular expressions as well.

Edited by PhilHibbs
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...