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 12:49 PM

Cut Help using Solaris 10
 
How can use the cut command to cut the last two octets or hostname from a string like for example:

PHP Code:

S00CBBF.aa.bb.ccc.edu 

To

PHP Code:

S00CBBF.aa.bb 

Thanks

schneidz 12-05-2007 01:12 PM

i dunno' if there is a simpler way but this works:
Code:

echo S00CBBF.aa.bb.ccc.edu | rev | cut -d . -f 3- | rev

pixellany 12-05-2007 01:12 PM

Why not SED. This is crude, but works:

sed 's/\....\....$//' filename

Translation: find a literal ".", any 3 characters, another literal ".", 3 more characters, and then the end of the line. Replace by an empty string.

metallica1973 12-05-2007 01:13 PM

I am using Sun Solaris 10. Is that command 'rev' still valid?

schneidz 12-05-2007 01:32 PM

Quote:

Originally Posted by metallica1973 (Post 2981231)
I am using Sun Solaris 10. Is that command 'rev' still valid?

i guess not:
Code:

Sun Microsystems Inc.  SunOS 5.8      Generic Patch  February 2004
schneidz@lq:/temp> echo hello-world | rev
ksh: rev:  not found

try compiling yourself one of these:
Code:

schneidz@lq:/temp> cat ssh-rev.c
#include <string.h>
#include <stdio.h>


char *str_rev(char *s)
    {
    char *p=s;
    char *q =s;
    char swap;
    if (*s)
    {
        q=strchr(q,'\0');
        while (--q > p)
        {
            swap = *q;
            *q = *p;
            *p = swap;
            ++p;
        }
    }
    return s;
}

main(int argc, char * argv[])
{
 char * loc1;
 char * loc2;
 int locdiff, argv2strlen;
 int pos = 0;

  argv[1] = str_rev(argv[1]);
  loc1 = strchr(argv[1], argv[1][0]);
  loc2 = strchr(argv[1], '/');
  locdiff = loc2 - loc1;
  argv[1][locdiff] = '\0';
  printf(" %s \n", str_rev(argv[1]));
}


ilikejam 12-05-2007 02:20 PM

awk for the win.
Code:

awk -F'.' '{for (i=1;i<=NF-2;i++) {if (i<NF-2) printf $i"."; else print $i}}'
Dave

chrism01 12-05-2007 04:50 PM

echo S00CBBF.aa.bb.ccc.edu |cut -d'.' -f1-3

gives
S00CBBF.aa.bb

too easy ;) and shorter / clearer than awk

ilikejam 12-05-2007 04:53 PM

Quote:

Originally Posted by chrism01 (Post 2981417)
echo S00CBBF.aa.bb.ccc.edu |cut -d'.' -f1-3

gives
S00CBBF.aa.bb

too easy ;) and shorter / clearer than awk

Bzzzzt. The OP wanted rid of the last two fields. What if you don't know in advance how many fields there are in the string?

unSpawn 12-05-2007 05:21 PM

Quote:

Originally Posted by ilikejam (Post 2981423)
Bzzzzt. The OP wanted rid of the last two fields. What if you don't know in advance how many fields there are in the string?

