Linux

Debug a bash shell script

Shell scripting debugging can be boring job (read as not easy). There are various ways to debug a shell script. Method # 1: Use -x option Run a shell script with -x option. $ bash -x script-name $ bash -x domains.sh Method # 2: Use of set builtin command Bash shell offers debugging options which […]

Read More
Linux

Execute commands on multiple hosts using ssh

#!/bin/bash # Linux/UNIX box with ssh key based login SERVERS=”192.168.1.1 192.168.1.2 192.168.1.3″ # SSH User name USR=”jadmin” # Email SUBJECT=”Server user login report” EMAIL=”admin@somewhere.com” EMAILMESSAGE=”/tmp/emailmessage.txt” # create new file >$EMAILMESSAGE # connect each host and pull up user listing for host in $SERVERS do echo “——————————–” >>$EMAILMESSAGE echo “* HOST: $host ” >>$EMAILMESSAGE echo “——————————–” […]

Read More
Scripting

Mass setting user passwords

Every need to change (or set) passwords for a large number of users? Something like this code snippet may help. # find all the usernames with bash logins usernames=$(cat /etc/passwd | grep bash | sed ‘s/:.*//g) for i in $usernames; do echo “somepassword” | passwd –stdin $i done

Read More
Scripting

nslookup-scan of IP-range/subnet

#!/bin/bash # nslookup-scan of IP-range # It’s possible to add more networks separated with space NETS=”192.168.0″ IPRange=”1 254″ for NET in $NETS; do for n in $(seq $IPRange); do ADDR=${NET}.${n} echo “${ADDR},`nslookup ${ADDR} | awk -F “=” ‘{ print $2 }’|sed ‘s/^[ t]*//’ | sed ‘/^$/d’ | |sed ‘s/.$//’`” done done Result 192.168.0.1,cba.infra.no 192.168.0.2,bca.infra.no 192.168.0.3,abc.infra.no […]

Read More
Scripting

Hints and Tips for general shell script programming

WARNING: this will fail if the user is playing with $0 For example using a symbolic or hard link with a unexpected name. # Simplest… # PROGNAME=`type $0 | awk ‘{print $3}’` # search for executable on path PROGNAME=`basename $PROGNAME` # base name of program # Advanced… # Script name, in what directory, and in […]

Read More