Find the answer to your Linux question:
Results 1 to 5 of 5
Hi, im trying to write a script to check the port 22 of 4 IPs. IPS={89.17.206.180,89.17.206.185,89.17.206.186,89. 17.206.187} for i in ${IPS} do nmap -p 22 {IPS} done but i get ...
  1. #1
    Just Joined!
    Join Date
    Apr 2007
    Posts
    22

    Problema con "for" in a bash script

    Hi,

    im trying to write a script to check the port 22 of 4 IPs.

    IPS={89.17.206.180,89.17.206.185,89.17.206.186,89. 17.206.187}

    for i in ${IPS}
    do
    nmap -p 22 {IPS}
    done


    but i get this error:

    Failed to resolve given hostname/IP: {IPS}. Note that you can't use '/mask' AND '1-4,7,100-' style IP ranges
    WARNING: No targets were specified, so 0 hosts scanned.
    Nmap done: 0 IP addresses (0 hosts up) scanned in 0.14 seconds

    Any idea?

  2. #2
    Linux Engineer Kieren's Avatar
    Join Date
    Aug 2007
    Location
    England
    Posts
    845
    You are using the call to the variable containing each IP address incorrectly within the for loop. The line:

    Code:
    for i in ${IPS}
    Will take each item in $IPS and give it the variable name of i

    Code:
    IPS={89.17.206.180,89.17.206.185,89.17.206.186,89. 17.206.187}
    
    for i in ${IPS}
    do
        nmap -p 22 ${i}
    done
    Linux User #453176

  3. #3
    Just Joined!
    Join Date
    Apr 2007
    Posts
    22
    Thanks both!, anyway i have extended my code and now im trying to print "Closed" or "Open" depending on the state of the port.

    My problem: it just prints "Open" one time, i mean: in this code below i have two IPs, but it seams the code is not considering none of them.


    Code:
    #IPS="89.17.206.129,89.17.206.132"
    
    for i in ${IPS}
    do
    if [ -z 'nmap -p 22 $i |grep open' ]; then
     echo "Closed"
    else
     echo "Open"
    fi
    done
    Any idea?

  4. #4
    Linux Engineer Kieren's Avatar
    Join Date
    Aug 2007
    Location
    England
    Posts
    845
    You have commented out your first line
    Linux User #453176

  5. #5
    Just Joined!
    Join Date
    Apr 2007
    Posts
    22
    Hi again,

    i have the answer:

    Code:
    IPS="$(seq -f "89.17.206.%.0f" 129 189) $(seq -f "89.17.219.%.0f" 65 126)"

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...