Question

I am trying to check if a domain is active on the server. So far I get errors.

list=/root/domainlist.txt
for i in $(cat $list)
do
  echo "checking " $i
  $ip = host $i |grep -o -m 100 '[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}'
  if [[ $ip == "xx.xx.xx.xx" ]]; then
    $i >> /root/activedomains.txt
  fi
done

Output:

activedomains: line 4: =: command not found

This is the current error I get.

Was it helpful?

Solution

  • No spaces before and after the =
  • No dollar sign in the assignment
  • You probably want the result of the command, so enclose it in $( )

    ip=$(host $i |grep -o -m 100 '[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}')
    

write to the file like this

echo "$i" >> /root/activedomains.txt

OTHER TIPS

You have a syntax error with the line

$ip = host $i |grep -o -m 100 '...'

you shoud use instead :

ip=$(host $i |grep -o -m 100 '...')

A better way using boolean logic (no need grep there, if host $ip failed, it will return FALSE):

list=/root/domainlist.txt

while read ip; do
    echo "checking $ip"
    host "$ip" &>/dev/null && echo "$ip" >> /root/activedomains.txt
done < "$list"

It's the equivalent of

list=/root/domainlist.txt

while read ip; do
    echo "checking $ip"
    if host "$ip" &>/dev/null; then
        echo "$ip" >> /root/activedomains.txt
    fi
done < "$list"

For starters you shouldn't assign to $ip to ip ... but it's possible there are more errors.

My guess would be you wanted (line 4/5):

ip=$(host $i |grep -o -m 100 '[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}')

Also read user000001's answer. The missing echo when getting the output is another issue.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top