From: Ben Finney on
Ciccio <lserena(a)gmail.com> writes:

> #!/bin/bash
>
> /usr/bin/netstat -an | grep TIME_WAIT > time.wait.file
>
> while read line
> do
> LOCAL=`echo $line | awk '{print $1}'`
> LOCALIP=`echo $LOCAL | cut -d "." -f 1-4`
> LOCALPORT=`echo $LOCAL | cut -d "." -f 5`
> REMOTE=`echo $line | awk '{print $2}'`
> REMOTEIP=`echo $REMOTE | cut -d "." -f 1-4`
> REMOTEPORT=`echo $REMOTE | cut -d "." -f 5`
> /usr/local/bin/tcpdrop $LOCALIP $LOCALPORT $REMOTEIP $REMOTEPORT
> done < time.wait.file

Dave <foo(a)coo.com> writes:

> I would have thought
> #!/bin/sh
>
> would have been an improvement, and write it in a portable way,
> without the GNUisms.


On the other hand, if you *do* want to have the program depend on Bash,
then take advantage of that by using the nestable shell interpolation
form:

LOCAL=$(echo $line | awk '{print $1}')
LOCALIP=$(echo $LOCAL | cut -d "." -f 1-4)
LOCALPORT=$(echo $LOCAL | cut -d "." -f 5)
REMOTE=$(echo $line | awk '{print $2}')
REMOTEIP=$(echo $REMOTE | cut -d "." -f 1-4)
REMOTEPORT=$(echo $REMOTE | cut -d "." -f 5)

