Advertisement
Bunsen

Linode Finder/Blocker

Mar 29th, 2017
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.01 KB | None | 0 0
  1. #!/bin/sh
  2. #This is a script to run on DD-WRT routers.
  3. #It will find current "linode connections", and add them to a script that adds then to iptables firewall.
  4. #RE: http://www.dd-wrt.com/phpBB2/viewtopic.php?p=1073042#1073042
  5. #This script is basically pieces of eibgrad's scripts, pasted together to do what we want.  
  6. # the _ipt function was taken from another of his scripts: credit to him.
  7. #Instructions:  
  8. # 1. Copy script to the router / save as a shell script "<script.sh>" [however you want to do this]
  9. # 2. add permsions to execute:   chmod +x <script.sh>
  10. # 3. Add a cron job to call this every 10min or so: that's all.
  11. #Also you can restart PMS occasionally to force new connections that can be found
  12.  
  13. LINODE_DIR="/jffs/sh" #save to jffs in order to "remember" entries that were previously found
  14. LINODE_BLOCKER="$LINODE_DIR/linode_blocker.sh"
  15. PLEXSERVER_IP="192.168.101.125"
  16. PLEX_USERNAME="user@gmail.com"
  17. PLEX_PW="thisIsMySecret"
  18.  
  19. if [ ! -e $LINODE_BLOCKER ]; then
  20.     mkdir -p $LINODE_DIR
  21.     cat << "    EOF" > $LINODE_BLOCKER
  22. #!/bin/sh
  23.     _ipt() {
  24.         # precede insert/append w/ deletion to avoid dups
  25.         cmd="-I FORWARD -s $PLEXSERVER_IP -d $1 -j REJECT"
  26.         while iptables ${cmd/-[IA]/-D} 2> /dev/null
  27.             do :; done
  28.         iptables $cmd
  29.     }
  30.  
  31.     EOF
  32.     chmod +x $LINODE_BLOCKER
  33.     sed -i "s:\$PLEXSERVER_IP:$PLEXSERVER_IP:" $LINODE_BLOCKER
  34. fi
  35.  
  36. awk '{i=1; if (NF > 0) do {if ($i ~ /((\d+\.){3}\d+)/) print substr($i,5); i++;} while (i <= NF);}' /proc/net/ip_conntrack | \
  37.  grep -v 192.168 | \
  38.  awk '!x[$0]++ {system("nslookup " $0 " 8.8.8.8")}' | \
  39.  grep "linode" | \
  40.  awk -v blocker="$LINODE_BLOCKER" '{print "_ipt " $3 >> blocker}'
  41.  
  42. curl -k -u$PLEX_USERNAME:$PLEX_PW https://plex.tv/services/pubsub/servers | awk -v blocker="$LINODE_BLOCKER" '{i=1; if (NF > 0) do {if ($i ~ /((\d+\.){3}\d+)/) system("if ! $(grep -q " substr($i,7,length($i)-7) " " blocker"); then echo _ipt " substr($i,7,length($i)-7)"; fi"); i++;} while (i <= NF );}' >> $LINODE_BLOCKER
  43.  
  44. $LINODE_BLOCKER
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement