Code : Tout sélectionner
#include <Constants.au3>
#Include <File.au3>
;~ Local $foo = Run(@ComSpec & " /c "&@ScriptDir&"\plink.exe -telnet 172.16.9.93", @SystemDir, @SW_HIDE, $STDIN_CHILD + $STDERR_CHILD + $STDOUT_CHILD)
Local $foo = Run(@ComSpec & " /c cmd.exe", @SystemDir, @SW_HIDE, $STDIN_CHILD + $STDERR_CHILD + $STDOUT_CHILD)
expect($foo, "Microsoft", 3)
expSend($foo, "dir")
expect($foo, "File(s)", 3)
expSend($foo, "telnet")
expect($foo, "Welcome", 3)
Func expect ($process, $expectedString, $timeout)
dbgLog ("Expect : '"&$expectedString& "', with timeout: "&$timeout, 2)
$rep_err=""
$rep_ok=""
For $i = 1 to $timeout
$rep_err &= StderrRead($process)
dbgLog ("StdoutRead $rep_err: "&$rep_err, 3)
If @error Then
MsgBox(0, "Error : STDERR read:", $rep_err)
ExitLoop
EndIf
If StringInStr($rep_err,$expectedString)<>0 Then
dbgLog ("Expected string in STDERR : '"&$expectedString& "' found", 2)
Return 1
EndIf
$rep_ok &= StdoutRead($process)
dbgLog ("StdoutRead $rep_ok: "&$rep_ok, 3)
If @error Then
MsgBox(0, "Error : STDOUT read:", $rep_err)
ExitLoop
EndIf
If StringInStr($rep_ok,$expectedString)<>0 Then
dbgLog ("Expected string : '"&$expectedString& "' found", 2)
Return 1
EndIf
Sleep (1000)
Next
dbgLog ("FAILED, Expected string '"&$expectedString&"' not found. ", 1)
dbgLog ("StdoutRead : "& $rep_ok, 1)
MsgBox(0, "", "FAILED, Expected string '"&$expectedString&"' not found. ", 2)
Return 0
EndFunc
Func expSend ($process, $inputText)
dbgLog ("expSend : "& $inputText, 2)
StdinWrite($process, $inputText & @CRLF )
StdinWrite($process)
EndFunc
Func dbgLog($text, $dbglevelAsked)
$dbglevel = 3
; Set dbglevel = 1 to have only errors messages
; Set dbglevel = 2 to have errors and warning messages
; Set dbglevel = 3 to have all DBG messages
$outputFile = "./DBG_"&@ScriptName&".txt"
if IsDeclared("firstCall") == 0 Then
$file = FileOpen($outputFile, 2)
FileClose($file)
Global $firstCall = 1
EndIf
If ($dbglevelAsked) <= $dbglevel Then
_FileWriteLog($outputFile, $text)
EndIf
EndFunc
ShellExecute("./DBG_"&@ScriptName&".txt", "", @ScriptDir, "edit")