Jump to content

ExtraPuTTY using dll for Telnet,SSH,Rlogin


ahmed9100
 Share

Recommended Posts

im trying but no hope

$Dll = "C:\Program Files\ExtraPuTTY\Bin\ExtraPuTTY.dll"
$a = DllCall($Dll, "Chr", "Connexion", "long", "10.0.0.138", "Chr", "admin", "59455", "BOOL", "long","FALSE" , "0", "long", "23", "long", "0", "int", "1", "long", "2^1")
msgbox("","",@error & "   " & $a)

;~ int Connexion(char *TargetName,unsigned long *ConnectionId,char *Login,char *Password,bool ShowPuTTY,long Protocol,unsigned long PortNumber,
;~               long GenerateReport,int *CallBackRcvData,unsigned long SpecSettings);
Link to comment
Share on other sites

what are u mean with this > .telnettelnet.exe 

 

what the (.) mean

 

"." means current directory.

If you are executing your script from for example C:AutoItTests, that line ".telnettelnet.exe" will try to execute the telnet program from C:AutoItTeststelnettelnet.exe

Link to comment
Share on other sites

i figure it out i made a folder called telnet and but the telnet.exe on it

but the telnet black screen popup and closed !

did i do something wrong ?

and i have a log in name here it just the ip and the password

Edited by ahmed9100
Link to comment
Share on other sites

Hi ahmed9100

as sahsanu said the the ".telnettelnet.exe" is the same as @scriptdir & "telnettelnet.exe"

the telnet directory contains telnet.exe and the others files from the installation directory of the consoletelnet program.

If you use the example I posted, the "black screen" shouldn't appear at all because of the @SW_HIDE parameter on the run command

the telnet.exe start in hidden mode and you use his functionality via StdinWrite and StdoutRead commands.

if you have written your own code, and you can post here (remove username and passwords) we can have a look at it.

Edited by PincoPanco

 

image.jpeg.9f1a974c98e9f77d824b358729b089b0.jpeg Chimp

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Link to comment
Share on other sites

i didnt make this part of code yet im now looking for a telnet code to use it

i will connect to linux server

log by ip user pass

and send a command from gui

working step :

1 - sent telnet command to import file in server

2 - get this file from ftp and use it

i got the ftp udf and its working great the problem is in the send telnet 

found putty in the form but sometime didnt send command i make a sleep(10000) to make it work but i need it to work fast so i start looking for another telnet code

here is putty code if u can make it work fast :

$pid return strang string so i make a ftp code to check the file if exist on the server

#Region _Connect
Func _Connect($host,$usr,$pass,$command)
    $exec = @ScriptDir & "\putty.EXE"
    $pid = Run($exec & " -ssh -pw " & $pass & " " & $usr & "@" & $host,"",@SW_HIDE);
    sleep(1000)
    GUICtrlSetData($Progress1,10)
    sleep(1000)
    GUICtrlSetData($Progress1,20)
        sleep(1000)
    GUICtrlSetData($Progress1,30)
        sleep(1000)
    GUICtrlSetData($Progress1,40)
        sleep(1000)
    GUICtrlSetData($Progress1,50)
        sleep(1000)
    GUICtrlSetData($Progress1,60)
        sleep(1000)
    GUICtrlSetData($Progress1,70)
        sleep(1000)
    GUICtrlSetData($Progress1,80)
        sleep(1000)
    GUICtrlSetData($Progress1,90)
        sleep(1000)
    GUICtrlSetData($Progress1,100)
    ControlSend($host&" - PuTTY", "", "", $command)
    Return $pid
EndFunc
#EndRegion
Edited by ahmed9100
Link to comment
Share on other sites

Hi ahmed9100
I have modified my example and transformed it in some functions (not tested),
you should include the functions (copy - paste) in your code and use:
Telnet_connect($host, $usr, $pass)   to connect to the remote terminal (use it at the begin of your program)

Telnet_send($command)                   to send a command to the remote terminal

                                                          this function returns the output you get from your command

Telnet_quit()                                       to quit current connection and quit telnet.exe (use it at the end of your program)

#include <Constants.au3>
#autoit3wrapper_usex64=n ; runs the script in 32 bit program mode
Global $tn ; this will be used as global handle to telnet.exe


; ---------------------------------------------
; put this functions at the end of your program
; ---------------------------------------------

