Overview
Like most UNIX people, I write some shell scripts to make my life easier. I’d like to help you out by sharing them with you.
Service Control
So I use my netbook as a file-transport device between work and home, and my computing environment is very heterogeneous—I use Linux, Unix, OSX, and gasp Windows. Since I’m lazy, I’ve been using Samba as my file transport protocol in all these locations.
Now, I’m not convinced that Samba’s secure in any way, so I usually just write
some iptables
rules to filewall everything except for the machine I’m
transferring with.
This is a script that automates starting and stopping services and handling the iptables work for you as well.
#!/bin/bash ################################################################################ # Setup ################################################################################ IPTABLES_CMD="sudo /sbin/iptables" # Assign useful names to params SERVICE=$1 COMMAND=$2 if [ -z $3 ] then if [[ $COMMAND == *start ]] then echo "No host specified. Opening firewall to the world!" fi SRC="" else SRC="-s $3" fi ################################################################################ # iptables functions ################################################################################ # prepend rules to iptables # $1 = -p [tcp/udp] --dport [port #] (REQUIRED) # $2 = -s [IP/FQDN] (OPTIONAL) function iptables_prepend { PORT=$1 SRC=$2 $IPTABLES_CMD -I INPUT $PORT $SRC -j ACCEPT } # delete rules from iptables # $1 = regex for iptables rules to remove function iptables_remove { REGEX=$1 # init temp vars RULE_ARRAY[0]="" COUNT=0 # use grep and awk to pull the rule numbers that match $REGEX for RULE in `$IPTABLES_CMD --line-numbers -L INPUT | grep "$REGEX" | awk '{print $1}'` do # stuff the rules into an array RULE_ARRAY[$COUNT]=$RULE COUNT=$(expr $COUNT + 1) done # what's the size of our array? LEN=$(expr $COUNT - 1) # remove the rules from iptables in reverse, because the numbers change # if you remove them from the front! for (( i=$LEN; i >= 0; i--)); do $IPTABLES_CMD -D INPUT `echo ${RULE_ARRAY[$i]}` done } ################################################################################ # Service Definitions! ################################################################################ # smbd function start_smbd { SRC=$1 iptables_prepend "-p udp --dport 137" "$SRC" iptables_prepend "-p udp --dport 138" "$SRC" iptables_prepend "-p tcp --dport 139" "$SRC" } function stop_smbd { iptables_remove "netbios" } # apache2 function start_apache2 { SRC=$1 iptables_prepend "-p tcp --dport 80" "$SRC" } function stop_apache2 { iptables_remove "http" } ################################################################################ # Main case statement ################################################################################ case $COMMAND in start) sudo service $SERVICE start start_$SERVICE "$SRC" ;; stop) sudo service $SERVICE stop stop_$SERVICE ;; restart) sudo service $SERVICE stop stop_$SERVICE sudo service $SERVICE start start_$SERVICE "$SRC" ;; status) sudo service $SERVICE status ;; *) echo "Usage: \"service name\" start|stop|restart [host]" exit 1; ;; esac exit $?