Tag Archives: loop

Scripts to Mount and search windows machines

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

mountmachines.sh

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

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

search.sh

#!/bin/bash
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 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)

#!/bin/bash
username=`head -n 1 credentials.txt`
password=`tail -n 1 credentials.txt`
for device in `cat devicelist.txt`
do
(
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
done

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.