LinuxQuestions.org
Visit Jeremy's Blog.
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 11-28-2007, 02:42 PM   #46
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Mint, OL
Posts: 9,486

Rep: Reputation: 354Reputation: 354Reputation: 354Reputation: 354

Quote:
Originally Posted by metallica1973 View Post
I promise I wont ask for anymore help but how can I have this script:

[php... [/php]to include the hostname of the device and have two different categories of devices like NAM's and Sniffers. like for example:

PHP Code:
 NAM             status   telnet 
 127.0.0.1       up       closed 
 192.168.1.1     up       open   
 192.168.1.2     up       closed 
 192.168.1.3     down     n
/a  

 Sniffers       status    telnet 
 127.0.0.1       up       closed 
 192.168.1.1     up       open   
 192.168.1.2     up       closed 
 192.168.1.3     down     n
/
I see in your example neither the hostname nor two different categories as both lists have the same IPs.

Anyway, for the first request, just run the script twice with a different set of adresses. For adding the hostnames, that may depend on what naming service is in use on your WAN. (plain files, DNS, NIS, LDAP, ...).
 
Old 11-29-2007, 05:30 AM   #47
metallica1973
Senior Member
 
Registered: Feb 2003
Location: Washington D.C
Posts: 2,113

Original Poster
Rep: Reputation: 57
PHP Code:
NAM            HOSTNAME     Status   Telnet  
127.0.0.1      Localhost    up       closed  
192.168.1.1    Test1        up       open    
192.168.1.2    Test2        up       closed  
192.168.1.3    Test3        down     n
/a   
 
Sniffers       Status   Telnet  
12.6.3.60      up       closed  
192.168.5.1    up       open    
192.168.8.2    up       closed  
192.168.9.3    down     n
/
That is what I need. How can this be achieved?

Last edited by metallica1973; 11-29-2007 at 05:34 AM.
 
Old 11-29-2007, 05:37 AM   #48
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Mint, OL
Posts: 9,486

Rep: Reputation: 354Reputation: 354Reputation: 354Reputation: 354
Where do Test1, Test2 and so on names come from ?
 
Old 11-29-2007, 05:55 AM   #49
metallica1973
Senior Member
 
Registered: Feb 2003
Location: Washington D.C
Posts: 2,113

Original Poster
Rep: Reputation: 57
That is just an example. Can I have this script not only ping the ip address from the namlist file which only has ip addresses and not hostnames(to many)but also include what the hostnames are by using better logic? for example when I perform ping -a using windows it will return the hostname of the device.In other words can we modify the script so that when it pings it will also include the hostname but truncates it to only to 6 characters if it is to long to keep the formating neat from the ip address from the namlist file and vice versa from the snifflist file(it only has host names, no ip address - the reason is that the ip address change and the hostnames are permanent). I see that we will have to use nslookup to get the hostname from devices!

PHP Code:
#!/bin/ksh
exec 2>/dev/null
printf 
" %-20.20s %-8s %-6s\n" NAM Status Telnet
echo "====================================="
for NAM in $(<namlist)
do
        
status=down
       telnet
=closed
        
if ping $NAM 5 >/dev/null 2>&1
        then
                status
=up
                read foo 
< /dev/tcp/$NAM/23 && telnet=open
        
else
                
telnet="n/a"
        
fi
        printf 
" %-20.20s %-8s %-6s\n" $NAM $Status $Telnet

done

echo

exec 2>/dev/null
printf 
" %-20.20s %-8s %-6s\n" SNIFFERS Status Telnet
echo "====================================="
for SNIFFERS in $(<snifflist)
do
        
status=down
        telnet
=closed
        
if ping $SNIFFERS 5 >/dev/null 2>&1
        then
                status
=up
                read foo 
< /dev/tcp/$SNIFFERS/23 && telnet=open
        
else
                
telnet="n/a"
        
fi
        printf 
" %-20.20s %-8s %-6s\n" $SNIFFERS $Status $Telnet
done 
and have it look like this:

PHP Code:
NAM          HOSTNAME  Status Telnet  Sniffers     HOSTNAME Status Telnet   
127.0.0.1    Localhost up     closed  192.168.16.4 s00cbbd  up     closed   
192.168.1.1  NAM1      up     open    192.168.16.5 s00cbbf  up     open     
192.168.1.2  NAM2      up     closed  192.168.16.6 s00cbbg  up     closed   
192.168.1.3  NAM3      down   n
/a     192.168.16.7 s00cbbj  down   n/

Last edited by metallica1973; 11-29-2007 at 08:30 AM.
 
Old 11-29-2007, 08:25 AM   #50
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Mint, OL
Posts: 9,486

Rep: Reputation: 354Reputation: 354Reputation: 354Reputation: 354
There is not necessarily a one to one correspondance between hostnames and IP addresses.

You can resolve a name to an IP with the "dig" command, eg:
Code:
$ dig +short www.linuxquestions.org
64.179.4.146
The opposite is slightly more complex and will slow down your script if some IPs can't be resolved.
 
Old 11-29-2007, 09:12 AM   #51
metallica1973
Senior Member
 
Registered: Feb 2003
Location: Washington D.C
Posts: 2,113

Original Poster
Rep: Reputation: 57
so how would I add

PHP Code:
dig +short www.linuxquestions.org
64.179.4.146 
this to my script

PHP Code:
for NAM in $(<namlist)
do
        
status=down
        telnet
=closed
        
if ping $NAM 5 >/dev/null 2>&1
        then
                status
=up
                read foo 
< /dev/tcp/$NAM/23 && telnet=open
        
else
                
telnet="n/a"
        
fi
        printf 
" %-20.20s %-8s %-6s\n" $NAM $status $telnet

done

echo

exec 2>/dev/null
printf 
" %-20.20s %-8s %-6s\n" SNIFFERS Status Telnet
echo "====================================="
for SNIFFERS in $(<snifflist)
do
        
status=down
        telnet
=closed
        
if ping $SNIFFERS 5 >/dev/null 2>&1
        then
                status
=up
                read foo 
< /dev/tcp/$SNIFFERS/23 && telnet=open
        
else
                
telnet="n/a"
        
fi
        printf 
" %-20.20s %-8s %-6s\n" $SNIFFERS $Status $Telnet
done 
and have the formating like

PHP Code:
NAM          HOSTNAME  Status Telnet  Sniffers     HOSTNAME Status Telnet    
127.0.0.1    Localhost up     closed  192.168.16.4 s00cbbd  up     closed    
192.168.1.1  NAM1      up     open    192.168.16.5 s00cbbf  up     open      
192.168.1.2  NAM2      up     closed  192.168.16.6 s00cbbg  up     closed    
192.168.1.3  NAM3      down   n
/a     192.168.16.7 s00cbbj  down   n/
I promise this is the last questions and then I will go buy a basic shell scripting book. My word of honor!

Last edited by metallica1973; 11-29-2007 at 10:36 AM.
 
Old 11-29-2007, 10:30 AM   #52
metallica1973
Senior Member
 
Registered: Feb 2003
Location: Washington D.C
Posts: 2,113

Original Poster
Rep: Reputation: 57
I really apprieciate all of your help. I think this post is too long so I start it in another post. thanks to all for your help. To finish this post with class I will post my grand creation. It still needs work. This script will scan for network devices via IP and or Hostnames and produce a nice file.

PHP Code:
#!/bin/ksh
NAMFILE="nam_output"
SNIFFILE="sniffer_output"
OUT="Report"
MAIL_LIST="test_admin@ccc.edu"

$NAMFILE
$SNIFFILE
$OUT

echo       Daily Sniffer and NAMs Scan as of `date` >> $OUT
echo       Script run with a 5 sec timeout >> $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
                read foo 
< /dev/tcp/$NAM/23 2>/dev/null && Telnet=open
#               hname="$(nslookup $NAM | tail -4 | head -1 | awk -F'=' '{print $2}'| sed 's/ //g')"
#               Hostname="${hname%%.*}"
                
[[ -"$Hostname" ]] && Hostname="n/a"
                
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

printf 
" %-8.20s %-15.20s %-8s %-6s \n" SNIFFERS IP_Address Status Telnet >> $SNIFFILE

echo "======================================================" >> $SNIFFILE

for SNIFFERS in $(<snifflist)

do
        
Status=down
        Telnet
=closed
        
if ping $SNIFFERS 5 >/dev/null 2>&1
        then
                Status
=up
                read foo 
< /dev/tcp/$SNIFFERS/23 2>/dev/null &&  Telnet=open
                IP_Addr
="$(nslookup $SNIFFERS | tail -2 |head -1 | awk -F: '{print $2}'| sed 's/ //g')"
                
IP_Address="${IP_Addr%.*}"
                
[[ -"$IP_Address" ]] && IP_Address="n/a"
                
printf " %-15.20s %-15.20s %-8s %-6s \n" $SNIFFERS $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" $SNIFFERS $IP_Address $Status $Telnet >>$SNIFFILE

        fi

done


cat $NAMFILE $SNIFFILE 
>> $OUT
mailx 
-s"Daily NAM and Sniffer Report" $MAIL_LIST $OUT 
It still needs a lot of work and use at your own risk.

Last edited by metallica1973; 12-05-2007 at 12:33 PM.
 
Old 12-19-2007, 11:39 AM   #53
metallica1973
Senior Member
 
Registered: Feb 2003
Location: Washington D.C
Posts: 2,113

Original Poster
Rep: Reputation: 57
ok I was able to get nmap so can you please modify the current script to include nmap and then I will cry tears of joy!
 
Old 12-19-2007, 12:38 PM   #54
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 62
Post #6 in this thread was me saying "nmap will do what you want"
 
Old 12-19-2007, 07:59 PM   #55
metallica1973
Senior Member
 
Registered: Feb 2003
Location: Washington D.C
Posts: 2,113

Original Poster
Rep: Reputation: 57
from here, how would I plug that in to get it to work.

PHP Code:
nmap -sP -iL hostlist.txt 
inside of this

PHP Code:
#!/bin/ksh
NAMFILE="nam_output"
SNIFFILE="sniffer_output"
OUT="Report"
MAIL_LIST="test_admin@ccc.edu"

$NAMFILE
$SNIFFILE
$OUT

echo       Daily Sniffer and NAMs Scan as of `date` >> $OUT
echo       Script run with a 5 sec timeout >> $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
                read foo 
< /dev/tcp/$NAM/23 2>/dev/null && Telnet=open
#               hname="$(nslookup $NAM | tail -4 | head -1 | awk -F'=' '{print $2}'| sed 's/ //g')"
#               Hostname="${hname%%.*}"
                
[[ -"$Hostname" ]] && Hostname="n/a"
                
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

printf 
" %-8.20s %-15.20s %-8s %-6s \n" SNIFFERS IP_Address Status Telnet >> $SNIFFILE

echo "======================================================" >> $SNIFFILE

for SNIFFERS in $(<snifflist)

do
        
Status=down
        Telnet
=closed
        
if ping $SNIFFERS 5 >/dev/null 2>&1
        then
                Status
=up
                read foo 
< /dev/tcp/$SNIFFERS/23 2>/dev/null &&  Telnet=open
                IP_Addr
="$(nslookup $SNIFFERS | tail -2 |head -1 | awk -F: '{print $2}'| sed 's/ //g')"
                
IP_Address="${IP_Addr%.*}"
                
[[ -"$IP_Address" ]] && IP_Address="n/a"
                
printf " %-15.20s %-15.20s %-8s %-6s \n" $SNIFFERS $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" $SNIFFERS $IP_Address $Status $Telnet >>$SNIFFILE

        fi

done


cat $NAMFILE $SNIFFILE 
>> $OUT
mailx 
-s"Daily NAM and Sniffer Report" $MAIL_LIST $OUT 
I need to keep the same formatting

Last edited by metallica1973; 12-19-2007 at 08:08 PM.
 
  


Reply

Tags
nmap, ping, scan, security


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
writing a shell script for scheduled pinging laucian Linux - Newbie 12 11-08-2007 05:48 AM
script for pinging servers steste Linux - Networking 12 11-16-2004 09:38 AM
Help writing a pinging bash script dehuszar Linux - Software 3 06-18-2004 02:03 PM
Network not pinging any more. duffboygrim Linux - Networking 2 03-30-2004 05:05 PM
bash script prob: pinging boxes bdp Programming 9 02-20-2004 01:50 AM


All times are GMT -5. The time now is 12:52 AM.

Main Menu
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