Jump to content

Plink Wrapper


susserj
 Share

Recommended Posts

Hello guys. I'm very new to plink. I started using it about 3 hours ago. I'm familiar with putty but want to automate some tasks on my network. I've taken the example config at the bottom of the wrapper and adjusted it accordingly to my file locations, however, plink doesn't seems to launch. After I compile the script the icon in the tray will indication that the script is paused. I accessed plink earlier with cmd, but cannot through the script provided in the OP. Would anybody here be willing to help me with this problem? I don't know if its that I don't have plink properly setup externally or what. 

Link to comment
Share on other sites

  • 5 months later...

Hi, I am trying following and need help.

-- connect to remote unix host using plink (done)

-- run a script (done)

-- capture only output of the script and ignore login banner and any other garbage (NEED HELP)

-- how to capture continuous output like tail -f filename and display it in GUI (NEED HELP)

Any help and support is appreciated.

Note: I am using plink_wrapper.au3

rgds, frowzzy

Link to comment
Share on other sites

  • 7 years later...
; #FUNCTION# ====================================================================================================================
; Name...........: _Start_plink_serial
; Description ...: open a new plink.exe terminal session
; Author ........: Marcin Przyborowski (Marcinan Barbarzynca)
; Syntax.........: $plink_serial = "COM6"
; Syntax.........: $_plinkhandle=_Start_plink_serial($plink_serial)
; Parameters ....: $_plink_loc is the location of the plink.exe ececutable on you workstation
; Parameters ....: $_plinkserver is the location of the server you wish to access
; Example........: $_plinkhandle = _Start_plink("c:/putty/plink.exe", "testserver.com")
; Return values .: $plinkhandle, pid of cmd processor
; Remarks........; Default plink.exe location is set to @ScriptDir
; Remarks .......: Needs to be made more robust
; -serial \\.\COM9 -sercfg 9600,8,1,N,N
; ===============================================================================================================================

;start the plink serial session
func _Start_plink_serial($_plink_serial_port,$_plink_loc = '"'&@ScriptDir&"\plink.exe"&'"',$bound_rate = 9600, $frame_len = 8, $stop_bits = 1,$n1 = "N", $n2 ="N" )

_Plink_close(); close any stray plink sessions before starting
 ;~     $_plink_loc=StringTrimLeft($_plink_loc,1)
;~  $_plink_loc=StringTrimRight($_plink_loc,1)
if (FileExists(StringTrimRight(StringTrimLeft($_plink_loc,1),1))) Then
    ConsoleWrite($_plink_loc&@CRLF)
Else
    MsgBox(0,"Error", "Unable to open plink.exe at:" & $_plink_loc,10)
    return false
    Exit
    EndIf

if $_plink_loc = "" then
MsgBox(0, "Error", "Unable to open plink.exe",10)
return false
Exit
endif

if $_plink_serial_port = "" then
MsgBox(0, "Error", "Undefined port",10)
Exit
return false
endif

