Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@valorin
Created December 6, 2014 20:49
Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save valorin/10c953c73f0da5fbee2f to your computer and use it in GitHub Desktop.
Save valorin/10c953c73f0da5fbee2f to your computer and use it in GitHub Desktop.
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
Copy link

ghost commented Jun 24, 2016

thank you

@anthonymobile
Copy link

this is pure genius

@rhndev
Copy link

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