Search code examples
bashshellipip-addresscommand-substitution

Using bash to generate random IPs in Curl


How to use random ip address in Curl request,I'm using this code and worked

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

but when use this code in Curl request and test on http://ifconfig.me not worked

curl --header 'X-Forwarded-For: printf "%d.%d.%d.%d\n" "$((RANDOM % 256))" "$((RANDOM % 256))" "$((RANDOM % 256))" "$((RANDOM % 256))"' http://ifconfig.me

Solution

  • Is suggest to use command substitution. Replace

    curl --header 'X-Forwarded-For: printf "%d.%d.%d.%d\n" "$((RANDOM % 256))" "$((RANDOM % 256))" "$((RANDOM % 256))" "$((RANDOM % 256))"' http://ifconfig.me
    

    with

    curl --header "X-Forwarded-For: $(printf "%d.%d.%d.%d\n" "$((RANDOM % 256))" "$((RANDOM % 256))" "$((RANDOM % 256))" "$((RANDOM % 256))")" http://ifconfig.me
                  ^                 ^^                                                                                                      ^^
    

    I switched from '...' to "..." and from printf "..." to $(printf "...").


    See: Difference between single and double quotes in bash