Monday, November 21, 2022

Mastodon gateway on Linux

Install toot

As root (or add sudo to commands) install Python 3.X if you have not already with

yum install python3

then run

pip3 install toot

toot login

Answer instance with your instance hostname, then get code from browser. Note I had an issue (see pic) when I tried this again for a second server which appears to be and server customization issue. Fortunately I was able to copy the config file from the first server to the second. This would indicate the info is not client host of user linked.

Once the config file is set up for the user you can test with something simple like

toot post "toot test"

Note the double quotes. See the usage docs for more complicated posts.

Toot when server starts

We need to create some files with:

vi /root/tootUp.sh

#!/bin/sh

toot post "$HOSTNAME is back up" 

chmod 744 /root/tootUp.sh

The 2>&1 | tee -a /var/log/toots.log bit is only if you want to log the toots.

vi /etc/systemd/system/toot.service

[Unit]

Description=Toots that the system is back up

After=network.target


[Service]

Type=simple

ExecStart=/root/tootUp.sh

TimeoutStartSec=0


[Install]

WantedBy=default.target

Now enable it, try it and check it with:

systemctl enable toot.service

systemctl start toot.service

status -l toot.service

If everything is working then you should see something like this:

# systemctl status -l toot.service

● toot.service - Toots that the system is back up

   Loaded: loaded (/etc/systemd/system/toot.service; enabled; vendor preset: disabled)

   Active: inactive (dead) since Mon 2022-11-21 14:24:52 EST; 19s ago

  Process: 2622 ExecStart=/root/tootUp.sh (code=exited, status=0/SUCCESS)

 Main PID: 2622 (code=exited, status=0/SUCCESS)


Nov 21 14:24:51 server.dea42.org systemd[1]: Started Toots that the system is back up.

Nov 21 14:24:52 server.dea42.org tootUp.sh[2622]: Toot posted: https://techhub.social/@avatar42/109383430460374213

ssh gateway

To be safer you should really create a toot user. As root run

adduser toot

passwd toot

mkdir -p ~toot/.config/toot

chown -R toot.toot ~toot/.config

chmod -R 700 ~toot/.config

cp /root/.config/toot/config.json ~toot/.config/toot

chown -R toot.toot ~toot/.config/toot/config.json

chmod 600 ~toot/.config/toot/config.json

su - toot

Note you are now acting as user toot. 

Create a simple post script. This example is as basic as it gets you can be way more fancy.

vi tootPost.sh

#!/bin/sh

toot post "$*" 2>&1 | tee -a /var/log/toots.log

chmod 755 tootPost.sh

You might want a quick test like

~toot/tootPost.sh ssh gateway test

Note no double quotes. The script deals with that which we will need for the next bit.

Using the ssh gateway.

For the examples below I'm using the gateway IP address of 10.10.2.200. Replace it with yours.

Login to the PC you want to "toot" from.

Open a command / terminal window and change to the folder you want the toot files in.

Generate a key with

ssh-keygen -q -f toot_rsa

Take defaults / just press ENTER at each prompt.

Note this is creating a private key file we can use separately from default so it does not need a password on it.

Now we want to give the server the public copy so in future they will know it is us. It creates the .ssh folder if need be.

cat toot_rsa.pub | ssh toot@10.10.2.200 -o StrictHostKeyChecking=no "umask 077; test -d .ssh || mkdir .ssh ; cat >> .ssh/authorized_keys" || exit 1

It will ask you for the password to connect but you will not need to again. You can test this with

ssh -o StrictHostKeyChecking=no -i ./toot_rsa toot@10.10.2.200

or if you just want to go for it

ssh -o StrictHostKeyChecking=no -i ./toot_rsa toot@10.10.2.200 ./tootPost.sh ssh gateway test from client

toot and RSS feed

Lastly we can use this setup to toot an RSS feed.

Install feedexec back on the gateway server as user toot. 

pip3 install feed2exec

Note you may need to install some dependencies as well. I needed to install these too.

pip3 install sqlite3 sqlite pysqlite

Test with 

feed2exec parse https://www.nasa.gov/rss/dyn/breaking_news.rss --output echo --args '{item.title}'

If that works, create the file ~/.config/feed2exec.ini

vi ~/.config/feed2exec.ini

[SpaceStation]

url = https://www.nasa.gov/rss/dyn/shuttle_station.rss

output = feed2exec.plugins.exec

args = /home/tootPost.sh {item.title}. For details see {item.link}

Just change the RSS URL and message as needed. To then post any new items call
feed2exec fetch
or better yet set up cron with
crontab -e
adding the line
00 * * * * /home/toot/.local/bin/feed2exec fetch
to run it every hour.