I have this script that I want to be able to scan a list of IP address from a file (namlist and snifflist)and Hostnames of network devices to see if they are alive or down and if telnet port 23 is open. I originally started to write this in Korn Shell and ran into some issue trying to closed a telnet probe. So I was told to try Perl which I know nothing of and I am a very basic programmer with no backround in programming as well. Here is my script:
PHP Code:
#!/bin/ksh
NAMFILE="nam_output"
SNIFFILE="sniffer_output"
OUT="Report.doc"
MAIL_LIST="test_admin@ccc.edu"
> $NAMFILE
> $SNIFFILE
> $OUT
echo Daily Sniffer and NAMs Scan as of - `date` >> $OUT
echo >> $OUT
echo There is a 8 sec timeout of devices >> $OUT
echo >>$OUT
exec 2>/dev/null
printf " %-15.20s %-8s %-6s \n" NAM Status Telnet >> $NAMFILE
echo " ===============================" >>$NAMFILE
for NAM in $(<namlist)
do
Status=down
Telnet=closed
if ping $NAM 5 >/dev/null 2>&1
then
Status=up
Telnet=$(perl -e 'use IO::Socket; print new IO::Socket::INET (PeerAddr => "'$SNIFFERS'", PeerPort => "23", Timeout => 5
) ? "open\n" : "closed\n";')
READPID=$!
# sleep 15
if ps | awk '{print $1}' | grep $READPID 2>/dev/null
then
Telnet="open"
kill $READPID
fi
printf " %-15.20s %-8s %-6s \n" $NAM $Status $Telnet >> $NAMFILE
else
Telnet="n/a"
printf " %-15.20s %-8s %-6s \n" $NAM $Status $Telnet >> $NAMFILE
fi
done
echo >> $SNIFFILE
printf " %-15.20s %-15.20s %-8s %-6s \n" SNIFFERS IP_Address Status Telnet >> $SNIFFILE
echo " ===============================================" >> $SNIFFILE
for SNIFFERS in $(<snifflist)
do
TRUNCSNIF=$(echo $SNIFFERS | awk -F'.' '{for (i=1;i<=NF-2;i++) {if (i<NF-2) printf $i"."; else print $i}}')
Status=down
Telnet=closed
if ping $SNIFFERS 5 >/dev/null 2>&1
then
Status=up
Telnet=$(perl -e 'use IO::Socket; print new IO::Socket::INET (PeerAddr => "'$SNIFFERS'", PeerPort => "23", Timeout =>5
) ? "open\n" : "closed\n";')
READPID=$!
sleep 15
if ps | awk '{print $1}' | grep $READPID 2>/dev/null
then
Telnet="open"
kill $READPID
fi
IP_Addr="$(nslookup $SNIFFERS | tail -3 | grep '^Address:' | awk '{print $2}')"
IP_Address="$IP_Addr"
[[ -z "$IP_Address" ]] && IP_Address="n/a"
printf " %-15.20s %-15.20s %-8s %-6s \n" $TRUNCSNIF $IP_Address $Status $Telnet >>$SNIFFILE
else
IP_Address="$(nslookup $SNIFFERS | tail -2 |head -1 | awk -F: '{print $2}'| sed 's/ //g')"
[[ -z "$IP_Address" || "$IP_Address" == "SERVFAIL" ]] && IP_Address="n/a"
Telnet="n/a"
printf " %-15.20s %-15.20s %-8s %-6s \n" $TRUNCSNIF $IP_Address $Status $Telnet >>$SNIFFILE
fi
done
cat $NAMFILE $SNIFFILE >> $OUT
mailx -s"Daily NAM and Sniffer Report" $MAIL_LIST < $OUT
Here is what I have tested
PHP Code:
bash-3.00$ perl -e 'use IO::Socket; print new IO::Socket::INET (PeerAddr => "'192.18.158.17'", PeerPort => "23", ) ? "closed\n" : "open\n";'
and here is the output that it always give me, a closed port when I know that it is open.
This is the part that I do not understand
PHP Code:
? "open\n" : "closed\n";'
from
PHP Code:
Telnet=$(perl -e 'use IO::Socket; print new IO::Socket::INET (PeerAddr => "'$SNIFFERS'", PeerPort => "23", Timeout =>5 ) ? "open\n" : "closed\n";')
Help