LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
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-16-2008, 09:18 AM   #1
metallica1973
Senior Member
 
Registered: Feb 2003
Location: Washington D.C
Posts: 2,121

Rep: Reputation: 58
KSH & Expect Node Probe/Audit Script


Gentleman,

I know that I am a thorn in your behind but I have made super progress in my devices probing script. My script will scan several host to see if they are up/down using the nmap and then using expect to see if telnet is open and then see of there is a login prompt. Expect is a very useful utility that can be useful for other purposes as well. My question is as such:

When you run the script now you get this output which is perfectly fine:

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

                  Ping    Telnet  Logable
?
SNIFFER           Status  (Nmap)  (Expect)
=================================================
192.12.58.132     up      open    No
192.18.58.133     up      open    No
192.95.96.134     up      open    No
192.46.96.135     up      open    No
192.31.210.136    up      open    No
192.31.210.137    up      open    No
192.54.210.138    up      open    No 
I need to add another column only on the NAM section pulling from the nam_list.txt which is setup like such:

PHP Code:

192.12.58.132     NAM0      
192.18.58.133     NAM0      
192.95.96.134     NAM1      
192.46.96.135     NAM1      
192.31.210.136    NAM2      
192.31.210.137    NAM2      
192.54.210.138    NAM2 
To print a column like such so that the output is like this:

PHP Code:

                         Ping    Telnet  Logable
?
NAM             Version  Status  (Nmap)  (Expect)
=================================================
192.12.58.117   NAM0     up      open    No
192.18.58.118   NAM0     up      open    No
192.95.96.119   NAM1     up      open    No
192.46.96.120   NAM1     up      open    No
192.31.210.122  NAM2     up      open    No
192.31.210.123  NAM2     up      open    No
192.54.210.125  NAM2     up      open    No

                  Ping    Telnet  Logable
