Scripts

This page is full of little (and not-so-little) scripts. Some of the work below is poorly written but useful nonetheless. When I update a script, I’ll bump it to the top. Unless otherwise stated, these scripts are copyright Brendan Hide and available under the GPLv3 license.

I’m still figuring out which is probably the best way to maintain this page so don’t be surprised if things go haywire.

dim – minimal DNS lookup results

1
2
3
4
5
6
7
8
9
10
11
12
#!/bin/bash
# Author: Brendan Hide (http://swiftspirit.co.za/)
# Copyright: (c) Brendan Hide, released under the GPLv3 license
#            See http://www.gnu.org/licenses/gpl-3.0.html for the License details
# Name: dim
# Release Version: 0.1 (2009-05-08)
# TODO: --help
# Description: gives minimal dig results (no fluff)
# Special thanks to Nico Visser for the inspiration
# Yes, I think it is funny that, for now, this is a one-liner with 10 lines of comment
 
dig $* | grep . | grep -v "^;"

mydigns – specify a DNS server as used by other dig scripts

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#!/bin/bash
# Name: mydigns
# Author and Copyright: Brendan Hide (http://swiftspirit.co.za/)
# Release Version: 0.4 (2009-04-10)
# TODO: long options - bash's builtin getopts doesn't support SunOS-style x(xlong) parameters
 
function rotate {
old=`cat ~/.mydigns`
#reform
trodden="false"
 
head -n 1 ~/.mydigns.all > ~/.mydigns.tmp
 
cat ~/.mydigns.all | while read nameserver ; do
 if [ "$trodden" == "true" ] ; then
  echo $nameserver > ~/.mydigns.tmp
  break
 fi
 if [ "$nameserver" == "$old" ] ; then
  trodden="true"
 fi
done
 
mv -f ~/.mydigns.tmp ~/.mydigns
 
} #rotate
 
function usage {
echo "mydigns - provides a DNS server name from ~/.mydigns for untarnished dns lookups"
echo "Usage: mydigns [OPTIONS]"
echo
echo "  -a servername    Add servername to the list of servers at ~/.mydigns.all"
echo "  -r               Rotate a new servername from ~/.mydigns.all into ~/.mydigns"
echo "  -h               Show this Help information"
}
 
function sortlist {
 cat ~/.mydigns.all | sort | uniq > ~/.mydigns.all.tmp
 mv -f ~/.mydigns.all.tmp ~/.mydigns.all
}
 
while getopts ":a:rh" options; do
 case $options in
  a ) echo $OPTARG >> ~/.mydigns.all ;;
  r ) rotate ;;
  h ) usage ;
      exit 0 ;;
  \?) usage;
      exit 1 ;;
  * ) usage;
      exit 1 ;;
 esac
done
 
sortlist
 
cat ~/.mydigns

authcheck – bash – basic http accessibility check

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
32
#!/bin/bash
# Name: authcheck
# Author and Copyright: Brendan Hide (http://swiftspirit.co.za/)
# Release Version: 0.3 (2009-04-09)
# TODO: nil
 
#If you have admin rights to the server, input the secret text into a comment of the default page
#SECRETTEXT=`echo "tricky secret text" | md5sum | awk {'print $1'}`
SECRETTEXT="tricky secret text"
 
RANDSET="$RANDOM.$$"
HUH="/tmp/.authcheck.$RANDSET.huh.htm"
DEF="/tmp/.authcheck.$RANDSET.def.htm"
 
trap "rm -f $HUH ; rm -f $DEF" INT TERM EXIT
 
