Jump to content
Sign in to follow this  

Interacting with a remote computer via SSH

Recommended Posts


I'm trying to interact with some programs on a remote linux server via SSH in a script. I have plink, which seems to be the way to do so. I can log in and get a shell with plink at the command line. My issue is I really need to be able to have autoit type in commands, and then parse the output. When I try that with plink, I get as much as "Using username foo" and then I get the handle is invalid. . . it looks like plink is creating a new PID?

Is there a better way to do this? I can send commands to a PuTTY window also, but I can't read out the data from there. . .

Share this post

Link to post
Share on other sites

Plink is the way to go.

I don't know what have you been using but I certainly remember about a post in Example Scripts, where somebody wrote an UDF for putty/plink.

Try that one - it might solve your problem.

If you already used that, consider posting some code - otherwise you are asking for generic answers.

SNMP_UDF ... for SNMPv1 and v2c so far, GetBulk and a new example script

wannabe "Unbeatable" Tic-Tac-Toe

Paper-Scissor-Rock ... try to beat it anyway :)

Share this post

Link to post
Share on other sites

I've managed to almost get this working with the plink UDF. My problem is I need to forward a port... I try using Run w/ plink

Run("plink " & $username & "@computer -pw " & $password & ' -L 159'& $vnc & ':computer:59'&$vnc,"",@SW_SHOW,0x8)

but it seems like plink is immediately exiting for me there. . . not keeping the port open.

I'm not really sure what I'm doing wrong...

Share this post

Link to post
Share on other sites

I use plink for an automated SCP copy script. Here is some snippet functions that may be a good example for you.

Func _Connect($host,$usr,$pass)
    $exec = @ScriptDir & "\PLINK.EXE"
    If Not FileExists($exec) Then _Err("PLINK.EXE Not Found!",0)
    ;If $debug Then
        ;$pid = Run($exec & " -ssh -pw " & $pass & " " & $usr & "@" & $host, @SystemDir, @SW_SHOW, 0x1 + 0x8)  ;Run SSH.EXE
        $pid = Run($exec & " -ssh -pw " & $pass & " " & $usr & "@" & $host, @SystemDir, "", 0x1 + 0x8)  ;Run SSH.EXE
    If Not $pid Then _Err("Failed to connect",0)
    $currentpid = $pid
    $rtn = _Read($pid)  ;Check for Login Success - Prompt
    If StringInstr($rtn,"(y/n)") Then
        _Send($pid,"y" & @CR)
        $rtn = _Read($pid)
    If StringInstr($rtn,"yes/no") Then
        _Send($pid,"yes" & @CR)
        $rtn = _Read($pid)
    If StringInstr($rtn,"Access denied") Or StringInstr($rtn,"FATAL")Then _Err($rtn,$pid)
    Return $pid

Func _Read($pid)
    If Not $pid Then Return -1
    Local $dataA
    Local $dataB
        $dataB = $dataA
        $dataA &= StdOutRead($pid)
        If @error Then ExitLoop
    Until $dataB = $dataA And $dataA And $dataB
    ;If $debug Then FileWriteLine(@ScriptDir & "\log.txt",$dataA & @CRLF)
    Return $dataA

Func _Send($pid,$cmd)

Func _Err($data,$pid)
    If $data And $data <> -1 Then MsgBox(0,"An Error has Occured",$data,5)

Func _Exit($pid)

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  


Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.