Jump to content

plink connection to remote machine, how to capture StdoutRead outputs as a variable


taylansan
 Share

Recommended Posts

Dear Autoit Forum,

Before I start, I have checked the following topics, but couldn't get far enough:

'?do=embed' frameborder='0' data-embedContent>>

Func _PLINK_Connect($remote, $user, $password)
   Local $hSessionPID = Run("plink.exe -ssh " & $remote & " -l " & $user & " -pw " & $password, "", @SW_HIDE, $STDIN_CHILD + $STDOUT_CHILD)
   If @error Then
      MsgBox(0, "Error: xxx", "Running plink.exe under the main folder failed.")
      Return False
   EndIf
   
   Local $sLine
   While True
      ;read each line
      $sLine = StdoutRead($hSessionPID)
      ;ty_DEBUG print each line after connected
      If $sLine <> "" Then ConsoleWrite("Current Line Start:" & $sLine & @CRLF & "==== Current Line End" & @CRLF)
      If ProcessExists($hSessionPID) = 0 Then
         ConsoleWrite("cannot find PID" & @CRLF)
         Return SetError(9)
      ;check if connected with the given user name
      ElseIf StringInStr($sLine, "Using keyboard") Then
         ConsoleWrite("cannot login" & @CRLF)
         Return SetError(8)
      ;check if connected to remote
      ElseIf StringInStr($sLine, " [cdr2db] :") Then
         ConsoleWrite("connected to remote" & @CRLF)
         ExitLoop
      EndIf
      Sleep(10)
   WEnd
   
   Return $hSessionPID
EndFunc

Now i call the function:

Case1: Everything is fine

_PLINK_Connect("10.25.0.20", "cdr2db", "Cdr2db_1")
Output:
Current Line Start:Last login: Sat Sep 13 12:56:06 2014 from 10.75.58.71


==== Current Line End
Current Line Start:<101 bb1a [cdr2db] :/onip/app/cdr2db>
==== Current Line End
connected to remote

Case2: Another username
_PLINK_Connect("10.25.0.20", "cdr2dba", "Cdr2db_1")
Output:
Current Line Start:Using keyboard-interactive authentication.
Password:
==== Current Line End
cannot login
Using username "cdr2dba".
Access denied

Case3: Wrong password
_PLINK_Connect("10.25.0.20", "cdr2db", "Cdr2db_1a")
Output:
Current Line Start:Using keyboard-interactive authentication.
Password:
==== Current Line End
cannot login
Using username "cdr2db".
Access denied

Case4: Non-existing remote machine
Output:
FATAL ERROR: Network error: Connection timed out
cannot find PID

Now my question is that:

For Case 2 and 3; my last console write says that "current line start / end" then, "cannot login". But in the output window i can find "Using username cdr2db. Access denied". So, how is that output generated?

For Case 4: Even there is no output as ConsoleWrite, how the "FATAL ERROR: ..." line is generated?

I would apperiate any comments on the topic.

Thanks in advance.

TY.

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

×
×
  • Create New...