Jump to content

Run SSH Command


Recommended Posts

I'm trying to check on the status of my Raspberry Pi that's currently running PiHole (a DNS server). I found the SSH command and thought it would be simple enough to write a script to SSH into my Pi, execute 'pihole status', and parse the output. I'm just getting started and running into issues already. Here's my current script:

#AutoIt3Wrapper_UseX64=y

#include <WinAPIFiles.au3>
#include <AutoItConstants.au3>

Global Const $__g_sRasPiIP = "192.168.0.105"

PiHoleStatus()
If @error Then ConsoleWrite("PiHoleStatus failed: " & @error & " Extended: " & @extended & @CRLF)

Func PiHoleStatus()

    _WinAPI_Wow64EnableWow64FsRedirection(False)

    Local $iPid = Run("ssh -l pi " & $__g_sRasPiIP, "", @SW_SHOW, $STDIN_CHILD + $STDOUT_CHILD)
    If @error Then Return SetError(1, 0, False)

    Local $sPassword = "Se@doggie1" ; Not really though
    StdinWrite($iPid, $sPassword & @CRLF)

    Local $sOutput

    While True
        $sOutput = StdoutRead($iPid)
        If @error Then ExitLoop

        If $sOutput <> "" Then ConsoleWrite($sOutput & @CRLF)
    WEnd

EndFunc

The Wow64Redirection line was required for me until I added UseX64, as I think the ssh command is x64.

It doesn't capture the output and when I attempt to write my password in, it doesn't do anything. I've tried adding delay between starting the command and entering the password. I've tried running it using @ComSpec. I even tried using #AutoIt3Wrapper_Change2CUI=y.

Currently, a command prompt shows up and asks me to login to my pi: "pi@192.168.0.105's password:" then loops forever, while this is currently printed to the output: "Pseudo-terminal will not be allocated because stdin is not a terminal."

All my code provided is Public Domain... but it may not work. ;) Use it, change it, break it, whatever you want.

Spoiler

My Humble Contributions:
Personal Function Documentation - A personal HelpFile for your functions
Acro.au3 UDF - Automating Acrobat Pro
ToDo Finder - Find #ToDo: lines in your scripts
UI-SimpleWrappers UDF - Use UI Automation more Simply-er
KeePass UDF - Automate KeePass, a password manager
InputBoxes - Simple Input boxes for various variable types

Link to comment
Share on other sites

You shouldn't be using a password. Use an SSH key instead. A quick guide on how to do it can be found here: https://serverfault.com/questions/241588/how-to-automate-ssh-login-with-password

 

Quote

If you already have an SSH key, you can skip this step... Just hit ENTER for the key and both passphrases:

$ ssh-keygen -t rsa -b 2048
Generating public/private rsa key pair.
Enter file in which to save the key (/home/username/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/username/.ssh/id_rsa.
Your public key has been saved in /home/username/.ssh/id_rsa.pub.

Copy your keys to the target server:

$ ssh-copy-id id@server
id@server's password:

Now try logging into the machine, with ssh 'id@server', and check-in:

.ssh/authorized_keys

Note: If you don't have .ssh dir and authorized_keys file, you need to create it first to make sure we haven't added extra keys that you weren't expecting.

Finally, check to log in...

$ ssh id@server

id@server:~$

 

 

Edited by Leendert-Jan
Added the contents of the link
Link to comment
Share on other sites

6 hours ago, Leendert-Jan said:

You shouldn't be using a password

To quote Steve Bennett (from that link), "This isn't an answer to the question." Yes, I'd like to understand more about SSH keys eventually, but until I do, they're no more secure as I don't understand how they work and my Pi isn't configured for them yet. I was looking into creating an SSH key and hope to do this later.

Unless I'm recalling incorrectly, after specifying a SSH key, I'll be again prompted for a password for the SSH key, for which I'll likely run into the same issue.

I'd really like to know how to pass the password from my script to the SSH command

All my code provided is Public Domain... but it may not work. ;) Use it, change it, break it, whatever you want.

Spoiler

My Humble Contributions:
Personal Function Documentation - A personal HelpFile for your functions
Acro.au3 UDF - Automating Acrobat Pro
ToDo Finder - Find #ToDo: lines in your scripts
UI-SimpleWrappers UDF - Use UI Automation more Simply-er
KeePass UDF - Automate KeePass, a password manager
InputBoxes - Simple Input boxes for various variable types

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