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.