Jump to content

Recommended Posts

Posted (edited)

Hi community

In order to fine treat the latency of the devices I am connecting on, I'd like to send everything separately on Plink.exe

I used untill now ssh.udf, but this Library shows its limits and can stress at a high point that I'd like to avoid

So, here's the code that runs fine untill I have to type the login

(I successfully send 'y' key for preliminar questions while login such as "Continue with connection? (y/n)" or "Store key in cache? (y/n)" )

Can't define why the login isn't typed

Any help appreciated

$SSH_Process_Id = Run(@ScriptDir & "\plink.exe -v -ssh -P 22 " & $IP_Adress, @ScriptDir, @SW_HIDE, $STDIN_CHILD + $STDOUT_CHILD + $STDERR_CHILD )
While 1
    $Data_Out &= StdoutRead($SSH_Process_Id)
    $Data_Err &= StderrRead($SSH_Process_Id)
    If StringInStr($Data_Err, "Continue with connection? (y/n)") > 0 Or StringInStr($Data_Err, "Store key in cache? (y/n)") > 0  Then
        StdinWrite($SSH_Process_Id, "y" & @CR)
    ElseIf StringInStr($Data_Out, "login as:") > 0 Then
        ExitLoop
    EndIf
WEnd
StdinWrite($SSH_Process_Id, $Login & @CR)       ; <- Here's the problem, nothing seems to be sent
While 1
    ToolTip($Data_Out,0,0)
    $Data_Out &= StdoutRead($SSH_Process_Id)
    $Data_Err &= StderrRead($SSH_Process_Id)
    If StringInStr($Data_Out, "password:") > 0 Then
        ExitLoop
    EndIf
WEnd

The problem seems relative @CR

 But don't really understand the "trick" involved here ...

Regards

 

Edited by Ebola57
Posted (edited)

StdinWrite returns the number of characters written. I suggest you  trap for @error after that statement and also msgbox yourself the return value so you at least know what it thinks it is doing.

I would question whether @CR is sufficient (not @LF or @CRLF) but as it appears to have worked for the previous line I guess you are OK (In the past I have had issues with UNIX requiring a LineFeed and windows requiring both).

I notice in the linked topic you added they are adding a space after the login, but I am surprised that works, let alone that it is required....

Edit: Double check the @LF option - perhaps your @CR is from the local machine prior to connection....

Edited by SlackerAl

Problem solving step 1: Write a simple, self-contained, running, replicator of your problem.

Posted
1 hour ago, SlackerAl said:

StdinWrite returns the number of characters written. I suggest you  trap for @error after that statement and also msgbox yourself the return value so you at least know what it thinks it is doing.

Not a bad suggestion.  I have personally see that for some "unknown" reason, StdinWrite has not sent any characters.  So what I did, is I put the statement into a do loop until the number of chars equals the number of characters I wanted to send.  Since then, it worked perfectly.

  • 1 month later...
Posted (edited)

After further investigation ... and holidays ^^

The @CR has nothing to do after typing the 'y' key. In fact Plink juster wait for a 'y' or 'n' key press, uselesse to send àCR, so the problem is not relative to @CR

I followed both of your suggestions and it looks like the number of sent chars is correct (the string + 1 for @CR )

$SSH_Process_Id = Run(@ScriptDir & "\plink.exe -v -ssh -P 22 " & $IP_Adress, @ScriptDir, @SW_HIDE, 7) ; $STDIN_CHILD + $STDOUT_CHILD + $STDERR_CHILD
While 1
    $Data_Out &= StdoutRead($SSH_Process_Id)
    $Data_Err &= StderrRead($SSH_Process_Id)
    ToolTip($Data_Out & @CR & "[" &$Data_Err & "]",0,0)
    If StringInStr($Data_Err, "Continue with connection? (y/n)") > 0 Or StringInStr($Data_Err, "Store key in cache? (y/n)") > 0  Then
        StdinWrite($SSH_Process_Id, "y")
    ElseIf StringInStr($Data_Out, "login as:") > 0 Then
        ExitLoop
    EndIf
