Unix Programming - I need a script

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > February 2004 > I need a script





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author I need a script
Joshua Sparks

2004-02-27, 11:33 pm

I use a bash shell (cygwin) out to my PC so I can do Unix stuff. I have a
file of IP addresses that I want to feed into tracert. So I build a script
that takes in $1 and $2 where $1 is the input file and $2 is the results.
Basically, I need to take $1, do a sort -u (for uniqueness) and feed that
into the tracert program which comes with Windows XP so I can do a batch
trace. The command I've come up with is:

sort -u $1 > sorted_$1
tracert -j `cat sorted_$1` > $2

but this doesn't work. It says either an ambiguous list or that the option
is too large. The only alternative is to do something in PERL or sed or awk
to add the tracert to the beginning of each line of the sorted file and spit
the results out there. Any ideas?



Barry Margolin

2004-02-28, 1:33 am

In article <FDU%b.106273$IF6.3220710@ursa-nb00s0.nbnet.nb.ca>,
"Joshua Sparks" <nacs@nb.sympatico.ca> wrote:

> I use a bash shell (cygwin) out to my PC so I can do Unix stuff. I have a
> file of IP addresses that I want to feed into tracert. So I build a script
> that takes in $1 and $2 where $1 is the input file and $2 is the results.
> Basically, I need to take $1, do a sort -u (for uniqueness) and feed that
> into the tracert program which comes with Windows XP so I can do a batch
> trace. The command I've come up with is:
>
> sort -u $1 > sorted_$1
> tracert -j `cat sorted_$1` > $2
>
> but this doesn't work. It says either an ambiguous list or that the option
> is too large. The only alternative is to do something in PERL or sed or awk
> to add the tracert to the beginning of each line of the sorted file and spit
> the results out there. Any ideas?


That command will run tracert one time with *all* the IP addresses as
parameters, it won't run it multiple times. You need to write a loop,
e.g.

for ip in `sort -u $1`
do
tracert -j $ip
done > $2

--
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
John W. Krahn

2004-02-28, 1:33 am

Joshua Sparks wrote:
>
> I use a bash shell (cygwin) out to my PC so I can do Unix stuff. I have a
> file of IP addresses that I want to feed into tracert. So I build a script
> that takes in $1 and $2 where $1 is the input file and $2 is the results.
> Basically, I need to take $1, do a sort -u (for uniqueness) and feed that
> into the tracert program which comes with Windows XP so I can do a batch
> trace. The command I've come up with is:
>
> sort -u $1 > sorted_$1
> tracert -j `cat sorted_$1` > $2
>
> but this doesn't work. It says either an ambiguous list or that the option
> is too large. The only alternative is to do something in PERL or sed or awk
> to add the tracert to the beginning of each line of the sorted file and spit
> the results out there. Any ideas?


Untested!:

#!/usr/bin/perl

@ARGV == 2 or die "Usage: $0 input_file results_file\n";
$results_file = pop; # remove results file from @ARGV

open OUT, '>', $results_file or die "Cannot open $results_file: $!";

@addresses = grep !$found{ $_ }++, grep [ chomp ], <>;

print OUT `tracert -j $_` foreach sort @addresses;

__END__



John
--
use Perl;
program
fulfillment
William Park

2004-02-28, 2:33 pm

Joshua Sparks <nacs@nb.sympatico.ca> wrote:
> I use a bash shell (cygwin) out to my PC so I can do Unix stuff. I have a
> file of IP addresses that I want to feed into tracert. So I build a script
> that takes in $1 and $2 where $1 is the input file and $2 is the results.
> Basically, I need to take $1, do a sort -u (for uniqueness) and feed that
> into the tracert program which comes with Windows XP so I can do a batch
> trace. The command I've come up with is:
>
> sort -u $1 > sorted_$1
> tracert -j `cat sorted_$1` > $2
>
> but this doesn't work. It says either an ambiguous list or that the option
> is too large.


1. sort -u $1 | while read IP; do
tracert -j $IP
done > $2

2. sort -u $1 | xargs -l1 tracert -j > $2

