Here is the problemYou have your network at home, with several machines connected to it (laptops, tablets, Raspberry PIs, phones, etc). Your home network is a Local Area Network (aka LAN), the machines can see each other, but they cannot be seen from outside, from the Internet.
You may very well want to deal with those machines while away from home, to restart services, launch a new program, or even reboot.
In the configuration mentioned above, this is simple, you just cannot do it. And that is frustrating!
There is a way though. Those machines on your home LAN can send and receive emails...
JavaMailis a Java package that has been available for ever, it understands the email protocols (IMAP, POP3, SMTP, etc), and can be used to interact with email accounts programmatically.
An exampleThere is an example of such an interaction on this github repository.
The fastest way to get it running is to run the following commands (these are for Linux - and MacOS - on Windows, use the git shell):
$ git clone https://github.com/OlivierLD/raspberry-pi4j-samples.git $ cd raspberry-pi4j-samples $ cd common-utils $ ../gradlew shadowJar $ cp email.properties.sample email.properties $ vi email.properties $ # Here you modify your properties file to match your email account $ java -cp ./build/libs/common-utils-1.0-all.jar email.examples.EmailWatcher -send:google -receive:googleThe
-send:google -receive:googledepends on the settings in your
Then, to the account mentioned in the
email.properties, send a message like this:
Subject: execute Content: whoami ifconfig uname -aNote: this example requires the content to be in
Once the message is received by the
EmailWatcher, it sends you an acknowledgement:
EmailWatcher, you would see in its console an output like that:
Start receiving. Received: whoami ifconfig uname -a Operation: [execute], sent for processing... pi lo0: flags=8049And finally, you receive an email like that:
mtu 16384 options=1203 inet 127.0.0.1 netmask 0xff000000 inet6 ::1 prefixlen 128 ...
CommentsThis process is not synchronous, this could ba drawback... But still, it allows you to interact remotely with machines unvisible from the Internet.
Having the command
java -cp ./build/libs/common-utils-1.0-all.jar email.examples.EmailWatcher -send:google -receive:googlefired when the machine boots will allow you make sure it is waiting for your emails as soon as the machine is up.
EmailWatcher as it is also allows you to execute scripts, attached to the email. Look into the code for details ;)
It is even possible to
ssh to another machine and execute a bunch of commands stored in a script... The command you send in the email's body would be like
ssh firstname.lastname@example.org bash -s < ~/nodepi.banner.shYou can even
sudo, if a password is required, use
sshpass -p 'secret-password' ssh email@example.com bash -s < ~/nodepi.sudo.shThis can be dangerous, hey? With great power come great responsibilities...