Func Telnet_connect($host, $usr, $pass)
    ; -----------------------------------------------------------------------------------------------
    ; call this at the beginning of your script to open a connection to the client and to autenticate
    ; and leave it opened, so it is ready to be used in the program any time you need it
    ; -----------------------------------------------------------------------------------------------
    ; http://www.autoitscript.com/forum/topic/148340-solvedhow-run-a-telnet-from-script/?hl=%2Btelnet#entry1054557
    ; Use False to disable redirection, it will only apply to the program if running as 32 bit process
    _Wow64FsRedirection(False)
    Global $tn = Run(".\telnet\telnet.exe", ".\telnet", @SW_HIDE, $STDIN_CHILD + $STDOUT_CHILD)
    _Wow64FsRedirection(True)
    If Not $tn Then
        ConsoleWrite("!Error starting Telnet. (error " & @error & ") End.")
        Return 0 ; returns 0 if not connected
    EndIf
    Sleep(2000) ; wait a moment
    ; waiting telnet prompt
    ; this prompt is from the telnet.exe program, not from the remote terminal
    ; when this prompt is received from StdoutRead the telnet client is ready
    While Not StringInStr(_ReadTerminal($tn), "telnet>", 2, -1) ; is prompt ready?
        Sleep(500)
    WEnd

    ; this command sends the command "open" to connect to the remote terminal
    StdinWrite($tn, "open " & $host & @CR)

    ; here we wait the response from the remote terminal that in THIS CASE is expected to be
    ; the string "Enter username". You must check this with the response you get from
    ; your terminal when you connect to it and you receive the request of the username.
    While Not StringInStr(_ReadTerminal($tn), "Enter username", 2) ; wait rq username string
        Sleep(500)
    WEnd

    ; here we send the username to the remote terminal
    StdinWrite($tn, $usr & @CR)

    ; here we wait the response from the remote terminal that in THIS CASE is expected to be
    ; the string "Enter password". You must check this with the response you get from
    ; your terminal when you connect to it and you receive the request of the password.
    While Not StringInStr(_ReadTerminal($tn), "Enter password", 2) ; wait rq pwd string
        Sleep(500)
    WEnd

    ; here we send the password to the remote terminal
    StdinWrite($tn, $pass & @CR)
    ; -----------------------------------------------------------------------------------------------
    ; end of connection and autentication to remote terminal via telnet.exe
    ; -----------------------------------------------------------------------------------------------
    Return 1 ; returns 1 if connected and autenticated
EndFunc   ;==>Telnet_connect

Func Telnet_send($command)

    ; here we send the command to the remote terminal
    StdinWrite($tn, $command & @CR)

    ; return the response received from the remote terminal to the caller
    Return _ReadTerminal($tn)

EndFunc   ;==>Telnet_send

Func Telnet_quit() ; this sequence will logoff from remote terminal
    StdinWrite($tn, "close" & @CR) ; close current connection
    StdinWrite($tn, "quit" & @CR) ; exit telnet
EndFunc   ;==>Telnet_quit

; http://www.autoitscript.com/forum/topic/111647-macro-problem-in-win7-x64/#entry790037
; read the above link for explanation of this
Func _Wow64FsRedirection($state)
    ; Disables or reverts the filesystem redirector for a 32 bit process running on 64bit OS
    If Not @AutoItX64 And @OSArch = 'X64' Then
        If $state Then
            DllCall("kernel32.dll", "int", "Wow64RevertWow64FsRedirection", "int", 0)
        Else
            DllCall("kernel32.dll", "int", "Wow64DisableWow64FsRedirection", "int", 0); or 1 as per help
        EndIf
        If @error Then Return SetError(1)
    EndIf
EndFunc   ;==>_Wow64FsRedirection

; retrieves the output from the terminal
; and return it to the caller
Func _ReadTerminal($Terminal)
    Local $sRecv = ""
    Local $RecvBuffer = ""

    $sRecv = StdoutRead($Terminal, False, False) ; first part of receiving data goes in $Recv (if any)
    While 1 ; read more data if available
        $RecvBuffer = StdoutRead($Terminal, False, False) ; is there more data ?
        If $RecvBuffer = "" Then ; <--- if no more data to read then exit
            ExitLoop
        EndIf
        $sRecv = $sRecv & $RecvBuffer; append new data to $Recv
        ; !!! if a very (too) long message is received then autoit goes out of memory here !!!
        ; do to: chech if overflow then truncate input and disconnect client
        Sleep(10)
    WEnd
    ; remove chr(0) if present otherwise data following the 0 is lost
    ; ConsoleWrite(StringReplace($sRecv, Chr(0), "") & @CRLF)
    Return $sRecv
