DEV Community

Samiul Islam
Samiul Islam

Posted on

Redirect filtered output

Good Day Everyone!

I have a list of ASN numbers in a text file which I want to use as INPUT for the following script shared in github
https://github.com/nitefood/asn/

Image description

I want to redirect the output of every ASN number to a separate file (appending is fine) so my sample script looks as following-

#!/bin/bash
while IFS= read -r ASN; do
asn $ASN >> ASN_info
sleep 10
done < ASN_LIST

So, my while loop just keeps iterating without waiting to finish the redirection to the file and shows the output like below-

Image description

Please help to modify my silly script to redirect entire output for every ASN number from the input file.Will really appreciate the community's help :)

Top comments (1)

Collapse
 
nitefood profile image
Adriano

Hey! asn author here. You can use JSON mode to easily accomplish that. If I get it right you're looking to associate AS numbers to their IP ranges. An example using only IPv4 ranges could be done like this:

$ cat ASN_LIST
1111
2222

$ for ASN in $(cat ASN_LIST); do asn -j $ASN | jq -r '.results[] | "- AS" + .asn + " (" + .asname + ")", .announced_prefixes.v4[], .inetnums_announced_by_other_as.v4[].prefix, "\n"' >> ASN_INFO; done

$ cat ASN_INFO
- AS1111 (University of Klagenfurt, AT)
143.205.0.0/16


- AS2222 (FR-RENATER FR, EU)
193.52.0.0/16
193.54.0.0/15
194.167.0.0/16
194.199.0.0/16
194.214.0.0/16
194.254.0.0/16
194.57.0.0/16
195.220.0.0/15
195.83.0.0/16
195.98.224.0/19
81.194.0.0/16
Enter fullscreen mode Exit fullscreen mode

Of course you can also include IPv6 ranges by including the relevant fields from the JSON output.

HTH