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 12-05-2007, 07:24 PM   #16
metallica1973
Senior Member
 
Registered: Feb 2003
Location: Washington D.C
Posts: 2,115

Original Poster
Rep: Reputation: 58

I will give it a shot. thanks
 
Old 12-06-2007, 05:43 AM   #17
metallica1973
Senior Member
 
Registered: Feb 2003
Location: Washington D.C
Posts: 2,115

Original Poster
Rep: Reputation: 58
That work fine for one string but this is a standard nslookup for our domain:

PHP Code:
# nslookup S00CBBF.aa.bb.ccc.edu
Server:         192.168.198.100
Address
:        192.168.198.100#53

Non-authoritative answer:
Name:   S00CBBF.aa.bb.ccc.edu
Address
192.168.2.17 
so with that in hand what additional statement would I need for this output to get

PHP Code:
S00CBBF.aa.bb 

thanks
 
Old 12-06-2007, 05:47 AM   #18
ilikejam
Senior Member
 
Registered: Aug 2003
Location: Glasgow
Distribution: Fedora / Solaris
Posts: 3,109

Rep: Reputation: 96
Quick and dirty:
Code:
nslookup S00CBBF.aa.bb.ccc.edu | grep '^Name:' | awk '{print $2}' | awk -F'.' '{for (i=1;i<=NF-2;i++) {if (i<NF-2) printf $i"."; else print $i}}'
Dave
 
Old 12-06-2007, 06:16 AM   #19
metallica1973
Senior Member
 
Registered: Feb 2003
Location: Washington D.C
Posts: 2,115

Original Poster
Rep: Reputation: 58
ilikejam, you are the biggest help but please forgive me for my extremly limited programming knowledge(I promise will study up on this stuff) but I tried to incorporate this into my little program and I cant seem to get the appropiate output, check it out!

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

$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 
" %-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 -3| head -1| grep '^Name:' | awk '{print $2}' | awk -F'.' '{for (i=1;i<=NF-2;i++) {if (i<NF-2)
 printf $i"
."; else print $i}}')"
                
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 $SNIFFILE 
>> $OUT 
This is the incorrect output:

PHP Code:
SNIFFERS            IP_Address           Status Telnet 
===========         =================    ===    ======
Test1.aa.bb.ccc.edu Test1.aa.bb          up     closed 
Test2
.bb.cc.ddd.edu Test2.bb.cc          up     closed 
Test3
.dd.ff.ggg.edu Test3.dd.ff          up     closed 
Test5
.ee.zzz.edu    Test5.ee.zz          up     closed 

I know that problem is here:

PHP Code:
IP_Address="$IP_Addr" 
I need my output to look like this:

PHP Code:
SNIFFERS     IP_Address     Status Telnet 
===========  ============   ====== ======
Test1.aa.bb  192.64.150.18   up     closed 
Test2
.bb.cc  192.54.211.3    up     closed 
Test3
.dd.ff  192.29.178.11   up     closed 
Test5
.ee.zz  192.16.114.7    up     closed 


help

Last edited by metallica1973; 12-06-2007 at 06:36 AM.
 
Old 12-06-2007, 06:27 AM   #20
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,696
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
Code:
nslookup S00CBBF.aa.bb.ccc.edu | nawk 'BEGIN{FS=":"}
 /Name:/ { gsub(/\.[a-zA-Z0-9]+\.[a-zA-Z0-9]+$| /,"",$2) ;print $2
} '
 
Old 12-06-2007, 06:38 AM   #21
metallica1973
Senior Member
 
Registered: Feb 2003
Location: Washington D.C
Posts: 2,115

Original Poster
Rep: Reputation: 58
that is another way of doing it but I still have problems with the variables in my script and I think that it is the :

PHP Code:
IP_Address="$IP_Addr" 
that is causing me this trouble. ILIKEJAM! My word of honor that I will make a contribution to this wonderful forum after this. help

Last edited by metallica1973; 12-06-2007 at 06:46 AM.
 
Old 12-06-2007, 07:14 AM   #22
ilikejam
Senior Member
 
Registered: Aug 2003
Location: Glasgow
Distribution: Fedora / Solaris
Posts: 3,109

Rep: Reputation: 96
Easy now.

I think you've just mixed up your variables. Try the following:
Code:
#!/bin/ksh
SNIFFILE="sniffer_output"
OUT="Report"
MAIL_LIST="test_admin@ccc.edu"