OK, BASH prolly isn't what you're looking for but w/o 'cut': i="S00CBBF.aa.bb.ccc.edu"; i=(${i//./ }); echo "${i[0]}.${i[1]}"

ilikejam 12-05-2007 05:26 PM

No joy, I'm afraid:
Code:

[0 dave@cronus ~]$ i="S00CBBF.aa.bb.ccc.edu"; i=(${i//./ }); echo "${i[0]}.${i[1]}"
S00CBBF.aa

Should give: S00CBBF.aa.bb , and you'd still have to know in advance how many fields there are in any case.

awk's still in front. Go awk! etc.

Anyone got any perl?

Dave

ghostdog74 12-05-2007 05:28 PM

Code:

# echo S00CBBF.aa.bb.ccc.edu | nawk 'gsub(/\.[a-zA-Z0-9]+\.[a-zA-Z0-9]+$/,"")'

unSpawn 12-05-2007 05:38 PM

heh. way too much work: i="S00CBBF.aa.bb.ccc.edu"; i=(${i//./ }); n=${#i[@]}; unset i[$[$n-1]] i[$[$n-2]]; i=${i[*]}; echo ${i// /.}

chrism01 12-05-2007 06:26 PM

Well, given the OP's example input, 'removing the last 2 fields' and 'keeping the first 3' both give his example answer. The qn is, what does he REALLY need??

metallica1973 12-05-2007 07:04 PM

Everyone please read my original post so that all of you can understand my situation.

http://www.linuxquestions.org/questi...-shell-604725/

The output will always be in this format:

PHP Code:

S00F3456.aa.bb.cc.dd.ee.fff.edu 

or


PHP Code:

S00F3456.aa.bb.cc.dd.fff.edu 

We use unix for DNS and we have to use the fully qualified name so what I want is from the output above, logic that can distinguish different fields in the output and remove the last 2 octects, always. so I would need an output like


PHP Code:

S00F3456.bb.cc.dd 

or

PHP Code:

S00F3456.bb.cc.dd.ee 

the last will need to truncated no matter how long the output is!

ilikejam 12-05-2007 07:20 PM

So, basically, you need a function which will remove the last two 'between-dots' fields from your hostname string.

In that case,
Code:

awk -F'.' '{for (i=1;i<=NF-2;i++) {if (i<NF-2) printf $i"."; else print $i}}'
will do it, e.g.
Code:

[0 dave@cronus ~]$ echo S00F3456.aa.bb.ccc.edu | awk -F'.' '{for (i=1;i<=NF-2;i++) {if (i<NF-2) printf $i"."; else print $i}}'
S00F3456.aa.bb
[0 dave@cronus ~]$ echo S00F3456.bb.ccc.edu | awk -F'.' '{for (i=1;i<=NF-2;i++) {if (i<NF-2) printf $i"."; else print $i}}'
S00F3456.bb
[0 dave@cronus ~]$ echo S00F3456.aa.bb.xyz.abc.ccc.edu | awk -F'.' '{for (i=1;i<=NF-2;i++) {if (i<NF-2) printf $i"."; else print $i}}'
S00F3456.aa.bb.xyz.abc

Dave

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

metallica1973 12-07-2007 07:12 AM

I work in a very stick environment and they have refused me before. Is there a way that we can throw in a ^c to cancel the login the telnet session and add that to the script?

ilikejam 12-07-2007 07:37 AM

It's pretty horrible, but here goes. There's probably a better way to do this, so feel free to jump in, anyone.

Code:

read foo < /dev/tcp/$SNIFFERS/23 2>/dev/null &
READPID=$!
sleep 2
if ps | awk '{print $1}' | grep $READPID &> /dev/null
    then
    Telnet=open
    kill $READPID
fi

Basically, we're starting the read, waiting 2 seconds, then looking at the ps output to see if the read process is still running. If it is, then we assume telnet's running on the remote side We kill the read process and continue.

There's an obvious problem with this, though. If network congestion gets bad, you may get false positives, as the read may be waiting for the network, rather than waiting with an open telnet connection. It's also slow - it'll take an extra 2 seconds per host, if the host has telnet running.

Use nmap if at all possible.

Dave

metallica1973 12-07-2007 01:17 PM

I had to add a sleep 10 second for it work on my script. That is ok but is there a way to make this more efficient?

metallica1973 12-07-2007 02:08 PM

I retried the script and it was giving false positives. help!

ilikejam 12-07-2007 02:47 PM

I don't think you're going to get round this without some software. Could you see if you can install 'Netcat' or 'nmap'? They might be willing to let Netcat on if they don't like the sound of nmap.

Or maybe you could sneak a Netcat binary into your ~/bin directory? It's only 26kB - they'll never notice ;) (Usual disclaimer: If the admin finds out, and beats you to death with a copy of 'The Practice of System and Network Administration (Limoncelli, Hogan 2001)', then we never had this conversation.)

Dave

ghostdog74 12-07-2007 07:08 PM

@OP, if your project is legit, there's no reason why admin won't install these tools for your usage. After all, it should be approved by your management. if its legit, it may be worth a try doing some Perl/python socket programming.

metallica1973 12-08-2007 11:22 AM

I asked another admin and he said that will not let me use nmap or netcat. I will ask my management and see if I can use those utility if it will make my life easier. With that being said I will ask them on Monday. I will get back to you and once again all of you deserve all the credit. Many thanks.

chrism01 12-08-2007 05:44 PM

hopefully management will see the diff between adding authorised tools when you need them and unnecessary paranoia...
Otherwise you might be better of going with eg Perl, which has socket modules as part of the core install. Use a forking soln?

PAix 12-08-2007 07:32 PM

I well remember that my SysAdmins were very against the development dept using top on SunOS/Solaris in the early 1990's as it was downloaded and compiled and not in the distibution as originally loaded. Six months later after much arguing with management it was accepted as being the standard tool that it has become and all the fuss forgotten. God forbid, the SysAdmins discovered SAR for performance monitoring which was a bit like using a drogue chute to monitor air speed.
Put your case together and present it after doing your homework to explain away any objections from the SysAdmins, who may be working in a timewarped environment from yesterday. It doesn't stop them being good lads, but they often just have a different set of imperitives to those of the developers.

ghostdog74 12-08-2007 08:00 PM

some environment set policies that will not allow these tools, like nmap/netcat as they are powerful tools, often used for malicious purposes. So that's a valid "reason" for sysadmins/mgmt not wanting to install such tools.
@OP. see here for other alternative. Or you can start learning some socket programming.

metallica1973 12-11-2007 05:52 AM

it looks like sleep 15 seconds (though very slow) is producing reliable results or is this script truly not reliable? Any feedback

metallica1973 12-11-2007 07:59 AM

If I use perl, will I still have the same issue using that as oppossed to ksh?

PHP Code:

perl -e  'use IO::Socket; print new IO::Socket::INET (PeerAddr => "domain.com", PeerPort => "23", ) ? "OPEN\n" : "CLOSED\n";' 


ilikejam 12-11-2007 01:26 PM

Works for me :)

metallica1973 12-11-2007 03:57 PM

Can you give me an example of how I would plug

PHP Code:

perl -e  'use IO::Socket; print new IO::Socket::INET (PeerAddr => "domain.com", PeerPort => "23", ) ? "OPEN\n" : "CLOSED\n";' 

into

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 


ilikejam 12-11-2007 04:11 PM

Just replace
Code:

read foo < /dev/tcp/$SNIFFERS/23 2>/dev/null &&  Telnet=open
with
Code:

Telnet=$(perl -e  'use IO::Socket; print new IO::Socket::INET (PeerAddr => "'$SNIFFERS'", PeerPort => "23", ) ? "open\n" : "closed\n";')
Dave

chrism01 12-11-2007 04:41 PM

Add
Timeout => 5
option to the new() method to make it give up trying after 5 seconds

metallica1973 12-11-2007 05:27 PM

Its that easy?

ilikejam 12-11-2007 07:10 PM

Yes. Yes it is.

metallica1973 12-11-2007 07:48 PM

right on! I will test it out and give you an update!

chrism01 12-11-2007 09:49 PM

Of course, it's Perl ;)


All times are GMT -5. The time now is 08:15 AM.