5

I have a list of URL and I'm trying construct a firewall log with that list. Example:

$ cat urls.csv 

a.com

I know how to construct the log with IP directly mentioned as variable.

$ cat processor.sh 

#!/bin/bash
filename="$1"
while read -r line
do
   URLS="$line"
   IP='10.109.1.1'
   today_date=`date +%d/%b/%Y`
   conact_1=" - - ["
   concat_2=":00:00:00 +0000] "
   date_concat=$conact_1$today_date$concat_2
   GET='"GET '
   protocol=' HTTP/1.1" 304 0 304 0 0 0 655 456 645 368 0'
   final_url=$IP$conact_1$today_date$concat_2$GET$URLS$protocol
   echo $final_url
done < "$filename"

Result:

$ bash processor.sh urls.csv

10.109.1.1 - - [22/Jul/2018:00:00:00 +0000] "GET a.com HTTP/1.1" 304 0 304 0 0 0 655 456 645 368 0 

Now If i have a log line of 50 or 100 or even more , How can I generate random IP for the total number of URLs and generate a log line ?

$cat urls.csv

a.com
b.com
c.com
d.com
....

Any suggestions on how to generate random IPs in bash?

Expected Result:

$ bash processor.sh urls.csv

1.1.1.1 - - [22/Jul/2018:00:00:00 +0000] "GET a.com HTTP/1.1" 304 0 304 0 0 0 655 456 645 368 0
1.1.1.2 - - [22/Jul/2018:00:00:00 +0000] "GET b.com HTTP/1.1" 304 0 304 0 0 0 655 456 645 368 0
1.1.1.3 - - [22/Jul/2018:00:00:00 +0000] "GET c.com HTTP/1.1" 304 0 304 0 0 0 655 456 645 368 0
1.1.1.4 - - [22/Jul/2018:00:00:00 +0000] "GET d.com HTTP/1.1" 304 0 304 0 0 0 655 456 645 368 0
............
1
  • 1
    date_concat=$(date +" - - [%d/%b/%Y:00:00:00 +0000] "); the format string isn't restricted to the special escape characters. Commented Jul 22, 2018 at 18:12

2 Answers 2

12

For IPv4, the basics would be:

printf "%d.%d.%d.%d\n" "$((RANDOM % 256))" "$((RANDOM % 256))" "$((RANDOM % 256))" "$((RANDOM % 256))"

You'll probably want to filter out 192.168.0.0/16 and 10.0.0.0/8 and a lot of other addresses. For IPv6, you might try:

for ((i=0;i<8;i++)); do printf "%02x%02x:" $((RANDOM%256)) $((RANDOM%256)); done | sed 's/:$//'  

($RANDOM only generates values up to 2^15-1, so $((RANDOM%65536)) is pointless)

Sign up to request clarification or add additional context in comments.

1 Comment

perhaps one need to do printf "%d.%d.%d.%d\n" "$((RANDOM % 256 ))" "$((RANDOM % 256 ))" "$((RANDOM % 256 ))" "$((RANDOM % 256 ))"
2

Works as expected based on Inder's answer.

$cat processor.sh

#!/bin/bash
filename="$1"
while read -r line
do
   URLS="$line"
   #IP=$(printf "%d.%d.%d.%d\n" "$((RANDOM % 256 ))")
   IP=$(printf "%d.%d.%d.%d\n" "$((RANDOM % 256 ))" "$((RANDOM % 256 ))" "$((RANDOM % 256 ))" "$((RANDOM % 256 ))")
   #IP='10.109.1.1'
   today_date=`date +%d/%b/%Y`
   conact_1=" - - ["
   concat_2=":00:00:00 +0000] "
   date_concat=$conact_1$today_date$concat_2
   GET='"GET '
   protocol=' HTTP/1.1" 304 0 304 0 0 0 655 456 645 368 0'
   final_url=$IP$conact_1$today_date$concat_2$GET$URLS$protocol
   echo $final_url
done < "$filename"

$ bash processor.sh input.csv
103.137.94.111 - - [22/Jul/2018:00:00:00 +0000] "GET a.com HTTP/1.1" 304 0 304 0 0 0 655 456 645 368 0
74.123.232.234 - - [22/Jul/2018:00:00:00 +0000] "GET b.com HTTP/1.1" 304 0 304 0 0 0 655 456 645 368 0
102.13.8.154 - - [22/Jul/2018:00:00:00 +0000] "GET c.com HTTP/1.1" 304 0 304 0 0 0 655 456 645 368 0
210.106.160.230 - - [22/Jul/2018:00:00:00 +0000] "GET d.com HTTP/1.1" 304 0 304 0 0 0 655 456 645 368 0

1 Comment

I just did a minor correction its still williams answer and credits :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.