Jump to content

Cannot read store key text from plink with StdoutRead


 Share

Recommended Posts

I have been teaching myself how to use the StdoutRead and StdinWrite commands to send and receive text from the plink program. I am trying to automate a script that logs on to a number of IP address. I was successful getting my test system to work, but then when I went to a new IP address, the script locked up. After some troubleshooting, I learned that the plink script was asking me if I wanted to store the key for the new connection (I had already done this for my test connection). If I ran plink from a command window I got this message:

"The server's host key is not cached in the registry. You have no guarantee that the server is the computer you think it is. The server's rsa2 key fingerprint is: ssh-rsa 1040. If you trust this host, enter "y" to add the key to PuTTY's cache and carry on connecting. If you want to carry on connecting just once, without adding the key to the cache, enter "n". If you do not trust this host, press Return to abandon the connection. Store key in cache? (y/n)"

I could press "y" from the command window and things would work happily and give me my prompt. However if I delete the key from my computer's registry (HKEY_CURRENT_USER\Software\SimonTatham\PuTTY\SshHostKeys) to turn it into an unknown again, the script will not get to my prompt when I run it the next time.

I attempted to look use StdoutRead to look for this the warning text and react accordingly, but the StdoutRead never sees the text. I was able to put a Sleep command and then just StdinWrite ("y" & @CR) blindly and it was able to move past the warning text. The problem is, I don't know the quality of my connection and if I don't sleep long enough it may not wait long enough or if I put too long of a sleep, the script wil take to long to run. I want to be able to positively know when I need to send the text and send it only when I have to, when I have to.

Does anyone have any idea why I am missing the text? If so could you please give me an idea on how to fix it? I have put some of my code here. It will open a console box and attempt to use plink to connect to a server. If the IP address is already in your host list, the script will take you directly to a prompt ("~ # "). However if it is not, it will just hang, waiting.

You need to have plink.exe in the folder you save the program. Set the $vsTestIP, $vsUser, and $vsPW variables at the beginning of the script to the ones you need to use for the test.

Any help would be greatly appreciated.

#include <Constants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
Local $vsTestIP = "IPADDRESS"
Local $vsUser = "USERNAME"
Local $vsPW = "PASSWORD"
Local $wConsole = GUICreate("SSH Console", 600, 330, 1, 1)
Local $hConsoleData = GUICtrlCreateInput ( "", 15, 15 , 570 , 270 , $ES_MULTILINE)
Local $bCancel = GUICtrlCreateButton("Cancel", 15, 295, 120, 25)
GUISetState(@SW_SHOW, $wConsole)
Local $vsDosCommand
Local $cLiveStatus
Local $vsLine
Local $vsLoopData
Local $vsConsoleData
Local $Msg
   ; Log on to Unit
   $vsDosCommand = "plink.exe -ssh " & $vsUser & "@" & $vsTestIP & " -pw " & $vsPW
   $cLiveStatus = Run(@ComSpec & " /c " & $vsDosCommand, "", @SW_HIDE, $STDOUT_CHILD + $STDIN_CHILD)  ; Start Plink.exe
   ; Wait for Prompt
   $vsLoopData = ""
   While Not @error
   $Msg = GUIGetMsg()
   If $Msg = $bCancel Or $Msg = $GUI_EVENT_CLOSE Then
    GUIDelete($hConsoleData)
    Exit
   EndIf
    $vsLine = StdoutRead($cLiveStatus)
    If $vsLine > "" Then
     $vsLoopData = $vsLoopData & $vsLine
ConsoleWrite("**1 $vsLine = " & @CR & ">" & $vsLine & "<" & @CR & "$vsLoopData = " & @CR & "<" & $vsLoopData & ">" & @CR & @CR)
    EndIf
    If StringInStr($vsLoopData, "Store key in cache? (y/n)") Then             ; Asking for a key
     $vsConsoleData = $vsConsoleData & $vsLoopData
     GUICtrlSetData ($hConsoleData, $vsConsoleData)
     $vsDosCommand = "y" & @CR                    ; Accept the key
     StdinWrite($cLiveStatus, $vsDosCommand)
     $vsLoopData = ""
    ElseIf StringRight($vsLoopData, 4) = "~ # " Then                 ; Got a Prompt
     $vsConsoleData = $vsConsoleData & $vsLoopData
     GUICtrlSetData ($hConsoleData, $vsConsoleData)
     $vsDosCommand = "cd /mnt/nand/0/.extracted/srv/www/timaru/js" & @CR            ; Commnand to change folder
     StdinWrite($cLiveStatus, $vsDosCommand)
     $vsLoopData = ""
     MsgBox(0,"Got Prompt", "You Betch Ya")
     ExitLoop
    EndIf
   WEnd
Link to comment
Share on other sites

  • 1 year later...

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