aboutsummaryrefslogtreecommitdiffstats
path: root/baseline/ufw-allow-mosh
blob: 3f696ca38aafcf02e92c8224e657888eedf1703d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#!/bin/bash
# ripped off from here:https://stephenreescarter.net/mosh-and-ufw-without-1000-open-ports/
# add entry in sudoers: my-awesome-user ALL:(ALL:ALL) NOPASSWD: /usr/local/bin/ufw-allow-mosh
# then add this in zshrc: sudo /usr/local/bin/ufw-allow-mosh

# 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