?
SNIFFER           Status  (Nmap)  (Expect)
=================================================
192.12.58.132     up      open    No
192.18.58.133     up      open    No
192.95.96.134     up      open    No
192.46.96.135     up      open    No
192.31.210.136    up      open    No
192.31.210.137    up      open    No
192.54.210.138    up      open    No 
As you can see in my script my attempt to add $ver and [[$ver == v[0-2]]
which I know my syntax and the way I am trying to do this is wrong. Here is my 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=`nmap -sP $ip_address | grep "appears to be up" `
#ver=`cat $ip_address | awk '{print $2}'` 
if [ "X$results" "X" ]
then
        ping_able
=down
        telnet_able
=No
        log_able
=No
   
#     [[$ver == v[0-2]]
        
printf "%-25s%-8s%-8s%-9s%-4s\n"  
    
$ip_address  $ver $ping_able $telnet_able $log_able >> $check_log
    
    
else    
    
printf "%-25s%-8s%-9s%-4s\n"  
          
$ip_address  $ping_able $telnet_able $log_able >> $check_log
fi

   ping_able
=up
   telnet_able
=`nmap -P0 -sT -p23 $ip_address | grep telnet | cut -d' ' -f2 `
  
# ver="$(cat $ip_address | awk '{print $2}')" 
   
if [ "$telnet_able" "open" ]
   
then
         
#login_session
         
expect_session
         
# Check the log file resulting from login session.
         
log_able=` cat $logOK | grep $ip_address | cut -d' ' -f2 `
       
# [[$ver == v[0-2]]
     
printf "%-25s%-8s%-8s%-9s%-4s\n"  
     
$ip_address  $ver $ping_able $telnet_able $log_able >> $check_log
         
      
else   log_able=No
         printf 
"%-25s%-8s%-9s%-4s\n"  
         
$ip_address $ping_able $telnet_able $log_able >> $check_log
    
   fi
}
#
################################################################################
# MAIN                                                                         #
################################################################################
clear
echo "Daily DSN and IP Address Scan... Please wait." 
userid=metallica1973
check_log
=check_nodes.log 
logdata
=nodes.login.data
logOK
=nodes.login.status
mail_to
=mind_your_darn_bizz@test.com
echo " " $check_log 
echo " " $logata
echo " " $logOK
echo "Daily DSN and IP Address Scan 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_list.txt
#ver="$(cat $nodes | awk '{print $2}')" 
cat $nodes awk '{print $1}' | while read ip_address  
do
   if [ 
"X$ip_address" "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 ip_address
do
   if [ 
"X$ip_address" "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 
This is what I am trying to say(Pseudocode)

Scan nodes from a text file -

scan sniffer from a text file -

see if devices is up or down -

see if telnet is open or closed -

if NAM device has telnet enable then attempt to get a login -

if successful then print devices is up, telnet is enabled and print NAM Version

if SNIFFER device has telnet enabled then attempt to get login -

if successful then print devices is up, telnet is enabled and do not print Version .

*** Note - Use this script at your own risk.

*** Note - NAM IOS 3.6 has a known bug for telnet and giving false positives. If you want more information search for CSCsk95563 at the Cisco site.

*** The person who posted this is not liable for any network problems or any damage caused by this script. If in doubt, ask the LinuxQuestions.org forum for any advice for any recommendations made which are then implemented.

Last edited by metallica1973; 01-18-2008 at 08:11 AM.
 
Old 01-18-2008, 06:19 AM   #2
metallica1973
Senior Member
 
Registered: Feb 2003
Location: Washington D.C
Posts: 2,121

Original Poster
Rep: Reputation: 58
Ok I have repaired my little script with the help of some co-workers. The new and improved.

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 [ "X$ver" "X" ]
then
   printf 
"%-25s%-8s%-10s%-5s\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
=metallica73@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
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="   "
   
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 
The problem I am having now is the output. The formatting is off for the sniffers.

PHP Code:
if [ "X$ver" "X" ]
then
   printf 
"%-25s%-8s%-10s%-5s\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 
output:

PHP Code:
Daily NAM/SNIFFER Probe for - Thu Jan 17 15:24:08 EST 2008

                                 Ping    Telnet  Logable
?
NAM                     Version  Status  (Nmap)  (Expect)
=========================================================
192.28.50.12             NAM0    up      open       Yes
192.13.51.12             NAM0    up      open       No
192.32.23.23             NAM1    up      open       No
192.134.52.111           NAM1    up      open       No
192.94.96.123            NAM1    up      open       No
192.22.123.124           NAM2    up      open       No
192.123.222.111          NAM2    up      open       No
192.11.22.123            NAM2    down    No         No

                         Ping    Telnet  Logable
?
SNIFFERS                 Status  (Nmap)  (Expect)
=================================================
testin1.dd.xx.xxx.xxx    up      closed  No
testin2
.dd.xx.xxx.xxx    up      closed  No
testin3
.dd.xx.xxx.xxx    up      filteredNo
testin4
.dd.xx.xxx.xxx    up      closed  No
testin5
.dd.xx.xxx.xxx    up      closed  No
testin6
.dd.xx.xxx.xxx    up      filteredNo
testin7
.dd.xx.xxx.xxx    up      closed  No 
*** The person who posted this is not liable for any network problems or any damage caused by this script. If in doubt, ask the LinuxQuestions.org forum for any advice for any recommendations made which are then implemented.

Last edited by metallica1973; 01-18-2008 at 08:12 AM.
 
Old 01-18-2008, 10:07 AM   #3
metallica1973
Senior Member
 
Registered: Feb 2003
Location: Washington D.C
Posts: 2,121

Original Poster
Rep: Reputation: 58
The issue has been resolved. ver=NONE was causing the issue.

PHP Code:

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 
So the finished product for anyone in the same situation can use my 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 
which now gives you and output:

PHP Code:
                         Ping    Telnet  Logable
SNIFFERS                 Status  (Nmap)  (Expect
================================================= 
testin1.dd.xx.xxx.xxx    up      closed   No 
testin2
.dd.xx.xxx.xxx    up      closed   No 
testin3
.dd.xx.xxx.xxx    up      filtered No 
testin4
.dd.xx.xxx.xxx    up      closed   No 
testin5
.dd.xx.xxx.xxx    up      closed   No 
testin6
.dd.xx.xxx.xxx    up      filtered No 
testin7
.dd.xx.xxx.xxx    up      closed   No 
That was one h#$@$ of a learning experience.

*** I am not liable for any network problems or any damage caused by this script. Use at your own risk. If in doubt, ask the LinuxQuestions.org forum for any advice for any recommendations made which are then implemented.

Last edited by metallica1973; 01-24-2008 at 09:08 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
JavaScript:: alert(node) shows null, but node != null taylor_venable Programming 1 05-01-2006 10:51 PM
Need Firewall Scipt PLease? extremebfn Linux - Networking 2 09-05-2004 07:23 AM
Cron & ksh: Solaris/RHEL 2 incompatibility marist89 Linux - Software 1 01-21-2004 04:48 PM
Mozilla 1.5 launcher & profiles isn't working as I expect. JordanH Linux - Software 9 12-09-2003 12:45 PM
php upload scipt error kudos Programming 8 10-02-2003 02:59 PM


All times are GMT -5. The time now is 11:53 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration