Archive

Posts Tagged ‘Bash Scripting’

dhcpd lease file – filter out the IP and MAC addresses with bash

December 25th, 2012 2 comments

Hi all

I had to pull out IP – MAC  information from the lease file of dhcpd (ISC dhcp v3) consisted of 90 machines, so I nailed it with a script that will help you do the same.
Example of a dhcp.leases file

[root@RHEL6-64bit Desktop]# tail -n 10 leases.txt
}
lease 192.168.212.220 {
starts 1 2012/12/24 13:31:41;
ends 1 2012/12/25 13:41:41;
binding state active;
next binding state free;
hardware ethernet 00:0c:29:a2:a4:b5;
uid “\001\000\014)\242\244\265″;
client-hostname “Test-Win2008R2-SP1″;

}

and results of script:

[root@RHEL6-64bit Desktop]# ./script.sh
Processing dhcp leases file…
Here’s a snippet from the outcome…
192.168.212.220 00:0c:29:a2:a4:b5
192.168.212.221 00:0c:29:19:56:be
192.168.212.223 00:0c:29:08:cc:7c
192.168.212.224 00:0c:29:e3:27:1c
Removing previously created temporary files…

file location: /tmp/outcome

Download the script in zip format: Download

note: to remove the ‘:’ from the MAC addresses (if needed for a reason, for example: manually adding reservations to Microsoft’s DHCP via netsh) just use the command:

sed s/://g leasefile > newfile.txt

Categories: Bash Scripting Tags:

Sync time against an NTP server with bash

September 10th, 2012 No comments

while we are heading towards time changes, I prepared a really simple script to sync time against an NTP server in a closed LAN environment I have that does not have internet access

#!/bin/bash
# Sync time against an NTP server
# Since it is impossible to sync the time against an NTP server while the NTP
# Daemon is running, it stops and then reloads it again after it’s done syncing.
SERVICE=`find /etc/init.d/ -name ntpd` > /dev/null
SERVER=192.168.0.1 # Your NTP Server Address
if [ -f $SERVICE ]; then
$SERVICE stop
ntpdate $SERVER > /dev/null
$SERVICE start
fi
exit 0

Categories: Bash Scripting Tags:

Ping range of addresses from a file

April 25th, 2012 No comments

This very simple but effective script allows you to ping multiple addresses taken from a file all at once, the addresses  must be sorted line by line.

#!/bin/bash
ips=`cat /path/to/ipfile`
sleep 1
clear
echo “Pinging remote computers…”
sleep 1
pingup()
{
ping -c 1 $1 > /dev/null
[ $? -eq 0 ] && echo “Remote $a is up”; [ $? -eq 1 ] && echo “Remote Server $a is DOWN”
}
for a in $ips
do
pingup $a
done

The addresses file contains some ip addresses I typed in beforehand.

And the results:

 

Only thing left to change is the /path/to/ipfile in the upper part of the script, change it to your own file.

 

Categories: Bash Scripting Tags:

Automating Remote backups of important data with SCP & Bash

April 21st, 2012 No comments

Automating Remote backups of important data with SCP & Bash.

at some point I had to get some files that change regularly backed up, those files were on a remote server and I wasn’t always in front of the computer to get it done.

so there are (few) solutions for that matter.

let’s say this is the topology of my system:

Let’s say I have a file server with the address of fileserv.penguin.com and I’d like to retrieve and backup a specific directory from that remote server to a dedicated backup server with the address of backup.penguin.com.

This task can be done in several ways, but it won’t be as effective as making it all automatic without user interference now would it? ;)

Sure, you can copy files over the net with scp, but the whole point of this post is not to take any actions in order for the backup to go through, and copying with scp requires user/passphrase.

So for that Issue we need to get several things done:

on the fileserver, issue the following:

ssh-keygen -t rsa  # This will generate public/private rsa key pairs

you will be prompted for for file save, press enter.

Then, you will be prompted for passphrase, pressing enter again. this is for no passphrase (this is what we need in our case)

afterwards, you will see a message that your public/private key pair were saved (in /home/<user>/.ssh/id_rsa.pub)

Now, what we have is two different files called id_rsa and id_rsa.pub on our file server (fileserv.penguin.com)

At the moment, our task is to get this id_rsa.pub file to the computer it needs to react with, that would be our backup server.

issue the following command to get it with scp (*notice that if authorized keys file contain keys of other PCs, make sure to APPEND the key so you won’t demolish the file!)

scp /home/<user>/.ssh/id_rsa.pub your@server.ip:/home/<user>/.ssh/authorized_keys

The blue part is the remote server we ssh’d to, and the red part is our backup server.

after issuing this command you will be prompt for user credentials.

now we should be able to ssh without the server asking for a passphrase ;)

and now for the artistic part of the post:

Automating it with bash

#!/bin/bash

LOG=/home/user/backups/log
echo “backup log for `date`” >> $LOG

file_bu()
{

#Local
DIR=/var/www/
TAR=”wordpress$EXT”
EXT=`date +%F`
LDIR=/home/dolev/backups

#Remote Backup Server
SERVER=fileserv.penguin.com
RDIR=/home/fileserver-admin/backup

#MAIL Details
RCPT=admin@mail.com
SUBJ=”Backup from fileserver for `date`”

clear
cd $LDIR
echo “[1/6] Compressing wordpress directory” >> $LOG
tar cf $TAR$EXT.tar $DIR
echo “…done.” >> $LOG
echo “[2/6] Moving tar file to the local backup dir” >> $LOG
mv “$TAR$EXT.tar” $LDIR
echo “[3/6] Calculating WordPress compressed file size… `ls -lh $TAR$EXT.tar | cut -d’ ‘ -f5`” >> $LOG
echo “[4/6] Preparing to send the compressed file…” >> $LOG
echo “[5/6] Establishing a connection to the remote computer & copying files…” >> $LOG
scp  ”$LDIR/$TAR$EXT.tar” “$SERVER:$RDIR” > /dev/null 2>&1
echo “[6/6] Mailing results to admin…done” >> $LOG
mail -s $SUBJ $RCPT < $LOG
}

DAY=`date +%w`

if [ "$DAY" -eq 6 ]; then
file_bu
fi

This very simple script does the following:

  • Compresses the desired backup folder
  • SCPs it (without any user involvement)
  • Mails the size of the compressed output file together with the rest of the log to the admin’s mail address
  • executes on saturdays *
* can be changed according to your needs.
the log file will eventually look like this ( you will receive it by mail ;) )

 

Categories: Bash Scripting Tags: