bashpingsilenttorsocks

Silently checking onion domain is available in bash?


Context

After having written a working check to see an onion address is reachable:

onion_address_is_available() {
  local onion_address="$1"

  local ping_output
  ping_output=$(torsocks httping --count 1 "$onion_address")
  if [[ "$ping_output" == *"100,00% failed"* ]]; then
    echo "NOTFOUND"
  elif [[ "$ping_output" == *"1 connects, 1 ok, 0,00% failed, time"* ]]; then
    echo "FOUND"
  else
    echo "Error, did not find status."
    exit 5
  fi
}

I would like to make it quiet.

Output

When I run for example: torsocks httping --count 1 non_existing.onion, there are 2 things that show up in the CLI when I run the function:

1681750009 ERROR torsocks[134694]: General SOCKS server failure (in socks5_recv_connect_reply() at socks5.c:527) problem connecting to host: Connection refused

and:

Auto enabling SSL due to https-URL

From what I understand, I can silence commands, both the stdout and stderr with: >>/dev/null 2>&1. However, I would not like to completely silence the command, as I would still like to parse the *"100,00% failed"* and *"1 connects, 1 ok, 0,00% failed, time"* bits of the output. Hence, I would like to ask:

Question

How can one silently check in shell-check compliant bash, whether an onion address is available or not?


Solution

  • The answer was given by @Barmar in the comments. A working, silent solution was found with: ping_output=$(torsocks httping --count 1 "$onion_address" 2>/dev/null).

    The full function is:

    onion_address_is_available() {
      local onion_address="$1"
    
      local ping_output
      ping_output=$(torsocks httping --count 1 "$onion_address" 2>/dev/null)
      if [[ "$ping_output" == *"100,00% failed"* ]]; then
        echo "NOTFOUND"
      elif [[ "$ping_output" == *"1 connects, 1 ok, 0,00% failed, time"* ]]; then
        echo "FOUND"
      else
        echo "Error, did not find status."
        exit 5
      fi
    }
    

    and it does not produce any output.