LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-24-2008, 07:48 AM   #1
metallica1973
Senior Member
 
Registered: Feb 2003
Location: Washington D.C
Posts: 2,190

Rep: Reputation: 60
Counters and Shell Script Using KSH Shell


I have a Korn shell script that I wanted to incorporate a counter into it to count how many devices I have to produce an output from:

PHP Code:
             Ping    Telnet  Logable
NAM               Status  (Nmap)  (Expect
================================================= 
192.12.58.117     up      open    No 
192.18.58.118     up      open    No 
192.95.96.119     up      open    No 
192.46.96.120     up      open    No 
192.31.210.122    up      open    No 
192.31.210.123    up      open    No 
192.54.210.125    up      open    No 
to:

PHP Code:
                  Ping    Telnet  Logable
Devices   NAM               Status  (Nmap)  (Expect
================================================= 
1         192.12.58.117     up      open    No 
2         192.18.58.118     up      open    No 
3         192.95.96.119     up      open    No 
4         192.46.96.120     up      open    No 
5         192.31.210.122    up      open    No 
6         192.31.210.123    up      open    No 
7         192.54.210.125    up      open    No 
How would I do so? Here is the script:

PHP Code:
#!/usr/bin/ksh 

################################################################################ 
# FUNCTIONS                                                                    # 
################################################################################ 

expect_session() 

echo 
" "  >> $logdata 
echo " " $logOK # Do not append to this logfile... 
# Open a telnet session to a remote server, and wait for a username prompt. 
expect << DONE >>$logdata  
spawn telnet 
-e~ -l$userid $ip_address 
expect 

       
Password: { 
                 
send "~" 
                 
sleep 1 
                 log_file 
-a $logOK 
                 send_log 
"$ip_address Yes\n"  
                 
log_file 
                 exp_continue 
       
timeout 
                 
log_file -a $logOK 
                 send_log 
"$ip_address No\n" 
                 
log_file   
                 
exit 
       } 
"closed by foreign host" 
                 
log_file -a $logOK 
                 send_log 
"$ip_address No\n" 
                 
log_file 
                 
exit 
       } 
"Connection refused" 
                 
log_file -a $logOK 
                 send_log 
"$ip_address No\n" 
                 
log_file 
                 
exit 
       } -
exact telnet>  { 
                 
send "quit\n" 
                 
sleep 1 
                 
exit 
       }  
DONE 


detail_line() 