--
\ “We are stuck with technology when what we really want is just |
`\ stuff that works.” —Douglas Adams |
_o__) |
Ben Finney
From: Arcege on
On Nov 3, 12:33 pm, Ciccio <lser...(a)gmail.com> wrote:
> Hi List,
>
> Given the output below,
>
> $ netstat -an | grep TIME_WAIT
> 10.159.244.250.80    10.159.244.250.49198 49152      0 49152      0
> TIME_WAIT
> 10.159.244.250.49199 10.159.244.250.1984  51148      0 49152      0
> TIME_WAIT
> 10.159.244.250.49200 10.159.244.250.1984  49944      0 49152      0
> TIME_WAIT
> 10.159.244.250.49167 10.159.244.250.1984  50485      0 49152      0
> TIME_WAIT
> 10.159.244.250.49168 10.159.244.250.1984  50363      0 49152      0
> TIME_WAIT
> 10.159.244.250.49169 10.159.244.250.1984  49247      0 49152      0
> TIME_WAIT
> 10.159.244.250.49170 10.159.244.250.1984  49247      0 49152      0
> TIME_WAIT
> 10.159.244.250.49171 10.159.244.250.1984  49249      0 49152      0
> TIME_WAIT
> 10.159.244.250.49172 10.159.244.250.1984  49247      0 49152      0
> TIME_WAIT
> 10.159.244.250.49173 10.159.244.250.1984  49245      0 49152      0
> TIME_WAIT
> 10.159.244.250.49174 10.159.244.250.1984  49251      0 49152      0
> TIME_WAIT
> 10.159.244.250.49175 10.159.244.250.1984  49663      0 49152      0
> TIME_WAIT
> 10.159.244.250.49176 10.159.244.250.1984  51201      0 49152      0
> TIME_WAIT
> 10.159.244.250.49177 10.159.244.250.1984  49495      0 49152      0
> TIME_WAIT
> 10.159.244.250.49181 10.159.244.250.22    49152      0 49152      0
> TIME_WAIT
> 10.159.244.250.49185 10.159.244.50.22     49640      0 49640      0
> TIME_WAIT
> 10.159.244.250.49186 10.159.244.46.22     49640      0 49640      0
> TIME_WAIT
> 10.159.244.250.49187 10.159.244.135.22    49640      0 49640      0
> TIME_WAIT
> 10.159.244.250.49188 10.159.244.250.1984  52081      0 49152      0
> TIME_WAIT
> 10.159.244.250.49194 10.159.244.250.1984  49541      0 49152      0
> TIME_WAIT
> 10.159.244.250.49195 10.159.244.250.1984  49553      0 49152      0
> TIME_WAIT
>
> I need to obtain these 4 variables ($LOCALIP $LOCALPORT $REMOTEIP
> $REMOTEPORT) to pass to tcpdrop.
>
> Here is what I came up with - and it works, but is there a better/
> faster/neater way of doing it?
>
> Cheers
>
> Ciccio
>
> #!/bin/bash
>
> /usr/bin/netstat -an | grep TIME_WAIT > time.wait.file
>
> while read line
> do
>    LOCAL=`echo $line | awk '{print $1}'`
>    LOCALIP=`echo $LOCAL | cut -d "." -f 1-4`
>    LOCALPORT=`echo $LOCAL | cut -d "." -f 5`
>    REMOTE=`echo $line | awk '{print $2}'`
>    REMOTEIP=`echo $REMOTE | cut -d "." -f 1-4`
>    REMOTEPORT=`echo $REMOTE | cut -d "." -f 5`
>    /usr/local/bin/tcpdrop $LOCALIP $LOCALPORT $REMOTEIP $REMOTEPORT
> done < time.wait.file

netstat -atn | awk '/TIME_WAIT/{split($4,L,/:/);split($5,R,/:/);print
"tcpdrop",L[1],L[2],R[1],R[2]}' | sh -s
From: Kaz Kylheku on
On 2009-11-04, Ben Finney <bignose+hates-spam(a)benfinney.id.au> wrote:
> On the other hand, if you *do* want to have the program depend on Bash,
> then take advantage of that by using the nestable shell interpolation
> form:
>
> LOCAL=$(echo $line | awk '{print $1}')
> LOCALIP=$(echo $LOCAL | cut -d "." -f 1-4)
> LOCALPORT=$(echo $LOCAL | cut -d "." -f 5)
> REMOTE=$(echo $line | awk '{print $2}')
> REMOTEIP=$(echo $REMOTE | cut -d "." -f 1-4)
> REMOTEPORT=$(echo $REMOTE | cut -d "." -f 5)

I don't see any obvious bash dependency. The nestable command substitution
syntax $(...) is POSIX.
From: Ben Finney on
Kaz Kylheku <kkylheku(a)gmail.com> writes:

> I don't see any obvious bash dependency. The nestable command
> substitution syntax $(...) is POSIX.

Yes, but it's not Bourne shell, which often seems to be what “no
Bashisms” complainers want us to target if we choose '#! /bin/sh' for
the shebang line.

Apparently, there are some people using OSes which provide a '/bin/sh'
that is POSIX-violating in basic ways. I haven't encountered such a
shell for over ten years, and I can't understand those who put up with
it; but it seems they may still exist.

--
\ “We tend to scoff at the beliefs of the ancients. But we can't |
`\ scoff at them personally, to their faces, and this is what |
_o__) annoys me.” —Jack Handey |
Ben Finney
From: bb on
On 2009-11-03 18:06, Ciccio wrote:
> On Nov 3, 4:56 pm, pk <p...(a)pk.invalid> wrote:
>> Ciccio wrote:
>>> Hi List,
>>> Given the output below,
>>> $ netstat -an | grep TIME_WAIT
>>> 10.159.244.250.80 10.159.244.250.49198 49152 0 49152 0
>>> TIME_WAIT
>> Try
>>
>> eval $(netstat -an | awk -F '\.| +' -v OFS=. '/TIME_WAIT/{
>> print "LOCALIP="$1,$2,$3,$4
>> print "LOCALPORT="$5
>> print "REMOTEIP="$6,$7,$8,$9
>> print "REMOTEPORT="$10
>>
>> }')
>
>
>
> Hi pk,
>
> Thanks for that - I gave that a shot and it came back with the
> following:
>
> $ eval $(netstat -an | awk -F '\.| +' -v OFS=. '/TIME_WAIT/{
>> print "LOCALIP="$1,$2,$3,$4
>> print "LOCALPORT="$5
>> print "REMOTEIP="$6,$7,$8,$9
>> print "REMOTEPORT="$10
>> }')
> awk: syntax error near line 1
> awk: bailing out near line 1
> $
>
> (should it matter, I'm on a Solaris 10 x64 server).
>
> Ciccio
>

On Solaris you should use nawk instead of awk.

/bb
First  |  Prev  |  Next  |  Last
Pages: 1 2 3 4
Prev: [tcsh] Screen's ^] inserting junk
Next: Parsing GC Log File