Skip to content

Instantly share code, notes, and snippets.

@amomin
Last active September 23, 2015 00:05
Show Gist options
  • Save amomin/1ec342b5e2a1481d0bdf to your computer and use it in GitHub Desktop.
Save amomin/1ec342b5e2a1481d0bdf to your computer and use it in GitHub Desktop.
Things I forget

Firewalls

Add rule to open port 3000 for nodejs on line 2001 (which must be before the DROP rule)

First get the line numbers

$ sudo iptables -nL --line-numbers

Assuming you have a DROP all line on line 2002 and you want to add a new rule just before that:

$ sudo iptables -I INPUT 201 -i eth0 -p tcp --dport 3002 -j ACCEPT -m comment --comment "Port 3000 for nodejs site"

Shell scripting

How to get the path to a file executing a bash script

From this SO Q/A:

SOURCE="${BASH_SOURCE[0]}"
while [ -h "$SOURCE" ]; do # resolve $SOURCE until the file is no longer a symlink
  DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
  SOURCE="$(readlink "$SOURCE")"
  [[ $SOURCE != /* ]] && SOURCE="$DIR/$SOURCE" # if $SOURCE was a relative symlink, we need to resolve it relative to the path where the symlink file was located
done
DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment