Tag Archives: bash

Scripts to Mount and search windows machines

Here are some scripts that I wrote to mount and search bulk windows machines.


cd /searchmachines
echo “Username please”
read username
echo “Password please”
read -s password
for machine in `cat machinelist.txt`
mkdir remote$machine
echo “Attempting to mount $machine…”
mount.cifs //$machine/c$ ./remote$machine -o username=$username,password=$password

This next script searches the machines for NTUSER.DAT files to find users that were logged in during a specific time frame.


cd /searchmachines
echo “Enter number of days for oldest file. (4 for no older than 4 days)”
read old
echo “Enter number of days for newest file. (2 for at least 2 days old)”
read new
echo “Searching…”
find ./ -name NTUSER.DAT -mtime $new -mtime -$old -daystart

Script for a DHCP address renewal

Normally my Internet connection is pretty good; but, once every couple of months, the provider changes something and my current IP address no longer works. The lease doesn’t actually expire, which is set to last 48 hours. So, I lose internet connectivity until a new renewal. It’s easy to do this by hand, but that doesn’t work when I’m not at home.

This is a script that checks to see if I’m connected to the Internet, and if it fails three times in a row, it connects to the router (pfSense in this case) and performs a DHCP release and renew.

#change to some writeable directory
cd /home/user

#check various sites
curl http://www.msn.com > internetchecksites
curl http://www.yahoo.com >> internetchecksites
curl http://www.google.com >> internetchecksites
curl http://www.cnn.com >> internetchecksites
curl http://www.cisco.com >> internetchecksites
curl http://www.hp.com >> internetchecksites

#count the number of lines received over http
#should be tons of crap from one of these alone
sizeofsites=`cat internetchecksites | wc -l`

#check to see if there are less than ten lines of HTML
if [ $sizeofsites -lt 10 ]; then
#mark a failure to the failure file
echo “strike” >> failedchecks

#reset the failure count
rm failedchecks
touch failedchecks

#if check has failed 3 times run the dhcp renewal process
if [ `cat failedchecks | wc -l` -ge 3 ]; then
curl -su admin:somepassword http://192.168.x.x/status_interfaces.php -d interface=”wan” -d submit=”Release” >> pf
curl -su admin:somepassword http://192.168.x.x/status_interfaces.php -d interface=”wan” -d submit=”Renew” >> pf
rm pf


Now just stick this script in your crontab for every 5 minutes or so.

~$ crontab -e

Add this line

*/5 * * * * /home/user/checkinternetscript.sh

The */5 is for every five minutes.

Make sure the script is executable with a nice chmod +x

The hardest part is determining what commands need to be sent to the HTTP interface on your router for a renewal.

This can be done by viewing the source code on the web page. It can also be done using a packet capture as well.

Make sure other users aren’t adding to your failedchecks file or they can cause a DHCP release/renew.

Script telnet for device changes

So you just kicked your roommate out and you need to change the username and password on 250 switches.

You can do it with a quick bash loop and pipe it into telnet. (ssh is way better, but isn’t enabled by default on most network devices)

Make a new file named credentials.txt and put the username on the first line and the password on the second.

Then make a new file named devicelist.txt and place the IP address or hostname of all of the switches in this file. (each on it’s own line)

username=`head -n 1 credentials.txt`
password=`tail -n 1 credentials.txt`
for device in `cat devicelist.txt`
echo open $device
sleep 1

echo ” “
(only required for devices with a banner before username prompt)
sleep 1

echo $username
sleep .5
echo $password
sleep .5
echo “command to execute on device”
sleep 1
echo “another command to execute on device”
) | telnet

If some of the devices have longer delays between prompts you can adjust the sleep values. (telnet isn’t smart enough to know when the device is ready for a command)

This script will obviously need to be modified depending on the device you’re connecting to. It will get all of those passwords changed automatically for you. You should be using a RADIUS or TACACS+ server for authentication with that many switches. 🙂

It does work well to invoke backups to tftp servers, etc.

Repurpose power button on Linux

The power button is pretty boring just turning the computer off.

Time to make it special for those headless Linux machines that play music but don’t have a keyboard.

Make sure ACPI is installed.

~$ sudo apt-get install acpi

Backup the old script.

~$ sudo cp /etc/acpi/powerbtn.sh  /etc/acpi/powerbtn.sh.bak

Clear all of that nonsense out of the old script and start with a fresh slate.

~$ sudo cat > /etc/acpi/powerbtn.sh



Hit ctrl-c to finish

But that only lets you do one thing with your button!

Lets fix that with a little evil and trickery. The following will wait for repeated presses after the first for different results.

Replace /etc/acpi/powerbtn.sh with this.

killall mainpowerbutton.sh
/root/mainpowerbutton.sh &

Now create /root/mainpowerbutton.sh and place this in it.


echo “i’ve been touched” >> /root/scriptisrunning

sleep 3

if [ `cat /root/scriptisrunning | wc -l` -eq 1 ]; then



if [ `cat /root/scriptisrunning | wc -l` -eq 2 ]; then



if [ `cat /root/scriptisrunning | wc -l` -eq 3 ]; then



rm /root/scriptisrunning


This allows you to press the button multiple times with different results for each press. The sleep timer will reset with each press so this can be expanded to any number of button presses. Just copy the if statement and change the -eq to the number of presses. Then put the path to the program or script you want to run on the next line. Make sure the if statement has a corresponding fi to end it.

Bash Guessing Game

Here is a game that is worth your time. You even get to code it yourself.

Lolz. Needs a bash interpreter.

~$ nano guessinggame.sh

Paste this in there.

echo “Please pick an integer between 1 and 10.”
read guess
while [ “$number” -le 1 ]
let “number %= 11 ”

if [ $guess -eq $number ]
echo “You win!”
echo “You suck. The number was $number.”


Then make it executable.

~$ chmod +x guessinggame.sh

Now you have yourself a dandy.

Run it like so.

~$ ./guessinggame.sh

This game does not have any input validation. If you try to guess a letter or special character, you will get an error. Guessing out of the bounds of 1 to 10 will also greatly reduce your odds of winning.