The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Korn shell interactive script leonard905 Shell Programming and Scripting 3 08-02-2007 11:16 AM
Help with Korn Shell script heprox AIX 1 12-19-2005 10:04 AM
korn shell script pavan_test UNIX Desktop for Dummies Questions & Answers 3 10-27-2005 09:09 AM
korn shell script pavan_test UNIX for Dummies Questions & Answers 1 10-26-2005 10:17 AM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 12-13-2007
Registered User
 

Join Date: Dec 2007
Posts: 4
Stumble this Post!
Korn Shell and Perl Administrative Script

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>&
        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>&
        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" 
                
[[ -"$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')" 
                
[[ -"$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.00perl -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.

PHP Code:
closed 
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
Reply With Quote
Google The UNIX and Linux Forums
Forum Sponsor
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 05:38 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0