• Automation
  • Scripting

How to use iOS Shortcuts to run scripts on a server from your iPhone

April 25, 2021 • 2 min read

Start running tasks on your server from the iPhone

In iOS 14, Apple added the Shortcuts app which allows you to automate tasks on your iPhone. To create a Shortcut you put together Actions. One of the actions available is the Scripting action which has a feature for running shell scripts. We will use this feature to connect to a remote server and run a simple script.

Requirements

  • iPhone
  • iOS 14
  • Access to a remote server

Adding An Action

The first thing we need to do is select the correct Action to run.

  1. Open the Shortcuts app
  2. Press on the + icon to create a new shortcut
  3. Now Press on the Add Action button
  4. Press the Scripting button or search for it if you don't see it
  5. Scroll to the "Shell" section then press on "Run Script Over SSH"
  6. Now you will see the action in your Shortcuts screen.

Run Script Over SSH Action

Connecting to the Server

You can connect to your remote server using a username and password, but it is more secure if you use an SSH Key. The good thing is that an SSH Key is easy to set up on an iPhone.


Username and Password

If you want to connect with your username and password:

  1. Click on "Show More"
  2. Fill in the Host, User, and Password fields. Leave everything else the same
  3. Press the run icon at the bottom to test the connection

Run Connection

  1. You will get a pop up after, go ahead and click on "Connect"

Creating and Using SSH Key (Optional)

  1. Click on "Show More"
  2. Select "SSH Key" in the Authorization section
  3. If you don't have an SSH Key, click in the SSH Key field to create a new one
  4. Share the public key with yourself via email
  5. Copy the SSH Key to the ~/.ssh/authorizedkeys or something equivalent for the user you are connecting as
  6. Test out the connection and click on "Connect" when the pop up shows up

Adding our script

Now that we can connect, you can add your script in the script section. For example, to create a new directory called "testiphone" in your home directory on the server, add the following and press run.


Test Script


Pretty much any command or script that you can run on your server from a command line, can be done from your iPhone with this feature.

Save the Shortcut

Press "Next", add a name for your shortcut and save it so yo can run it whenever you want.

A Real World scenario

For one my websites, I was having consistent issues that caused the site to go down frequently. I started looking for solutions but I was having a hard time finding any. The temporary solution for me to was to hop on my computer, connect to the server, and shut it down. Of course this required me to always be near my personal laptop, which is not always the case. Being able to do it from my iPhone makes it a lot easier since most of the time my phone is nearby. This issue does not happen anymore, but if it does and I need to turn off my server I can use a shortcut to do it from my phone.


Another real world example is being able to run a script to check the status of website from your phone and letting you know if it is up or not.

Conclusion

Hope this was helpful and let me know if you come up with any interesting shortcuts.