ConsoleWrite("RUN: "&@comspec & " /c " & $_plink_loc & " -serial \\.\" & $_plink_serial_port & " -sercfg "& $bound_rate&","&$frame_len&","&$n1&","&$n2&@CRLF)
$_plinkhandle = Run(@comspec & " /c " & $_plink_loc & " -serial \\.\" & $_plink_serial_port & " -sercfg "& $bound_rate&","&$frame_len&","&$n1&","&$n2 ,"",@SW_HIDE,7)
return $_plinkhandle
endFunc
; ===============================================================================================================================

Ive Added serial com communication for yo wrapper. I hope it will be reliable

Link to comment
Share on other sites

  • 1 year later...
On 1/23/2013 at 9:01 PM, SadBunny said:

Sorry to resurrect. I was looking for a way to run a remote unix bash script from a Windows domain login (autoit-)script, and get back the output from the bash script and came to this thread by search. Looks nice!

 

But, I could not really use this UDF because it "says" things to the plink session and "waits" for a certain string to appear, even to the point of waiting for user/password prompt. Should not be necessary (is actually quite cumbersome) if you just want to run some remote command on some user@host combination and get its output in a string, as long as your remote machine accepts that kind of direct login, which should be pretty normal. plink.exe supports using username/password (or username/keyfile) direct connect session already, as BartW also demonstrated a couple of posts before.

 

Also, having to manually putty or plink to the server to allow it to push the host ssh key into the registry (to not be disrupted by the auth request - I need a full-auto login script obviously) is not an option for tens to hundreds of very limited and unpredictable office user logins. So for my purposes I proactively write the key of the machine in question to the correct place in the registry (which is in CURRENT_USER-land so should be possible from limited user elevation), so plink.exe will never ask to allow it and just do as it's told. Security note: this is not auto-acceptance of a host-key (which would kinda defeat part of the purpose of ssh), it's pre-configuring the known host key in the registry so plink.exe thinks it was accepted earlier. If that key is wrong/unknown (i.e. fake server in between?), the connect should just gracefully fail. This works well enough (and is secure enough) for our purposes.

 

I just wanted to give username/password/host/command to a function, run the command, and get the output and then quit the session. I imagine others visiting this thread might want this too. It can be easily done in just a few lines of code (though omitting some error checking). This can be easily modified for more customization possibilities (like BartW did) but for most of my cases that won't be needed. The bottom line is that this function is just one function that runs one command and feeds back all standard output that was received from the plink session. Nothing more, nothing less :)

 

Hope this helps anyone. Feel free to use this in any way you want.

 

 

Func _runSshCommand($plinkUser, $plinkHost, $command, $password = "", $timeoutInMs = 0)

$plinkFile = @UserProfileDir & "\AppData\Local\plink.exe"
FileInstall("c:\AU3_dox\plink.exe", $plinkFile, 1)

$runThis = @ComSpec & " /c " & $plinkFile & " -batch " & $plinkUser & "@" & $plinkHost
If $password <> "" Then $runThis &= " -pw " & $password
$runThis &= " " & $command

$plinkHandle = Run($runThis, "", @SW_HIDE, $STDIN_CHILD + $STDOUT_CHILD + $STDERR_CHILD)

$plinkFeedback = ""
$waitForOutputStartTime = TimerInit()
Do
Sleep(10)
$plinkFeedback &= StdoutRead($plinkHandle)
$stdoutReadError = @error
Until $stdoutReadError Or ($timeoutInMs And TimerDiff($waitForOutputStartTime) > $timeoutInMs)

If ProcessExists("plink.exe") Then ProcessClose("plink.exe")

If $stdoutReadError <> 0 Then Return $plinkFeedback

MsgBox(16, "Timeout", "Timeout while waiting for plink command output!")
EndFunc ;==>_runSshCommand

 

Usage example:

 

#include <Constants.au3>

Global $unixUser = "user"
Global $unixPassword = "password"
Global $unixHost = "unixhost"
Global $unixHostKey = "0x23,0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef"

_writePuttyKnownHostKeyToRegistry($unixHost, $unixHostKey)

$sshCommand = "ls -la /tmp"


$answer = _runSshCommand($unixUser, $unixHost, $sshCommand, $unixPassword)
If $answer <> "" Then
MsgBox(0, "output", $answer)
EndIf

Exit

Func _writePuttyKnownHostKeyToRegistry($host, $hostKey)
RegWrite("HKEY_CURRENT_USER\Software\SimonTatham\PuTTY\SshHostKeys", "rsa2@22:" & $host, "REG_SZ", $hostKey)
EndFunc ;==>_writePuttyKnownHostKeyToRegistry

 

Note: to learn what unixHostKey cache entry to write to the registry on machines that have not connected to the unix box before (thus preventing the interactive 'store this key in cache' prompt from plink), do one manual session from your own machine, allow it to store the key, then look in HKEY_CURRENT_USERSoftwareSimonTathamPuTTYSshHostKeys and just copypaste the whole thing into the RegWrite function call.

 

Thank you very much for this...
this helps a lot !

Link to comment
Share on other sites

  • Developers
1 minute ago, nicknuke said:

Thank you very much for this...
this helps a lot !

Please do not resurrect Old threads for  thank-you post! There is an LIKE option at the bottom right of each post, so please use that!

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

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