WEnd

Do
    $NbSent = StdinWrite($SSH_Process_Id, $Login & @CR)       ; <- Here's the problem, nothing seems to be sent
Until $NbSent == StringLen($Login & @CR)

While 1
    $Data_Out &= StdoutRead($SSH_Process_Id)
    $Data_Err &= StderrRead($SSH_Process_Id)
    ToolTip($Data_Out & @CR & "[" &$Data_Err & "]",0,0)
    If StringInStr($Data_Out, "password:") > 0 Then
        ExitLoop
    EndIf
WEnd

Output stream stays definitely stuck at waiting login

Did you implement the statement like this ? (in the loop)

Here's my output below (ip and rsa masked)

login as: 
[Looking up host "ip_adress" for SSH connection
Connecting to ip_adress port 22
We claim version: SSH-2.0-PuTTY_Release_0.72
Remote version: SSH-2.0-OpenSSH_6.0
We believe remote version has SSH-2 channel request bug
Using SSH protocol version 2
No GSSAPI security context available
Doing ECDH key exchange with curve nistp256 and hash SHA-256 (unaccelerated)
Server also has ecdsa-sha2-nistp256/ssh-dss host keys, but we don t know any of them
Host key fingerprint is:
ssh-rsa 2048 rsa_key
Initialised AES-256 SDCTR (AES-NI accelerated) outbound encryption
Initialised HMAC-SHA-256 (unaccelerated) outbound MAC algorithm
Initialised AES-256 SDCTR (AES-NI accelerated) inbound encryption
Initialised HMAC-SHA-256 (unaccelerated) inbound MAC algorithm
]

 

Edited by Ebola57
Posted

here is the problem :-

1-

$Data_Err &= StderrRead($SSH_Process_Id)

so variable $data_err will always have "Store key in cache" string , so 

If StringInStr($Data_Err, "Continue with connection? (y/n)") > 0 Or StringInStr($Data_Err, "Store key in cache? (y/n)") > 0  Then
        StdinWrite($SSH_Process_Id, "y")

the first if statement will always be true , and the  
 

ElseIf StringInStr($Data_Out, "login as:") > 0 Then
        ExitLoop
    EndIf

will never be executed so your code just spam "y"

2-you need to use @crlf after each command not CR alone

Posted

Thanks for your reply :)

Even if you did not point the real problem, you gave me the key

- The 'y' statement perform well, I tested it on a 'virgin' device

- The @CRLF was indeed the real problem, patching it solved my problem

Here's the final working code

Global $IP_Adress = "Your IP"
Global $Data_Out = ""
Global $Data_Err = ""
Global $Login = "Your Login"
Global $Password = "Your password"
 
$SSH_Process_Id = Run(@ScriptDir & "\plink.exe -v -ssh -P 22 " & $IP_Adress, @ScriptDir, @SW_HIDE, 7) ; $STDIN_CHILD + $STDOUT_CHILD + $STDERR_CHILD
While 1
    $Data_Out &= StdoutRead($SSH_Process_Id)
    $Data_Err &= StderrRead($SSH_Process_Id)
    ToolTip($Data_Out & @CR & "[" &$Data_Err & "]",0,0)
    If StringInStr($Data_Err, "Continue with connection? (y/n)") > 0 Or StringInStr($Data_Err, "Store key in cache? (y/n)") > 0  Then
        StdinWrite($SSH_Process_Id, "y")
    ElseIf StringInStr($Data_Out, "login as:") > 0 Then
        StdinWrite($SSH_Process_Id, $Login & @CRLF)
        ExitLoop
    EndIf
    Sleep(100)
WEnd

While 1
    $Data_Out &= StdoutRead($SSH_Process_Id)
    $Data_Err &= StderrRead($SSH_Process_Id)
    ToolTip($Data_Out & @CR & "[" &$Data_Err & "]",0,0)
    If StringInStr($Data_Out, "password:") > 0 Then
        StdinWrite($SSH_Process_Id, $Password & @CRLF)
        ExitLoop
    EndIf   
WEnd

 

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
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...