if [ $# -eq 1 ]
 then
  wget --no-cache -T 2 -t 3 -O - $1/huh.htm 2>&1 | cat > $HUH
  wget --no-cache -T 2 -t 3 -O - $1 2>&1 | cat > $DEF
  grep "$SECRETTEXT" < $HUH > /dev/null && echo "Domain $1 is being served by the default site on the server"
  grep "Giving up" < $DEF > /dev/null && echo "Domain $1 is not responding - \"Giving up\""
  grep "No route to host" < $DEF > /dev/null && echo "Domain $1 is not reachable - \"No route to host\""
  grep "401 Unauthorized" < $DEF > /dev/null && echo "Domain $1 has ACL issues - 401 Unauthorized"
  grep "403 Forbidden" < $DEF > /dev/null && echo "Domain $1 has no default content and directory browsing is forbidden - 403 F$
 else echo "Usage: authcheck domainname"
fi
 
rm -f $HUH
rm -f $DEF
 
trap - INT TERM EXIT

pidofp – Linux – gives the process id (pid) of the application’s parent

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#!/bin/bash
# Name: pidofp
# Author and Copyright: Brendan Hide (http://swiftspirit.co.za/)
# Release Version: 0.2 (2009-03-17)
# TODO: multiple parent-of-check; verbose output format options
 
if [ $# -gt "0" ]
 then
  for i in `pidof $1` ; do
   cat /proc/$i/status | grep PPid | awk '{printf $2} {printf " "}'
  done
  echo
 else
  echo "pidofp : print the parent \$PID"
  echo "Usage: pidofp program"
  echo " See also: pidof"
fi

rd(|c) – remote desktop scripts to log into a Microsoft Windows Server

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#!/bin/bash
# Name: rd
# Author and Copyright: Brendan Hide (http://swiftspirit.co.za/)
# Release Version: 1.3.0 (2009-03-17)
# TODO: encrypted password storage support ; getopts
# License: GPLv3
# Installation covered at http://dogma.swiftspirit.co.za/archives/11
 
#DEFAULTS
res="1024x768"
readonlyshare="/media/rd"
writableshare="/media/rd/honey"
defaultuser="Administrator"
 
# You shouldn't ordinarily have to change anything below this line
 
[ -e ~/.rd ] && . ~/.rd
 
usageinfo="echo Usage: rd(c) [-s shell] server [username]"
 
function remote {
 
tmplog="/tmp/.rd.$RANDOM.log"
 
rdesktop -r disk:rd=$1 -r disk:rd=$2 -g $3 -a $4 -k $5 $6 -u $7 $8 $9 2 > $tmplog.err > $tmplog 
 
remotehost=$8
 
cat $tmplog $tmplog.err | grep -i error
cat $tmplog $tmplog.err | grep -i error > /dev/null && echo -n Remote Desktop to $remotehost failed.
cat $tmplog $tmplog.err | grep "unable to resolve host" > /dev/null && echo " Could not resolve host name."
cat $tmplog $tmplog.err | grep "ERROR: recv: Connection reset by peer" > /dev/null && echo You may have been disconnected by another user on $remotehost.
cat $tmplog $tmplog.err | grep "ERROR: .*: Connection refused" > /dev/null && echo The server $remotehost refused the connection.
cat $tmplog $tmplog.err | grep -i error > /dev/null && echo -n Press [Enter] to continue.                                       
 
# Comment these out for debugging purposes
rm -f $tmplog
rm -f $tmplog.err                         
 
} #function remote
 
mycmd=`echo $0 | sed -r 's/.+\/([^\/]*)$/\1/'`
 
if [ "$mycmd" == "rdc" ]
 then rdconsole="-0"
 else rdconsole=""
fi
 
rdesktop 2>&1 | grep "command not found" > /dev/null && echo "The rdesktop client was not found." && echo "Please install it from your distribution or see http://www.rdesktop.org/" && exit 8
 
case $1 in
 "-h") $usaginfo ;;
 "--help") $usageinfo ;;
 *) case $# in
     0) $usageinfo ;;
     1) case $1 in
         "-s") echo "Invalid syntax: -s without a shell argument" ; $usageinfo ;;
         "--shell") echo "Invalid syntax: --shell without a shell argument" ; $usageinfo ;;
         *) remote $readonlyshare $writableshare $res 16 en-us -z $defaultuser $1 $rdconsole & ;;
        esac ;;
     2) case $1 in
         "-s") echo "Invalid syntax: no server argument" ; $usageinfo ;;
         "--shell") echo "Invalid syntax: no server argument" ; $usageinfo ;;
         *) remote $readonlyshare $writableshare $res 16 en-us -z $2 $1 $rdconsole & ;;
        esac ;;
     3) case $1 in
         "-s") remote $readonlyshare $writableshare $res 16 en-us -z $defaultuser $3 -s $2 $rdconsole & ;;
         "--shell") remote $readonlyshare $writableshare $res 16 en-us -z $defaultuser $3 -s $2 $rdconsole & ;;
         *) echo -n "Invalid syntax: " ; $usageinfo ;;
        esac ;;
     4) case $1 in
         "-s") remote $readonlyshare $writableshare $res 16 en-us -z $4 $3 -s $2 $rdconsole & ;;
         "--shell") remote $readonlyshare $writableshare $res 16 en-us -z $4 $3 -s $2 $rdconsole & ;;
         *) echo -n "Invalid syntax: " ; $usageinfo
        esac ;;
    esac ;;
esac

diga – dig specifically for an A record

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#!/bin/bash
# Name: diga
# Author and Copyright: Brendan Hide (http://swiftspirit.co.za/)
# Release Version: 0.2 (2009-03-17)
# TODO: dns server failover
 
#nameserver
ns=`mydigns`
if [ $# = 1 ]; then
 dig soa $1 @$ns | grep SOA | grep -v "^;" | awk '/[.]/ && !/[;]/{print $5}' | xargs -i dig $1 A "@{}" | grep -v "^;" | grep . || SOARec=Fail
 echo "$1" >> ~/.diga.log
 if [ "$SOARec" = "Fail" ]; then
  echo No SOA record found on $ns
  dig $1 A @$ns | grep -v "^;" | grep . || echo "No records found. Please try DNS-tracing."
 fi
 else echo "Usage: diga example.com"
fi
  • Share/Bookmark