Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Script to open and close Mosh ports in UFW
#!/bin/bash
# Load active ports
PORTS=`lsof -i | grep mosh-serv | cut -f2 -d":"`
STATUS=`sudo ufw status`
# Add Rules for new ports
for PORT in $PORTS; do
echo $STATUS | grep "$PORT/udp" > /dev/null
if [ $? -gt 0 ]; then
echo "Allowing new port $PORT"
sudo ufw allow $PORT/udp > /dev/null
fi
done
# Remove closed ports
PORTS=`sudo ufw status | grep "^60.../udp" | cut -f1 -d"/" | sort | uniq`
OPEN=`lsof -i | grep mosh-serv`
for PORT in $PORTS; do
echo $OPEN | grep $PORT > /dev/null
if [ $? -gt 0 ]; then
echo "Removing closed port $PORT."
sudo ufw delete allow $PORT/udp > /dev/null
fi
done
@ber532k

This comment has been minimized.

Copy link

@ber532k ber532k commented Jun 24, 2016

thank you

@anthonymobile

This comment has been minimized.

Copy link

@anthonymobile anthonymobile commented Aug 15, 2018

this is pure genius

@rhndev

This comment has been minimized.

Copy link

@rhndev rhndev commented Nov 15, 2020

THIS. Is why I love programming right here. You FUCKING ROCK. I love you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment