Jump to content
Sign in to follow this  
bigdogdan2

Help with using plink to connect to a pc

Recommended Posts

bigdogdan2

Hi, I'm new to autoit so I thought I would start with some of the examples already here.  I'm trying to use plink to connect to a linux pc and run a few commands, but I am having trouble.

Im using the plink wrapper and script from plink-wrapper:

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

Joel's commands have been cut out for copy and paste length.  When I run the code, I just hangs or goes in an infinite loop and never seems to log in.  More info below.

; #VARIABLES# ===================================================================
global $_plinkhandle="" ; used to handle starting of plink
global $_plinkserver="10.206.156.22" ; name of server you wish to connect to
global $_plink_loc ="c:/plink.exe" ; location of plink.exe executable on workstation
global $_plink_display_messages=false ; display interim messages to screen (default false)
global $_plink_display_message_time=1 ; time in seconds messages are displayed
global $_plink_logging=false ; record plink log file (default false)
global $_plink_logfile="c:/plink.log" ; location of plink log file
global $_plink_logfile_handle="" ; plink log file handle
; ===============================================================================

func _Plink_close()
;If there are any stray plink sessions kill them
if ProcessExists("plink.exe") then
ProcessClose("plink.exe")
Else
return false
endif

EndFunc
; ===============================================================================

func _Start_plink($_plink_loc,$_plinkserver)
_Plink_close(); close any stray plink sessions before starting
if $_plink_loc = "" then
MsgBox(0, "Error", "Unable to open plink.exe",10)
return false
Exit
endif

if $_plinkserver = "" then
MsgBox(0, "Error", "Unable to open server",10)
Exit
return false
endif

$_plinkhandle = Run(@comspec & " /c" & $_plink_loc & " -ssh " & $_plinkserver,"",@SW_HIDE,7)
return $_plinkhandle
endFunc
; ===============================================================================

func _Init_plink_log($_plink_logfile)
$_plink_logfile_handle = FileOpen($_plink_logfile, 2)
; Check if file opened for writing OK
If $_plink_logfile_handle = -1 Then
MsgBox(0, "Error", "Unable to open file.")
Exit
EndIf
return true
endfunc
; ===============================================================================
func _Expect($match_text)
local $text
local $found
While 1
$text = StdoutRead($_plinkhandle)
if $_plink_logging Then
filewriteline($_plink_logfile_handle,"**********************NEW SECTION************************")
filewrite($_plink_logfile_handle,$match_text)
filewrite($_plink_logfile_handle,$text)
filewriteline($_plink_logfile_handle,"**********************END SECTION************************")
endif
$found = StringRegExp($text,$match_text)
If $found = 1 Then
If $_plink_display_messages Then MsgBox(4096, $match_text, $text, $_plink_display_message_time)
ExitLoop
endif
sleep(100)
Wend
EndFunc
; ===============================================================================

func _SayPlus($output)
StdinWrite($_plinkhandle, $output & @CR)
endfunc
; ===============================================================================

;Close log file
func _Plink_close_logfile($_plink_logfile_handle)
FileClose($_plink_logfile_handle)
endfunc
; ===============================================================================

;Initialize Variables------------------------------------------------------------------------------------------------------------
$username="dan " ; It seems necessary to put an extra space after the login name. The last character is being cut off.
$password="dandan "; It seems necessary to put an extra space after the password. The last characters is being cut off.
$_plink_logging=true ; Turn Logging ON (default is off)
$_plink_display_messages=false ; Turn Screen Messages ON (default is on)
$_plink_display_message_time=5 ; Display message for 5 secs (default 1 sec)
; ===============================================================================

; Initizations-------------------------------------------------------------------------------------------------------------------
$_plinkhandle=_Start_plink("c:\plink.exe","10.206.156.22"); Initialized plink connection to server
_Init_plink_log("c:\plink.log"); Initialize log file. Required even it not being used

; Terminal Emulation Session Interaction
_Expect("login as:")
_SayPlus($username)
_Expect("Password:")
_SayPlus($password)
_Expect("megatron")
_SayPlus("ls")
_Expect("megatron")
_SayPlus("exit")

;SHUTDOWN-----------------------------------------------------------------------------------------------------------------------
_Plink_close(); shutdown plink session
_Plink_close_logfile($_plink_logfile_handle);shutdown log file
; ===============================================================================

When I look at plink.log, all I see is:

login as:**********************END SECTION************************
**********************NEW SECTION************************
login as:**********************END SECTION************************
**********************NEW SECTION************************
login as:login as: **********************END SECTION************************
**********************NEW SECTION************************
Password:**********************END SECTION************************
**********************NEW SECTION************************
Password:**********************END SECTION************************
**********************NEW SECTION************************
Password:**********************END SECTION************************

If I try to use plink to log in via the command prompt, it looks like this:

C:>plink.exe -ssh 10.206.156.22
login as: dan
dan@10.206.156.22's password:
Last login: Mon Feb 10 10:15:16 2014 from 10.206.153.162
dan@megatron:~[dan@megatron ~]$

Have any of you sucessfully run Joel's scripts?  The functions are the same above, the only I modified was the variables and the initialization part.

thanks!

Dan

Edited by bigdogdan2

Share this post


Link to post
Share on other sites
AdamUL

I have not used the Plink Wrapper UDF, but I have used similar functions.  Have a look at >this example thread.  It is connecting to an iMac, and sending a command, but it should help you with what you are trying to do.  

 

Adam

Share this post


Link to post
Share on other sites
AdamUL

Your welcome.  Glad I could help.  

 

Adam

Share this post


Link to post
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
Sign in to follow this  

×