LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Cut Help (https://www.linuxquestions.org/questions/programming-9/cut-help-604731/)

metallica1973 12-05-2007 07:24 PM

I will give it a shot. thanks

metallica1973 12-06-2007 05:43 AM

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

ilikejam 12-06-2007 05:47 AM

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

metallica1973 12-06-2007 06:16 AM

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

ghostdog74 12-06-2007 06:27 AM

Code:

nslookup S00CBBF.aa.bb.ccc.edu | nawk 'BEGIN{FS=":"}
 /Name:/ { gsub(/\.[a-zA-Z0-9]+\.[a-zA-Z0-9]+$| /,"",$2) ;print $2
} '


metallica1973 12-06-2007 06:38 AM

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

ilikejam 12-06-2007 07:14 AM

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.

metallica1973 12-06-2007 07:30 AM

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 


ilikejam 12-06-2007 07:48 AM

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.

metallica1973 12-06-2007 08:25 AM

Do not ask me why but the scipts works. I am testing it now and it validity. I will get back to you shortly!

ilikejam 12-06-2007 12:43 PM

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

metallica1973 12-06-2007 01:08 PM

It seems to be working, try it out.

ilikejam 12-06-2007 01:41 PM

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

metallica1973 12-07-2007 05:37 AM

You are right. when it finds an open telnet session/port 23 it hangs. what to do!

ilikejam 12-07-2007 06:57 AM

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


All times are GMT -5. The time now is 04:54 AM.