Pages

dyndee.sh - dyndns update client

Just a minimalistic dyndns.org update client written entirely in bash.
The only requirement is that bash should be compiled with --enable-net-redirections which on many systems is by default.

It gets the ip from http://metawire.org/~inode/ip.php which is a simple
<?php echo $_SERVER['REMOTE_ADDR']; ?>

It can be modified to use others such as checkip.dyndns.org
If you find any bug or have any requests, contact me.
#!/bin/bash
# dyndee.sh - update client for dyndns.org
# written entirely in bash

substrings, brace expansion, sequence expressions

Let's say you have a set of files in a directory named like
exp1eve1.txt
exp1eve2.txt
exp1eve3.txt
exp1eve4.txt
and you want to change the exp1 part to exp2 so they become
exp2eve1.txt
exp2eve2.txt
...

BASH_REMATCH, regular expressions

Since version 3, bash has an additional binary operator `=~' which is able to handle regular expressions. Every substring which is matched by the subexpression which is in parenthesis can be accessed from the BASH_REMATCH variable.

Here is a small script i use to get the latest freshmeat releases
#!/bin/bash

rarcrack.sh: crack those rar files

There are times when you just "forget" that rar password
first get unrar from http://www.rarlab.com/rar/unrarsrc-3.3.4.tar.gz
and the script
just change wordlist and unrar paths and run it
./rarcrack file.rar

#!/bin/bash
wordfile=/usr/share/dict/words
xrar=/usr/bin/unrar
[ -x "$xrar" ] || exit 1
[ -r "$wordfile" ] || exit 1
[ -r "$1" ] || exit 1
trap 'printf "\n%s\n" "caught signal" && exit 1' 2
try=('|' '/' '-' '\')
i=1
while read line; do
printf "\r%-$((${#pline}+10))s\r%s" "${try[$((i%4))]} trying $line"
"$xrar" e -p"$line" "$1">/dev/null 2>&1 &&
printf "\r%-$((${#line}+10))s\n" "password: $line" && exit 0
((i++))
pline=$line
done<$wordfile
printf "%s\n" "tried $i passwords but no luck!"

shell tip: dealing with weird filenames

I know its frustrating. Thank god there is the end of options list.


-bash-3.00$ cd -=test=- # nope
-bash: cd: -=: invalid option
cd: usage: cd [-L|-P] [dir]
-bash-3.00$ cd "-=test=-" # nope
-bash: cd: -=: invalid option
cd: usage: cd [-L|-P] [dir]
-bash-3.00$ cd '-=test=-' # nope
-bash: cd: -=: invalid option
cd: usage: cd [-L|-P] [dir]
-bash-3.00$ cd \-\=test\=\- # nope
-bash: cd: -=: invalid option
cd: usage: cd [-L|-P] [dir]
-bash-3.00$ cd -- -=test=- # yes
-bash-3.00$ cd ..
-bash-3.00$ rmdir -- -=test=- # yes
-bash-3.00$

script: massping.sh: mass ping scanner

pretty fast
i know.. some ranges are not valid. More checking needs to be done

#!/bin/bash
#
# ./shping.sh 192.168.0.1-150

function usage() {
echo "usage example: $0 192.168.0.1-100"
exit 1
}

IFS='.'
iprange=($@) # 192 168 0 1-100
IFS=$'\040\t\n' # printf "$IFS"|hexdump -bc
srange=${iprange[3]/%-*/} # 1
erange=${iprange[3]/#*-/} # 100
j=0

# Do some checking
[ "$#" == "1" ] && # only 1 arg
[ ${iprange[*]//[[:digit:]]/} ] && # does it exist?
[ ${#iprange[@]} == 4 ] && # x.x.x.x
[ ${iprange[*]//[[:digit:]]/} == "-" ] ||
usage


# values within 0.0.0.0-255.255.255.255
for i in "${iprange[@]:0:3}" "$srange" "$erange"; do
[ $i ] &&
[ $i -le 255 ] &&
[ $i -ge 0 ] ||
usage
done

# change 192.168.0.10-1 to 192.168.0.1-10
if [ $srange -gt $erange ]; then
trange=$srange
srange=$erange
erange=$trange
fi

# add dots & feed the array
for ((i=$srange; $erange >= i; i++)) do
iplist[$j]="${iprange[@]:0:3}.$i"
iplist=("${iplist[@]// /.}")
(( j++ ))
done

# now feed ping with random ips
for i in ${iplist[@]}; do
r=$(( RANDOM%${#iplist[@]} ))
ping -c1 -s0 ${iplist[$r]}>/dev/null && echo ${iplist[$r]} &
iplist[$r]=""
iplist=(${iplist[@]})
done

exit 0;

script: expect.sh: using bash instead of expect

because i hate expect scripting
try this:

#!/bin/bash
expect=("hi" "white" "in")
reply=("hello" "black" "out")

while read -n1 "char"; do
word="$word$char"
[ "$word" == "${expect[$count]:0:${#word}}" ] || word="$char"
if [ "$word" == "${expect[$count]}" ]; then
printf "\n${reply[$count]}\n"
(( count++ ))
[ "$count" == "${#expect[@]}" ] && break
word=""
fi
done