Hi everyone!
I've temporarily come out of hibernation (and will be gone for about two weeks after this post too) to ask for input on a small PHP script I have just completed.
The script aims to be a remote front-end for Nmap - now for the safety of this post, I ask that any replies refrain from saying anything related to (attack) hacking - god knows Neo's never taken it lightly when a newbie starts spewing his guts (and i'm not challenging his justification - it's his forum, after all).
the tarball is located at:
(i had to make a geocities account for this - it better be worth it! )
i was trying to teach my self bash scripting, just to get a comfortable with it anyway and i wrote a script that uses nmap. its very simple, and the only real thing i learned with it is how to use command line options. anyway it was fun to write and i do use it as an easy way to see if my server is up. right now this script only tests to see if a few services are running, and one fake service just for good measure for my self to see if its working. im gonna add more probably, and try to find a way to remove the dependancy on nmap. its easy to add more, if you want to see it post back and i will post the script. BTW my script has nothing to do with hacking, i just wrote it as an easy way to make sure all services on my server are up.
ah what the hell ill post it. all comments are stripped from the script to save space, everything is very self explanitory as this is very basic.
serverstatus.sh
#!/bin/bash
echo "Starting server stat..."
ftp_func2 ()
{
if echo $a | grep 21 > /dev/null
then
echo "ftp is running"
else
echo "ftp is not running"
fi
}
ssh_func2 ()
{
if echo $a | grep 22 > /dev/null
then
echo "ssh is running"
else
echo "ssh is not running"
fi
}
fake_func2 ()
{
if echo $a | grep testing > /dev/null
then
echo "fake service running" # shouldnt be
else
echo "fake service not running"
fi
}
telnet_func2 ()
{
if echo $a | grep 23 > /dev/null
then
echo "telnet is running"
else
echo "telnet is not running"
fi
}
smtp_func2 ()
{
if echo $a | grep 25 > /dev/null
then
echo "smtp is running"
else
echo "smtp is not running"
fi
}
http_func2 ()
{
if echo $a | grep 80 > /dev/null
then
echo "http is running"
else
echo "http is not running"
fi
}
pop_func2 () # not 2, not 4, but 3
{
if echo $a | grep 110 > /dev/null
then
echo "pop3 is running"
else
echo "pop3 is not running"
fi
}
non_interactivemode_func ()
{
ftp_func2
ssh_func2
fake_func2 # this is just to make me feel better
telnet_func2
smtp_func2
http_func2
pop_func2
}
version_func ()
{
echo "Serverstat 0.9"
echo "Copyright 2003 Joseph Oldak."
echo "This is free software; There is NO warranty; not even for MERCHANTABILITY or"
echo "FITNESS FOR A PARTICULAR PURPOSE. -taken from GNU grep -V"
echo "This script was written soley for my own use and is extremely simple."
echo "It is more of an exercise for my self to learn BASH shell scripting."
}
help_func ()
{
echo "Serverstat 0.9"
echo "This script checks to see what services are running on a given host."
echo "You must run serverstat with the -s option to tell it what host to scan."
echo "For now, this script uses nmap, however in the future it wont require it."
echo "The options i or n are required, one or the other."
echo "Example: ./serverstat -si somehostname.com"
echo "-s Specify hostname, either in dotted quad, or a FQDN."
echo "-i Interactive mode, select what to check is running."
echo "-n Non-Interactive mode, scans everything. Mainly for use in other scripts"
echo "-v Output version information."
echo "-h Output usage (this help message)."
}
interactivemode_func ()
{
clear
echo "Select which services you wish to check status of : "; echo $hostname2
echo "1. ftp"; echo "$b"
echo "2. ssh"; echo "$c"
echo "3. telnet"; echo "$e"
echo "4. smtp"; echo "$f"
echo "5. http"; echo "$g"
echo "6. pop3"; echo "$p"
echo "7. exit server status"
echo "select from 1-7"
read option_number
case "$option_number" in
1 )
ftp_func
;;
2 )
ssh_func
;;
3 )
telnet_func
;;
4 )
smtp_func
;;
5 )
http_func
;;
6 )
pop_func
;;
7 )
echo " Quiting ..."
exit 0
;;
* )
echo "please select 1-7"
interactivemode_func
;;
esac
}
main ()
{
NO_ARGS=0
E_OPTERROR=65
if [ $# -eq "$NO_ARGS" ] # should check for no arguments
then
echo "Usage: `basename $0` -s<OPTIONS> <HOSTNAME> "
echo "You must specify interactive, or non interactive mode for now"
echo "Try './serverstatus -h' for more information."
exit $E_OPTERROR
fi
while getopts ":sinvh" Option
do
case $Option in
s )
hostname=`echo $@`
hostname2=`echo $hostname|awk '{print $2}'`
echo $hostname2
a=`nmap $hostname2`
;;
i )
interactivemode_func
;;
n )
non_interactivemode_func
;;
v )
version_func
;;
h )
help_func
;;
* )
echo "Unimplemented option chosen"
;;
esac
done
shift $(($OPTIND - 1))
}
# ok this is retarded ...
ftp_func ()
{
if echo $a | grep 21 > /dev/null
then
echo "ftp is running"
b=`echo ftp is running`
else
echo "ftp is not running"
b=`echo ftp is not running`
fi
interactivemode_func
}
ssh_func ()
{
if echo $a | grep 22 > /dev/null
then
echo "ssh is running"
c=`echo ssh is running`
else
echo "ssh is not running"
c=`echo ssh is not running`
fi
interactivemode_func
}
fake_func ()
{
if echo $a | grep testing > /dev/null
then
echo "fake service running" # shouldnt be
d=`echo fake service is running`
else
echo "fake service not running"
d=`echo fake service is not running`
fi
interactivemode_func
}
telnet_func ()
{
if echo $a | grep 23 > /dev/null
then
echo "telnet is running"
e=`echo telnet is running`
else
echo "telnet is not running"
e=`echo telnet is not running`
fi
interactivemode_func
}
smtp_func ()
{
if echo $a | grep 25 > /dev/null
then
echo "smtp is running"
f=`echo smtp is running`
else
echo "smtp is not running"
f=`echo smtp is not running`
fi
interactivemode_func
}
http_func ()
{
if echo $a | grep 80 > /dev/null
then
echo "http is running"
g=`echo http is running`
else
echo "http is not running"
g=`echo http is not running`
fi
interactivemode_func
}
pop_func () # not 2, not 4, but 3
{
if echo $a | grep 110 > /dev/null
then
echo "pop3 is running"
p=`echo pop3 is running`
else
echo "pop3 is not running"
p=`echo pop3 is not running`
fi
interactivemode_func
}
main "$@"
Credit?! of course! i mentioned in the header of index.php that anyone who contributed would be given proper credit! tell me what you want said! e-mail address, ICQ UIN, url, name, street, phone number, fax, SIN, whatever you want.
While i don't really believe in intellectual property, i *strongly* believe in giving proper credit.
And so does my hybernation begin again - see you in 2 wks... hopefully with your script included.