cancel
Showing results for 
Search instead for 
Did you mean: 

need a scrript

SOLVED
Go to solution
Maaz
Valued Contributor

need a scrript

please help me write a script that can

1, add the "used" and "cached" values(from the output of 'free'), and then email the value to admin@mynetworks.local

2, System Reboot instances should be reported(via email) to the sys admin, each system should be configured such that on startup it sends a notification to the System admin with following minimum details
a, Server Name and IP
b, Startup Date and Time

3, Through an automated script generate list of users files(/home/*) which fall under unauthorized or suspicious catagory. This includes but not limited to AVI, MP3, JPEG, TIF. All users found with such files should be notified(via email), also a notification should also be sent to the system admin and HR.

please help me meet the above requirements of our Auditors.
6 REPLIES
Maaz
Valued Contributor

Re: need a scrript

and last
4, check whether the time synchronization is correct with the NTP Server, if time difference is more then 1 minute an email is sent to sysadmin with details.

Thanks in anticipation
Regards
Maaz
Ivan Ferreira
Honored Contributor

Re: need a scrript

For 1)

free -m | grep Mem | awk 'TOT=$3+$6 { print TOT }' | mail -s "Memory report" admin@mynetworks.local
Por que hacerlo dificil si es posible hacerlo facil? - Why do it the hard way, when you can do it the easy way?
Ivan Ferreira
Honored Contributor
Solution

Re: need a scrript

For 2)

Create a script like this:

#!/bin/bash
DEVICE=eth0
HOSTNAME=$(hostname)
IP=$(ifconfig eth0 | grep "inet addr" | awk '{print $2}' | sed 's/addr://')
DATE=$(date)

echo "$HOSTNAME ($IP) booted at $DATE" | mail -s "$HOSTANME boot" admin@mynetworks.local


And add to /etc/rc.local.
Por que hacerlo dificil si es posible hacerlo facil? - Why do it the hard way, when you can do it the easy way?
Maaz
Valued Contributor

Re: need a scrript

for 4) I have wrote the following script, although the script works quite fine, but need suggestions/comments/help to make the script better

the only problem(I noticed) with this script is that I have to configure all the ntp clients for "password less ssh login" on the ntp server(ssh server)

#!/bin/bash

# timezone of the local network
export zone=UZT

export script=ntp_script@xyz.local

# sys admin email address
export sysadmin=abc@xyz.local

# ldate, lmonth, lyear, lhour, lminute, lzone are client's date, month, year etc
export ldate=$(date +%d)
export lmonth=$(date +%m)
export lyear=$(date +%Y)
export lhour=$(date +%k)
export lminute=$(date +%M)
export lzone=$(date +%Z)

# sdate, smonth, syear, shour, sminute, szone are the NTP Server's date, month, year etc
# ntp server ip = 192.168.0.1
export sdate=$(ssh 192.168.0.1 "date +%d")
export smonth=$(ssh 192.168.0.1 "date +%m")
export syear=$(ssh 192.168.0.1 "date +%Y")
export shour=$(ssh 192.168.0.1 "date +%k")
export sminute=$(ssh 192.168.0.1 "date +%M")
export szone=$(ssh 192.168.0.1 "date +%Z")


if [ $ldate -eq $sdate -a $lmonth -eq $smonth -a $lyear -eq $syear -a $lzone = $szone ]; then
echo "date, month, year and timezone are OK"
exit
fi

if [ $lzone != $szone -a $szone = $zone ]; then
cp /etc/localtime /tmp
rm -f /etc/localtime
ln -s /usr/share/zoneinfo/Asia/Tashkent /etc/localtime
/etc/init.d/ntp stop; sleep 3;
ntpdate 192.168.0.1 && ntpdate 192.168.0.1 && ntpdate 192.168.0.1
/etc/init.d/ntp start;
# inform the admin via email
echo "client timezone was not right, issue resolved" |mail -s "NTP issue fixed" -r $script $sysadmin <~
exit

else if [ $szone != $zone ]; then
echo "NTP Server timezone is not correct" |mail -s "NTP Server Timezone wrong" -r $script $sysadmin <~
exit
fi

fi

if [ $ldate -eq $sdate -a $lmonth -eq $smonth -a $lyear -eq $syear ]; then exit
else
/etc/init.d/ntp stop; sleep 3;
ntpate 192.168.0.1 && ntpdate 192.168.0.1 && ntpdate 192.168.0.1
/etc/init.d/ntp start;
echo "client date/time was not right, issue resolved" |mail -s "NTP issue fixed" -r $script $sysadmin <~
fi

Regards
Ivan Ferreira
Honored Contributor

Re: need a scrript

Your script check more than just the time difference, that is OK. But I'm not sure if you should check the timezone also. Timezone can be different but still work correctly, depending of your location.

See also this script:

http://www.mattiaswikstrom.net/linux/20061102-time-offset-check-script.html
Por que hacerlo dificil si es posible hacerlo facil? - Why do it the hard way, when you can do it the easy way?
Maaz
Valued Contributor

Re: need a scrript

Thanks for the quick help

> Timezone can be different but still work correctly, depending of your location.
Yes, I did this because both(client/Server) belongs to the same timezone, anyway I think timezone checking should be omitted.

and for the url, its simply great, I think I exactly need such a script that can do quite efficiently/perfectly.

Regards
Maaz