0

I have the following bash script, which is supposed to spider a range of IP addresses.

#!/bin/bash 
a = 0    
    for i in `seq 1 255`;
        do
            a = a + 1
            echo $i
            wget -r --spider -D --header="Accept: text/html"  --user-agent="Order Of The Mouse: Stress Tester" 139.162.246.$a:80
        done

However, at the moment it doesn't include the variable a. How do I properly include a variable in a command line argument when writing a bash script?

Current output looking like this:

/root/burningWood/scripts/StressTest/tester.sh: line 5: a: command not found
254
Spider mode enabled. Check if remote file exists.
--2016-08-28 13:23:10--  http://139.162.246./
Resolving 139.162.246. (139.162.246.)... failed: Name or service not known.
wget: unable to resolve host address ‘139.162.246.’
Found no broken links.

2 Answers2

0

bash math needs special syntax - here's one way to do it

a=$((a + 1))
0

I've made a couple of changes to your code:

#!/bin/bash

for i in {1..255} # <-- use brace expansion instead of seq, no semicolon needed
do
    # a = a + 1 <-- variable $a is redundant, just use $i
    wget -r --spider -D --header="Accept: text/html"  \
        --user-agent="Order Of The Mouse: Stress Tester" "139.162.246.$i:80"
done

I moved part of the call to wget onto a new line so you could see the change more clearly.

Note that there are no spaces around an assignment, so if you wanted to use the variable a, you would assign to it like a=$(( a + 1 )).

Tom Fenech
  • 72,334
  • 12
  • 107
  • 141
  • 1
    Thanks! You're more than welcome to submit this as a pull request to https://github.com/Shakyamuni177te/StressTest too, as I would feel odd taking credit for work that's not mine. Up to you though :). –  Aug 28 '16 at 13:45
  • 1
    I would be happy to submit a pull request, although the current code in the repo doesn't use the port number! The contents of my answer are free to use with attribution so you're welcome to make the changes yourself and link back to here, or else let me know when the repo matches the code in your question. – Tom Fenech Aug 28 '16 at 14:08
  • Try https://github.com/Shakyamuni177te/Web-Spider instead :). –  Aug 28 '16 at 14:23