--
William Park, Open Geometry Consulting, <opengeometry@yahoo.ca>
Linux solution for data management and processing.
Joshua Sparks

2004-02-29, 3:34 pm

I'm actually going to use the Uwin ksh system to use a korn shell interface.
bash is too "unfamiliar". Anyway, here's what I came up with

#!/usr/bin/ksh
grep "^Remote" $1 | grep "local service is" | grep -v "ftp" |
/usr/bin/cut -c34-55 > temp1
/usr/bin/awk -F, '{ print $1 }' temp1 > temp2
/usr/bin/sort -u temp2 > temp3
for ip in `/usr/bin/cat temp3`
do
tracert $ip
done > $2
rm -f temp*

This program takes the "Firewall" tab of Norton Internet Security, which you
have saved toa text file as input $1 and processes it to
find all the unique IP addresses of suspicious activities and runs a trace
route on them. If anyone else wants to use it, feel free.

"Barry Margolin" <barmar@alum.mit.edu> wrote in message
news:barmar-FCDD1C.00584628022004@comcast.ash.giganews.com...
> In article <FDU%b.106273$IF6.3220710@ursa-nb00s0.nbnet.nb.ca>,
> "Joshua Sparks" <nacs@nb.sympatico.ca> wrote:
>
a[color=darkred]
script[color=darkred]
results.[color=darkred]
that[color=darkred]
option[color=darkred]
awk[color=darkred]
spit[color=darkred]
>
> That command will run tracert one time with *all* the IP addresses as
> parameters, it won't run it multiple times. You need to write a loop,
> e.g.
>
> for ip in `sort -u $1`
> do
> tracert -j $ip
> done > $2
>
> --
> Barry Margolin, barmar@alum.mit.edu
> Arlington, MA
> *** PLEASE post questions in newsgroups, not directly to me ***



Heiner Steven

2004-02-29, 5:33 pm

Joshua Sparks wrote:

> I'm actually going to use the Uwin ksh system to use a korn shell interface.
> bash is too "unfamiliar". Anyway, here's what I came up with
>
> #!/usr/bin/ksh
> grep "^Remote" $1 | grep "local service is" | grep -v "ftp" |
> /usr/bin/cut -c34-55 > temp1
> /usr/bin/awk -F, '{ print $1 }' temp1 > temp2
> /usr/bin/sort -u temp2 > temp3
> for ip in `/usr/bin/cat temp3`
> do
> tracert $ip
> done > $2
> rm -f temp*


Without knowing the format of the input file "$1"
the script can still be changed to not need any temporary
file:

#! /usr/bin/ksh
grep '^Remote.*local service is" "$1" |
grep -v ftp |
cut -c34-55 |
cut -d, -f1 |
sort -u |
while read ip
do
tracert "$ip"
done

The "grep -v" and the "cut" commands maybe could be optimized,
too, but for this we'd need to know how an example input
file looks like.

Heiner
--
___ _
/ __| |_ _____ _____ _ _ Heiner STEVEN <heiner.steven@nexgo.de>
\__ \ _/ -_) V / -_) ' \ Shell Script Programmers: visit
|___/\__\___|\_/\___|_||_| http://www.shelldorado.com/
Barry Margolin

2004-02-29, 9:33 pm

In article <4Cr0c.107167$IF6.3278378@ursa-nb00s0.nbnet.nb.ca>,
"Joshua Sparks" <nacs@nb.sympatico.ca> wrote:

> I'm actually going to use the Uwin ksh system to use a korn shell interface.
> bash is too "unfamiliar". Anyway, here's what I came up with


Nothing in my script was bash-dependent.

>
> #!/usr/bin/ksh
> grep "^Remote" $1 | grep "local service is" | grep -v "ftp" |
> /usr/bin/cut -c34-55 > temp1
> /usr/bin/awk -F, '{ print $1 }' temp1 > temp2
> /usr/bin/sort -u temp2 > temp3
> for ip in `/usr/bin/cat temp3`
> do
> tracert $ip
> done > $2
> rm -f temp*


Why do you need all those temp files? Just pipe everything together.

--
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com