joebro391
Posts: 18
Joined: Thu Aug 05, 2021 3:46 am

How to Return a Calculated Value to Local Machine via SSH

Thu Aug 05, 2021 3:57 am

Hello All,

I just had a random idea and am curious to whether or not it's feasable.

I have a Pi on my local network. I SSH into it frequently and created a simple shellscript that returns the temperature of the CPU in Fahrenheit by entering the command "temp". Very simple, it's in a bin folder that is in my $PATH and can be called from anywhere on the Pi.

My question is, what would I have to do get that value returned to my local machine without SSHing into the Pi? For example, calling an alias on my local machine, such as "PiTemp" that would ssh into the Pi, run the command on the Pi and then return the value to my local machine.

I can definitely create an alias that would call a .bat file that would run through all of those steps, but in regards to returning the value from the Pi, to my local machine...that I do not know how to do. Any ideas?

One way would be to record the value to a .csv file on the Pi and then PSCP the file from the Pi, to my local machine, and then read the value. That would definitely work. But is there a simplier, more elegant way?

Many thanks!

-Joe

jayben
Posts: 328
Joined: Mon Aug 19, 2019 9:56 pm

Re: How to Return a Calculated Value to Local Machine via SSH

Thu Aug 05, 2021 7:58 am

A Python program using paramiko?

User avatar
B.Goode
Posts: 12792
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: How to Return a Calculated Value to Local Machine via SSH

Thu Aug 05, 2021 8:04 am

If ssh works I don't see the motivation to not use it.

MQTT might be a useable alternative. (But initially more complex to implement.)

ame
Posts: 3557
Joined: Sat Aug 18, 2012 1:21 am
Location: New Zealand

Re: How to Return a Calculated Value to Local Machine via SSH

Thu Aug 05, 2021 8:44 am

Rather than using SSH to log in to the Pi and execute your script from the command line, just use SSH to execute the script and return the result. No need to log in.

Here's an example and a discussion:
https://unix.stackexchange.com/question ... er-via-ssh

GlowInTheDark
Posts: 1946
Joined: Sat Nov 09, 2019 12:14 pm

Re: How to Return a Calculated Value to Local Machine via SSH

Thu Aug 05, 2021 8:54 am

Try this:

Code: Select all

$ ssh otherhost cat /sys/class/thermal/thermal_zone0/temp | awk '{ print "The temperature on that host is:",$0/1000,"degrees C." }' 
There are, of course, other ways, using any of the popular remote mounting protocols (e.g., sshfs) to get access to that file on the remote host.
Poster of inconvenient truths.

Linux zealot and proud of it.

joebro391
Posts: 18
Joined: Thu Aug 05, 2021 3:46 am

Re: How to Return a Calculated Value to Local Machine via SSH

Thu Aug 05, 2021 1:09 pm

ame wrote:
Thu Aug 05, 2021 8:44 am
Rather than using SSH to log in to the Pi and execute your script from the command line, just use SSH to execute the script and return the result. No need to log in.

Here's an example and a discussion:
https://unix.stackexchange.com/question ... er-via-ssh
So this actually works marvelously.

Code: Select all

$ ssh pi@<IP> ./bin/temp | awk '{print $1, $2, $3, $4, $5, $6, $7}'
pi@<IP>'s password:
The CPU temp is 120.92 degrees F.
One thing to note is that I was not able to call "temp", but rather had to enter the full path. That's not a problem, especially if I make this an alias, but any idea why that might be? When I first just had "temp", I got the 'ol "bash: temp: command not found". Perhaps the system doesn't check the full $PATH because I'm not really logged in? Curious.

NOTE: I also realized through tinkering around that I don't need the pipe and the AWK command since I'm fine with receiving the full return statement.

Thank you so much! This is cool.

-Joe

GlowInTheDark
Posts: 1946
Joined: Sat Nov 09, 2019 12:14 pm

Re: How to Return a Calculated Value to Local Machine via SSH

Thu Aug 05, 2021 2:25 pm

It is kind of like cron. People always get thrown by the fact that they don't get their usual login environment when they run stuff via cron.

When you run something via "ssh host command", the command gets run in an austere environment similar to what you get with cron. In particular, $HOME/bin is not on your PATH.

Anyway, you don't need to write ./bin/temp; you could just use bin/temp.
Poster of inconvenient truths.

Linux zealot and proud of it.

joebro391
Posts: 18
Joined: Thu Aug 05, 2021 3:46 am

Re: How to Return a Calculated Value to Local Machine via SSH

Thu Aug 05, 2021 3:41 pm

GlowInTheDark wrote:
Thu Aug 05, 2021 2:25 pm
It is kind of like cron. People always get thrown by the fact that they don't get their usual login environment when they run stuff via cron.

When you run something via "ssh host command", the command gets run in an austere environment similar to what you get with cron. In particular, $HOME/bin is not on your PATH.

Anyway, you don't need to write ./bin/temp; you could just use bin/temp.
Thank you! Cheers

Return to “Other projects”