results=` ping $ip_address | grep alive 
if [ 
"X$results"X" 
then 
    ping_able
=down 
    telnet_able
=No 
    log_able
=No 
else 
    
ping_able=up 
    telnet_able
=`nmap -P0 -sT -p23 $ip_address | grep telnet | cut -d' ' -f2 
    if [ 
"$telnet_able"open" 
    
then 
       expect_session 
#      Check the log file resulting from login session. 
       
log_able=` cat $logOK | grep $ip_address | cut -d' ' -f2 
    
fi 
fi 

if [ $ver NONE 
then 
   printf 
"%-25s%-8s%-10s %-4s\n"   
       
$ip_address $ping_able $telnet_able $log_able >> $check_log 
else 
   
printf "%-25s%-8s%-8s%-10s %-4s\n"   
       
$ip_address $ver $ping_able $telnet_able $log_able >> $check_log 
fi 


################################################################################ 
# MAIN                                                                         # 
################################################################################ 
clear 
echo "Daily NAM/SNIFFER Probe ... Please wait."  
userid=metallica1973 
check_log
=check_nodes.log  
logdata
=nodes.login.data 
logOK
=nodes.login.status 
mail_to
=metallica1973@mindyourbizz@aol.com 
echo " " $check_log  
echo " " $logata 
echo " " $logOK 
echo "Daily NAM/SNIFFER Probe for - `date`" >> $check_log  
echo " " >> $check_log  
printf 
"                                 Ping    Telnet  Logable?\n" >> $check_log  
printf 
"NAM                     Version  Status  (Nmap)  (Expect)\n" >> $check_log  
printf 
"=========================================================\n" >> $check_log 

nodes=NAM_SHORT 
cat $nodes 
| while read data_line   
do 
   
ip_address=`echo $data_line | cut -d' ' -f1`  
   
ver=`echo $data_line | cut -d' ' -f2`   
   if [ 
"X$data_line"X" 
   
then 
      
continue 
   
fi 
   detail_line 
done 
echo "  " >> $check_log 

printf "                         Ping    Telnet  Logable?\n" >> $check_log 
printf 
"SNIFFERS                 Status  (Nmap)  (Expect)\n" >> $check_log 
printf 
"=================================================\n" >> $check_log 

nodes=sniffer_list.txt 
cat $nodes 
| while read data_line  
do 
   
ip_address=` echo $data_line 
   
ver=NONE 
   
if [ "X$data_line"X" 
   
then 
      
continue 
   
fi 
   detail_line 
done 
echo "  " >> $check_log 
echo "Mailing scan results to $mail_to." 
mailx -s"Daily IP Address Activity Report" $mail_to $check_log 
echo "Done!!!" 
exit 

Last edited by metallica1973; 01-24-2008 at 09:47 AM.
 
Old 01-24-2008, 09:45 AM   #2
metallica1973
Senior Member
 
Registered: Feb 2003
Location: Washington D.C
Posts: 2,190

Original Poster
Rep: Reputation: 60
I had to throw in some "let" statements:

PHP Code:

let device
=0
let device
="device + 1" 

PHP Code:
#!/usr/bin/ksh  

################################################################################  
# FUNCTIONS                                                                    #  
################################################################################  
#  
expect_session()  
{  
echo 
" "  >> $logdata  
echo " " $logOK # Do not append to this logfile...  
# Open a telnet session to a remote server, and wait for a username prompt.  
expect << DONE >>$logdata   
spawn telnet 
-e~ -l$userid $ip_address  
expect 
{  
       
Password: {  
                 
send "~"  
                 
sleep 1  
                 log_file 
-a $logOK  
                 send_log 
"$ip_address Yes\n"   
                 
log_file  
                 exp_continue  
       
timeout {  
                 
log_file -a $logOK  
                 send_log 
"$ip_address No\n"  
                 
log_file    
                 
exit  
       } 
"closed by foreign host" {  
                 
log_file -a $logOK  
                 send_log 
"$ip_address No\n"  
                 
log_file  
                 
exit  
       } 
"Connection refused" {  
                 
log_file -a $logOK  
                 send_log 
"$ip_address No\n"  
                 
log_file  
                 
exit  
       } -
exact telnet>  {  
                 
send "quit\n"  
                 
sleep 1  
                 
exit  
       }   
DONE  
}  
#  
detail_line()  
{  
results=` ping $ip_address | grep alive `  
let device="device + 1"
if [ "X$results"X" ]  
then  
    ping_able
=down  
    telnet_able
=No  
    log_able
=No  
else  
    
ping_able=up  
    telnet_able
=`nmap -P0 -sT -p23 $ip_address | grep telnet | cut -d' ' -f2 `  
    if [ 
"$telnet_able"open" ]  
    
then  
       expect_session  
#      Check the log file resulting from login session.  
       
log_able=` cat $logOK | grep $ip_address | cut -d' ' -f2 `  
    
fi  
fi  
#  
if [ $ver NONE ]  
then  
   printf 
"%-25s%-8s%-10s %-4s\n"    
       
$ip_address $ping_able $telnet_able $log_able >> $check_log  
else  
   
printf "%-25s%-8s%-8s%-10s %-4s\n"    
       
$ip_address $ver $ping_able $telnet_able $log_able >> $check_log  
fi  
}  
#  
################################################################################  
# MAIN                                                                         #  
################################################################################  
clear  
echo "Daily NAM/SNIFFER Probe ... Please wait."   
userid=metallica1973  
check_log
=check_nodes.log   
logdata
=nodes.login.data  
logOK
=nodes.login.status  
mail_to
=metallica1973@mindyourbizz.daman.com  
echo " " $check_log   
echo " " $logata  
echo " " $logOK  
echo "Daily NAM/SNIFFER Probe for - `date`" >> $check_log   
echo " " >> $check_log   
printf 
"                                 Ping    Telnet  Logable?\n" >> $check_log   
printf 
"NAM                     Version  Status  (Nmap)  (Expect)\n" >> $check_log   
printf 
"=========================================================\n" >> $check_log  
#  
nodes=NAM_SHORT  
let device
=0
cat $nodes 
| while read data_line    
do  
   
ip_address=`echo $data_line | cut -d' ' -f1`   
   
ver=`echo $data_line | cut -d' ' -f2`    
   if [ 
"X$data_line"X" ]  
   
then  
      
continue  
   
fi  
   detail_line  
done  
echo "  " >> $check_log  
#  
printf "                         Ping    Telnet  Logable?\n" >> $check_log  
printf 
"SNIFFERS                 Status  (Nmap)  (Expect)\n" >> $check_log  
printf 
"=================================================\n" >> $check_log  
#  
nodes=sniffer_list.txt  
let device
=0
cat $nodes 
| while read data_line   
do  
   
ip_address=` echo $data_line `  
   
ver=NONE  
   
if [ "X$data_line"X" ]  
   
then  
      
continue  
   
fi  
   detail_line  
done  
echo "  " >> $check_log  
echo "Mailing scan results to $mail_to."  
mailx -s"Daily IP Address Activity Report" $mail_to $check_log  
echo "Done!!!"  
exit 
and it produced the output of :

PHP Code:
                            Ping    Telnet  Logable?  
Devices   NAM               Status  (Nmap)  (Expect)  
=================================================  
1         192.12.58.117     up      open    No  
2         192.18.58.118     up      open    No  
3         192.95.96.119     up      open    No  
4         192.46.96.120     up      open    No  
5         192.31.210.122    up      open    No  
6         192.31.210.123    up      open    No  
7         192.54.210.125    up      open    No 
Good Stuff

Last edited by metallica1973; 01-24-2008 at 09:52 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Shell script inside shell script treotan Linux - General 4 02-19-2009 06:34 AM
Shell Scripting: Getting a pid and killing it via a shell script topcat Programming 15 10-28-2007 02:14 AM
I made a shortcut to a shell script and it is using default shell icon... shlinux Linux - Software 2 04-20-2006 06:29 AM
Alias or shell script to confirm 'exit' commands from a shell rose_bud4201 Programming 2 03-08-2006 02:34 PM
shell script problem, want to use shell script auto update IP~! singying304 Programming 4 11-29-2005 05:32 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:17 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration