Archive for the ‘Scripts’ Category

Calculate Free Space Using Python

Here is an easy way of finding the total amount of free disk space using Python:

import os
diskSpace = os.statvfs(‘/’)
(diskSpace.f_bavail * diskSpace.f_frsize) / (1024 * 1024)

Random password generation with Perl

This script will generate new passwords for servers. This will take a list of servers from one file, generate a new password, then output the name of the server along with the password to a seperate file. In the new password file, it also generates code to reset the password for the account. The output will look like:

testserver #]biV}]F!?u
echo -e ‘#]biV}]F!?u\n#]biV}]F!?u’; history -c | passwd –stdin root

Copy and paste the whole echo command into the terminal. This will change the root password and then clear the history file. After all of the passwords have been change, run:
grep -v echo > newpassfile.txt
Now you have an easy to read password file to store for archives.

#!/usr/bin/perl -w

#############################################################
#Version 1.0
#Copyright (c) Jason C. Brown (2009)
#
#Released under the GNU GPLv3 license
#————————————
#This program is free software: you can redistribute it and/or modify
#it under the terms of the GNU General Public License as published by
#the Free Software Foundation, either version 3 of the License, or
#(at your option) any later version.
#
#This program is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
#GNU General Public License for more details.
#
#You should have received a copy of the GNU General Public License
#along with this program. If not, see .
#############################################################

system(“clear”);
print “Server File: “;
$SERVERFILE = <>;
print “Password File: “;
$USRPASSFILE = <>;
print “Length of password: “;
$passlen = <>;
open(SERVFILE, “$SERVERFILE”) || die “$!”;
open(PASSFILE, “>$USRPASSFILE”) || die “$!”;
sub passgen()
{
@char = (‘a’..’z',’A’..’Z',’1′..’9′,’!',’@',’#',’

,’%',’^',’&’,'*’,'(‘,’)',’-',’_',’=',’+',’<’,'>’,’[',']‘,’{‘,’}',’:',’;',’?',’|');

$len = $passlen;
$ranpass = “”;
for (0..$len) {
$ranpass .= $char[int rand @char];
}
return $ranpass;
}
$i = 0;
foreach() {
$pass = passgen();
chomp($_) =~ /(.+)/;
print PASSFILE “$_\t$pass\n\techo -e \’$pass\\n$pass\’; history -c | passwd –stdin root\n”;
$i++;
}

close(SERVFILE);
close(PASSFILE);

RAID Health Check

Here is a Bash Script that checks the status of a Linux software RAID using MDADM and Smart.

#!/bin/bash
#A Bash Script that will check the status of a Linux software RAID
#Version 1.0
#Copyright (c) Jason C. Brown (2009)
#Released under the GNU GPLv3 license
#Tested under RHEL 5

DAY=$(date +%D)
RAID=md0
HD=$(/sbin/mdadm –detail /dev/$RAID | grep active | awk -F ” ” ‘{ print $7 }’ | sed ’s/\/dev\///g’)
LEVEL=$(/sbin/mdadm –detail /dev/$RAID | grep “Raid Level” | awk -F “:” ‘{ print $2 }’)
HN=$(uname -n | awk -F “.” ‘{ print $1 }’)
SYSUPTIME=$(uptime | awk ‘{ print $3 ” ” $4 ” ” $5 ” hours”}’ | sed ’s/\,//g’)

echo “”

echo “RAID health check for” $HN “on” $DAY
echo “System uptime: ” $SYSUPTIME
echo “”
echo “File System”
df -H | grep -vE ‘^Filesystem|tmpfs|cdrom’ | awk ‘{ print $1 ” ” $5 }’ | column -t
echo ” “

echo “Checking” $LEVEL “Consistency”
echo “———————————————————-”
echo $RAID
/sbin/mdadm –detail /dev/$RAID | grep -E ‘State|Checksum|Device Size|Raid Devices|Active Devices|Working Devices|Failed Devices
|Persistence’ | grep -v ” Number Major Minor RaidDevice State”
echo “”

for drive in $HD;
do
echo $drive;
/sbin/mdadm –examine /dev/$drive | grep -E ‘State|Checksum|Device Size’ | grep -v ” Number Major Minor RaidDevice
State” | column -t
/usr/sbin/smartctl -a /dev/$drive | grep ‘SMART overall-health self-assessment test result’ | awk -F ” ” ‘{ print $1 ” “
$2 ” ” $5 ” ” $6}’
/usr/sbin/smartctl -a /dev/$drive | grep ‘Temperature_Celsius’ | awk -F ” ” ‘{ print $2 ” ” $4 }’
echo “”
done

echo “I/O stats for main drive”
echo “———————————————————-”
iostat -p /dev/hdc | grep -vE ‘Linux’
echo “I/O stats for raid drive”
echo “———————————————————-”
iostat -p /dev/md0 | grep -vE ‘Linux’
Return top
Get Adobe Flash playerPlugin by wpburn.com wordpress themes