Execute ssh script with iOS shortcut app: Rasberery Pi and Linux operation progress!

Introduction

Do you use the iOS shortcut app? I haven't used it at all until recently ... However, recently I have come to feel the infinite possibilities of the combination with Rasberry Pi (* it can be used with other devices), and I wanted to share this impression with my brush! image.png

This article mainly deals with the following contents.

--SSH connection setting method (public key authentication supported from iOS13) --Execute arbitrary script --Simple example --Run a program that runs continuously in the background --Exit the program running in the background --Shut down/restart the remote machine

I've recently started working around here, so I'd appreciate it if you could comment on any mistakes.

SSH connection setting method (public key authentication supported from iOS 13)

Until iOS12, only connection by password authentication was possible, but since iOS13, public key authentication is supported. The connection method is very easy.

1. Operation on iPhone: Public key sharing

First, launch the shortcut app and select "Run script via SSH" from the search field. image.png

Then, the screen for entering the SSH connection settings will be displayed as shown below. Enter the host port user according to your environment. The authentication method is password by default, but you can choose SSH key. image.png

When you select the SSH key, the following screen will be displayed. First, select ① "Generate new key" and select the desired type and bit length. Next, select ② "Share public key" to display the public key. Choose a sharing method that suits you best, such as sending it by email. If the machine you want to connect to SSH has not disabled password authentication, copy the public key generated here to the clipboard, connect SSH with the password → add the public key to the setting file, etc. I think it can be operated. I had disabled password authentication at this point, so I emailed the iPhone's public key to another PC that had already been authenticated with the public key, and added the public key using the other PC.

image.png

2. Operation on the destination machine you want to connect to SSH

Since there are various articles here, I will omit the details and list only the links. I wanted to use a Rasberry Pi, so please forgive me for the source being that way.

Primary source; https://www.raspberrypi.org/documentation/remote-access/ssh/passwordless.md

Secondary source: https://tool-lab.com/raspi-key-authentication-over-ssh/

** This completes the connection settings. ** After that, I will write the script. I will explain with a concrete example.

Run any script

Simple example

First, let's execute a simple example of getting a list of tasks running on a remote PC and displaying them on the iPhone. You can get the task list with ps, but you can't display the result on iPhone. If you add a "view" action there, you will receive a "shell script result" by default. When you do this, you should see a list of tasks running on the remote PC in the notification area.

image.png

Run a program that runs continuously in the background

Here is an example: In the case of a program that runs continuously, such as "start the server", the shortcut app will continue to wait for the program to finish and the action will not complete. Even if the above problem is avoided, it is necessary to continue the process on the remote PC side even after the SSH connection is disconnected.

This time, I will describe the case where the program ~/myapp/main is executed in the background as a trial.

cd ~/myapp
nohup ./main >/dev/null 2>&1 &

--nohup: This command allows you to continue processing after an SSH connection --Last &: This will do the work in the background -->/dev/null 2> & 1: If you execute only nohup ./main & as above, the action was not completed probably because various standard outputs are displayed. Therefore, add this command to read the standard output/error.

With the above processing, we were able to execute the processing in the background and continue the operation even after the SSH connection is disconnected.

Quit the program running in the background

Now, while it's nice to be able to do the work in the background, the problem is that you can't kill it. In such a case, execute the following script. (Replace the main part with the task name you want to finish) I referred to here.

ps aux | grep main | grep -v grep | awk '{print "kill -9", $2}' | sh

Shut down/restart the remote machine

By the way, when using Rasberry Pi etc., it is a simple and troublesome thing to shut down the machine. I'm doing sudo shutdown -h now every time, but I'd like to do it with the touch of a button if possible.

You can also use the shortcut app in such cases: innocent:

The procedure is simple, just write the script as in the above example, but ** you had to enter the password when sudo, and it took a lot of work to realize it with the shortcut app. ** **

In case of reboot

echo (Ask every time) | sudo -S reboot 2>/dev/null

--You can receive the password from standard output by doing sudo -S. ――So I run echo, but somehow I'm afraid to save the password in clear text after that. Therefore, I decided to use the "variable" function of the shortcut and ask for the password every time.

Now you can shortcut and restart with the touch of a button ...!

in conclusion

What did you think. This time I introduced an example based on the case I practiced, but I think it can be used for various other cases depending on the combination. Comments such as that it can be used for such things are welcome ...!

Recommended Posts

Execute ssh script with iOS shortcut app: Rasberery Pi and Linux operation progress!
linux with termux app