Tag Archives: linux

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.

Nice way to kill your unresponsive linux system

Sometimes when a Linux system is unresponsive it seems like your only option is to hold down the power button.

Follow these key combos for a less risky reboot.

Hold Down Alt + SysRq and press R (raw keyboard mode) S (syncs disks) E (terminates all processes) I (kills the stragglers) U (read-only remount file-systems) B (reboots)

Give it lots of time to Sync the Disks and do it one more time after killing the processes to play it safe.

There she be. Careful, it’s a lot of fun.

Reboot System Even If Utterly Broken

Living wild and free

So now you have key-based authentication setup for remote administration of your servers and you really want to crank up the laziness factor.

Remembering a password sucks, especially if you’ve eliminated them for logons. Now you only need it for sudo commands, right? NOT AFTER THIS DANDY!!

Every time you need to run something that requires root privileges, you have to type sudo and then your password if you haven’t used the command recently.

Lets quit beating around the bush and get some work done.

~$ sudo visudo

If your user name isn’t already in the config, scroll down and add this line at the bottom. If it is, just modify it to fit this format (specifically the NOPASSWD: )


Then save it and quit. Now your good to go! No more pesky password.

This makes your private key very important to protect, especially if you didn’t put a passphrase on it.

Public Key Authentication with Putty


Windows = Still Exists

Administration from Windows of Linux = Putty

Key-based authentication from Putty = Puttygen

Launch the key generator included with Putty called PuTTYgen.

Click Generate. Move the mouse around for some good ol random numbers.

Stick a passphrase on it if you’re feeling insecure about yourself.

Save the public key somewhere and save the private key somewhere too. (Both are required for a good time.)

Before you close the program, copy the prepared public key to the clipboard from the box in PuTTYgen that has been prepared for the authorized_keys file.

Open an ssh session to the machine you’re administering.

~$ cat >> ~./ssh/authorized_keys

Then paste your key in there, hit enter, and press CTRL-C.

Now create a new session in PuTTY.

Go to Connection > Data and put in your Auto-login username that you just appended the public key to.

Then go to Connection > SSH > Auth and put the path of your private key in the box.

Then go back up to Session. Put the address or host name of your machine in the Host Name box, stick a name in Saved Sessions and hit Save.

Now you can double-click on it for instant satisfaction of authentication.

Mount stuff over ssh quickly with sshfs

This little program allows you to mount a folder on a remote system to a local folder using ssh.

Install sshfs.

$ sudo apt-get install sshfs

Use the following command to mount a folder.

$ sshfs user@remotehostname:/path/to/remote/folder  /path/to/local/folder

You don’t even have to be root to mount.

You can speed the process up with key-based access.

See here. http://blog.buttewifi.com/?p=15

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.