> $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 " %-8.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>&1
        then
           Status=up
           read foo < /dev/tcp/$SNIFFERS/23 2>/dev/null &&  Telnet=open
           IP_Addr="$(nslookup $SNIFFERS | tail -3 | grep '^Address:' | awk '{print $2}')"
                IP_Address="$IP_Addr"
                [[ -z "$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')"
                [[ -z "$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 $SNIFFILE >> $OUT
Dave

Edit: I'm assuming the snifflist is a list of fully qualified hostnames, not IP addresses.

Last edited by ilikejam; 12-06-2007 at 07:25 AM.
 
Old 12-06-2007, 07:30 AM   #23
metallica1973
Senior Member
 
Registered: Feb 2003
Location: Washington D.C
Posts: 2,115

Original Poster
Rep: Reputation: 58
I freakin love you man! next pay check it is done. Here is my finish product with the help of so many great individuals just to name a few(ilikejam,jiliagre,chrism01,acidkeypie,pixellany) keep up the great work.

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 >>$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%%.*}"
#               [[ -z "$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

      
#!/bin/ksh
SNIFFILE="sniffer_output"
OUT="Report"
MAIL_LIST="test_admin@ccc.edu"

$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 
" %-8.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>&1
        then
           Status
=up
           read foo 
< /dev/tcp/$SNIFFERS/23 2>/dev/null &&  Telnet=open
           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


paste $SNIFFILE 
>> $OUT

done


cat $NAMFILE $SNIFFILE 
>> $OUT
mailx 
-s"Daily NAM and Sniffer Report" $MAIL_LIST $OUT 

Last edited by metallica1973; 12-06-2007 at 08:09 AM.
 
Old 12-06-2007, 07:48 AM   #24
ilikejam
Senior Member
 
Registered: Aug 2003
Location: Glasgow
Distribution: Fedora / Solaris
Posts: 3,109

Rep: Reputation: 96
By the way, that script still won't work - the ksh /dev/tcp/<IP>/<port> trick only works with numerical IP addresses, not hostnames, so you'll have to have
Code:
           IP_Addr="$(nslookup $SNIFFERS | tail -3 | grep '^Address:' | awk '{print $2}')"
           read foo < /dev/tcp/$IP_Addr/23 2>/dev/null &&  Telnet=open
instead of
Code:
           read foo < /dev/tcp/$SNIFFERS/23 2>/dev/null &&  Telnet=open
           IP_Addr="$(nslookup $SNIFFERS | tail -3 | grep '^Address:' | awk '{print $2}')"
but even that doesn't work on my Solaris 10 boxes, because the read never returns when connecting to telnet hosts. Works with SSH (port 22), but not telnet.
 
Old 12-06-2007, 08:25 AM   #25
metallica1973
Senior Member
 
Registered: Feb 2003
Location: Washington D.C
Posts: 2,115

Original Poster
Rep: Reputation: 58
Do not ask me why but the scipts works. I am testing it now and it validity. I will get back to you shortly!
 
Old 12-06-2007, 12:43 PM   #26
ilikejam
Senior Member
 
Registered: Aug 2003
Location: Glasgow
Distribution: Fedora / Solaris
Posts: 3,109

Rep: Reputation: 96
I'm betting one of two things will happen - either the script will always show the telnet connection as closed, or it won't return.

Dave
 
Old 12-06-2007, 01:08 PM   #27
metallica1973
Senior Member
 
Registered: Feb 2003
Location: Washington D.C
Posts: 2,115

Original Poster
Rep: Reputation: 58
It seems to be working, try it out.
 
Old 12-06-2007, 01:41 PM   #28
ilikejam
Senior Member
 
Registered: Aug 2003
Location: Glasgow
Distribution: Fedora / Solaris
Posts: 3,109

Rep: Reputation: 96
Your telnet server(s) must print a line before the login prompt for 'read' to return. None of our do, so read just sits there waiting for a newline.

But hey, whatever works...

Dave
 
Old 12-07-2007, 05:37 AM   #29
metallica1973
Senior Member
 
Registered: Feb 2003
Location: Washington D.C
Posts: 2,115

Original Poster
Rep: Reputation: 58
You are right. when it finds an open telnet session/port 23 it hangs. what to do!

Last edited by metallica1973; 12-07-2007 at 06:09 AM.
 
Old 12-07-2007, 06:57 AM   #30
ilikejam
Senior Member
 
Registered: Aug 2003
Location: Glasgow
Distribution: Fedora / Solaris
Posts: 3,109

Rep: Reputation: 96
To be honest, I'd use nmap (packages are available at sunfreeware) - it was designed to do this sort of thing.

This is the script (well, the important bit anyway) I run from cron at 6 every morning - the output is sent to my personal email to give me an early warning in case a machine has fallen over during the night.
Code:
for go in `cat $LIST`
  do
    /usr/local/bin/nmap -P0 -sT -p22 $go 2>&1 | egrep 'closed|filtered|Failed to  resolve' > /dev/null
    if [ $? == 0 ]
        then
        echo "$go - no ssh"
    else
        echo "$go - OK"
    fi
done
$LIST is just a file with a list of hostnames. Most of our hosts have telnet switched off, so I use an open SSH port to tell if the machine's in something resembling a usable state. Just change '-p22' to '-p23' to test telnet instead.

If nmap's not an option, you could do something funky with a sleep->kill after the read in your script to see if the read process is waiting, and take that as a 'yes, telnet's running', but that's a disgusting hack and I feel dirty for even having thought of it.

Dave
 
  


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
how to cut an image ? hectorDUQUE Fedora 1 04-28-2007 09:35 PM
Cut from right to left? LocoMojo Programming 26 03-01-2007 05:14 PM
cut question krock923 Programming 1 10-19-2005 04:03 PM
Ok, maybe I'm not cut out for linux... goosegg Linux - Newbie 5 09-01-2003 03:43 PM
cut-problem dahljor Programming 2 07-08-2003 12:58 PM


All times are GMT -5. The time now is 10:05 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