EndFunc   ;==>_ReadTerminal

I do not know wich prompt you receive from remote terminal in request of username and password

so you have to check lines 47 and 57 to use proper string in the StringInString() statement

my terminal for example requires only the password without username so in this case you should delete

the lines from 44 to 52 that waits for the username request

it is a first shot test to be verified, but I think it should work.

let us know the outcome

Edited by PincoPanco

 

image.jpeg.9f1a974c98e9f77d824b358729b089b0.jpeg Chimp

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Link to comment
Share on other sites

nothing happen so i changed @SW_HIDE to @SW_SHOW to see what happen Telnet open and close lees than 1 sec

and if i open it manually from start run telnet work

so i changed Run(".telnettelnet.exe", ".telnet" to Run("C:\Windows\System32\telnet.exe", "C:\Windows\System32\" and stll the same

maybe its a [ win 7 - 32 ] problem ?

Link to comment
Share on other sites

I use it also on WIN7 x64

have you started telnet.exe using my Telnet_connect() function or by using only the run command?

the _Wow64FsRedirection() function must be used as this:

_Wow64FsRedirection(False)
    $tn = Run(".\telnet\telnet.exe", ".\telnet", @SW_HIDE, $STDIN_CHILD + $STDOUT_CHILD)
    _Wow64FsRedirection(True)
Edited by PincoPanco

 

image.jpeg.9f1a974c98e9f77d824b358729b089b0.jpeg Chimp

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Link to comment
Share on other sites

I used it like thet

Telnet_connect("10.0.0.138", "admin", "12345")
Func Telnet_connect($host, $usr, $pass)

    ; -----------------------------------------------------------------------------------------------

    ; call this at the beginning of your script to open a connection to the client and to autenticate

    ; and leave it opened, so it is ready to be used in the program any time you need it

    ; -----------------------------------------------------------------------------------------------

    ; http://www.autoitscript.com/forum/topic/148340-solvedhow-run-a-telnet-from-script/?hl=%2Btelnet#entry1054557

    ; Use False to disable redirection, it will only apply to the program if running as 32 bit process

    _Wow64FsRedirection(False)
    $tn = Run(".\telnet\telnet.exe", ".\telnet", @SW_SHOW, $STDIN_CHILD + $STDOUT_CHILD)
    _Wow64FsRedirection(True)

    If Not $tn Then

        ConsoleWrite("!Error starting Telnet. (error " & @error & ") End.")

        Return 0 ; returns 0 if not connected

    EndIf

    Sleep(2000) ; wait a moment
    ; waiting telnet prompt
    ; this prompt is from the telnet.exe program, not from the remote terminal
    ; when this prompt is received from StdoutRead the telnet client is ready
    While Not StringInStr(_ReadTerminal($tn), "telnet>", 2, -1) ; is prompt ready?
        Sleep(500)
    WEnd



    ; this command sends the command "open" to connect to the remote terminal
    StdinWrite($tn, "open " & $host & @CR)

    ; here we wait the response from the remote terminal that in THIS CASE is expected to be
    ; the string "Enter username". You must check this with the response you get from
    ; your terminal when you connect to it and you receive the request of the username.

    While Not StringInStr(_ReadTerminal($tn), "Enter username", 2) ; wait rq username string

        Sleep(500)

    WEnd



    ; here we send the username to the remote terminal

    StdinWrite($tn, $usr & @CR)



    ; here we wait the response from the remote terminal that in THIS CASE is expected to be
    ; the string "Enter password". You must check this with the response you get from
    ; your terminal when you connect to it and you receive the request of the password.

    While Not StringInStr(_ReadTerminal($tn), "Enter password", 2) ; wait rq pwd string

        Sleep(500)

    WEnd



    ; here we send the password to the remote terminal

    StdinWrite($tn, $pass & @CR)

    ; -----------------------------------------------------------------------------------------------

    ; end of connection and autentication to remote terminal via telnet.exe

    ; -----------------------------------------------------------------------------------------------

    Return 1 ; returns 1 if connected and autenticated

EndFunc   ;==>Telnet_connect
Link to comment
Share on other sites

Hi ahmed9100

I have modified a little the  function  _ReadTerminal() with some timeout checking

important: inside the telnet directory there is a file called telnet.ini,
you have to control inside that file the presence of this parameter: Telnet_Redir=1
without the ;
to test, if you run this script it should display a msgbox with the output of the telnet local command 'help'

#include <Constants.au3>
#autoit3wrapper_usex64=n ; runs the script in 32 bit program mode
Global $tn ; this will be used as global handle to telnet.exe


Telnet_connect() ; if called without parameters it only start telnet.exe
MsgBox(0, "Output for telnet 'help' command", Telnet_send('help'))
Telnet_quit() ; close telnet.exe

; ---------------------------------------------
; put this functions at the end of your program
; ---------------------------------------------

Func Telnet_connect($host = "", $usr = "", $pass = "")
    ; -----------------------------------------------------------------------------------------------
    ; call this at the beginning of your script to open a connection to the client and to autenticate
    ; and leave it opened, so it is ready to be used in the program any time you need it
    ; -----------------------------------------------------------------------------------------------
    ; http://www.autoitscript.com/forum/topic/148340-solvedhow-run-a-telnet-from-script/?hl=%2Btelnet#entry1054557
    ; Use False to disable redirection, it will only apply to the program if running as 32 bit process
    _Wow64FsRedirection(False)
    Global $tn = Run(".\telnet\telnet.exe", ".\telnet", @SW_HIDE, $STDIN_CHILD + $STDOUT_CHILD)
    _Wow64FsRedirection(True)
    If Not $tn Then
        ConsoleWrite("!Error starting Telnet. (error " & @error & ") End.")
        Return 0 ; returns 0 if not connected
    EndIf
    Sleep(2000) ; wait a moment
    ; waiting telnet prompt
    ; this prompt is from the telnet.exe program, not from the remote terminal
    ; when this prompt is received from StdoutRead the telnet client is ready
    While Not StringInStr(_ReadTerminal($tn), "telnet>", 2, -1) ; is prompt ready?
        Sleep(500)
    WEnd
    If $host <> "" And $usr <> "" And $pass <> "" Then
        ; this command sends the command "open" to connect to the remote terminal
        StdinWrite($tn, "open " & $host & @CR)

        ; here we wait the response from the remote terminal that in THIS CASE is expected to be
        ; the string "Enter username". You must check this with the response you get from
        ; your terminal when you connect to it and you receive the request of the username.
        While Not StringInStr(_ReadTerminal($tn), "Enter username", 2) ; wait rq username string
            Sleep(500)
        WEnd

        ; here we send the username to the remote terminal
        StdinWrite($tn, $usr & @CR)

        ; here we wait the response from the remote terminal that in THIS CASE is expected to be
        ; the string "Enter password". You must check this with the response you get from
        ; your terminal when you connect to it and you receive the request of the password.
        While Not StringInStr(_ReadTerminal($tn), "Enter password", 2) ; wait rq pwd string
            Sleep(500)
        WEnd

        ; here we send the password to the remote terminal
        StdinWrite($tn, $pass & @CR)
    EndIf

    ; -----------------------------------------------------------------------------------------------
    ; end of connection and autentication to remote terminal via telnet.exe
    ; -----------------------------------------------------------------------------------------------
    Return 1 ; returns 1 if connected and autenticated
EndFunc   ;==>Telnet_connect

Func Telnet_send($command)

    ; here we send the command to the remote terminal
    StdinWrite($tn, $command & @CR)

    ; return the response received from the remote terminal to the caller
    Return _ReadTerminal($tn)

EndFunc   ;==>Telnet_send

Func Telnet_quit() ; this sequence will logoff from remote terminal
    StdinWrite($tn, "close" & @CR) ; close current connection
    StdinWrite($tn, "quit" & @CR) ; exit telnet
EndFunc   ;==>Telnet_quit

; http://www.autoitscript.com/forum/topic/111647-macro-problem-in-win7-x64/#entry790037
; read the above link for explanation of this
Func _Wow64FsRedirection($state)
    ; Disables or reverts the filesystem redirector for a 32 bit process running on 64bit OS
    If Not @AutoItX64 And @OSArch = 'X64' Then
        If $state Then
            DllCall("kernel32.dll", "int", "Wow64RevertWow64FsRedirection", "int", 0)
        Else
            DllCall("kernel32.dll", "int", "Wow64DisableWow64FsRedirection", "int", 0); or 1 as per help
        EndIf
        If @error Then Return SetError(1)
    EndIf
EndFunc   ;==>_Wow64FsRedirection

; retrieves the output from the terminal
; and return it to the caller
Func _ReadTerminal($Terminal)
    Local $sRecv = ""
    Local $RecvBuffer = ""
    Local $Timeout = TimerInit()
    Sleep(100)
    While 1
        $sRecv = StdoutRead($Terminal, False, False) ; first part of receiving data goes in $Recv (if any)
        While 1 ; read more data if available
            Sleep(150) ; give some time to telnet to build response ; <-- fine tunung delay here
            $RecvBuffer = StdoutRead($Terminal, False, False) ; is there more data to read?
            If $RecvBuffer = "" Then ; <--- if no more data to read then exit
                ExitLoop
            EndIf
            $sRecv &= $RecvBuffer; append new data to $Recv
            ; !!! if a very (too) long message is received then autoit goes out of memory here !!!
            ; do to: chech if overflow then truncate input and disconnect client
        WEnd
        If $sRecv <> "" Then ; Got response, return it to caller
            Return $sRecv
        ElseIf TimerDiff($Timeout) > 3000 Then ; if no response received from telnet.exe within 3 seconds
            ConsoleWrite("timeout" & TimerDiff($Timeout) & @CRLF)
            SetError(1, 0, "")
            ExitLoop
        EndIf
    WEnd
EndFunc   ;==>_ReadTerminal

let us know the outcome

bye

 

image.jpeg.9f1a974c98e9f77d824b358729b089b0.jpeg Chimp

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Link to comment
Share on other sites

>"C:\Program Files\AutoIt3\SciTE\AutoIt3Wrapper\AutoIt3Wrapper.exe" /run /prod /ErrorStdOut /in "C:UsersMasterDesktopNew AutoIt v3 Script (3).au3" /UserParams    
+>20:25:06 Starting AutoIt3Wrapper v.2.1.0.33    Environment(Language:0409  Keyboard:00000409  OS:WIN_7/Service Pack 1  CPU:X64 OS:X86)
>Running AU3Check (1.54.22.0)  from:C:Program FilesAutoIt3
+>20:25:06 AU3Check ended.rc:0
>Running:(3.3.8.1):C:Program FilesAutoIt3autoit3.exe "C:UsersMasterDesktopNew AutoIt v3 Script (3).au3"    
--> Press Ctrl+Alt+F5 to Restart or Ctrl+Break to Stop
timeout3118.97024371119
timeout3113.1023900339
timeout3147.13179419773
timeout3113.09355810965
timeout3113.12619782973
timeout3101.08137312614
timeout3114.07159772198
timeout3145.92527654483
timeout3128.12971715955
+>20:25:43 AutoIT3.exe ended.rc:0
>Exit code: 0    Time: 39.696
 
 
 
Here is the outcome
Link to comment
Share on other sites

i searched for the telnet.ini and found it and i butted it beside the telnet.exe and i changed the Value

[Terminal]
;Dumpfile=
;Term=ansi
;Telnet_Redir=1
;Input_Redir=0
;Output_Redir=0
;Strip_Redir=FALSE
;Destructive_Backspace=FALSE
;Speaker_Beep=TRUE
;Beep=TRUE
;EightBit_Ansi=True
;VT100_Mode=True
;Disable_Break=FALSE
;Preserve_Colors=FALSE
;Wrap_Line=TRUE
;Fast_Write=TRUE
;Term_Width=-1
;Term_Height=-1
;Wide_Enable=FALSE
;Buffer_Size=2048
;XDispLoc=TRUE
 
but still the same  :sweating:  i believe some how that's ur code is ok and the problem in my windows system
Link to comment
Share on other sites

 

important: inside the telnet directory there is a file called telnet.ini,

you have to control inside that file the presence of this parameter: Telnet_Redir=1

without the ;  <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

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