I have a list of ip socks / port(eg: 192.168.0.1 80). I would like to write a bash to test automatically these addresses in a loop with firefox. The problem is that firefox process stays alive even when firefox does not work because of wrong network settings. So I want to kill the process when the browser does not work. I was thinking of the condition:
timeout 10s watch -n 1 'netstat -a | grep "ESTA http"'
If such command return true, break and exit, otherwise kill firefox and continue.
I dont know how to traduce an output command in a boolean condition. If (output commnand) or if !(output command)
Here is what my script looks like:
#1. read the list
sort -R list | while read -r IP PORT
do
#2.clean firefox user preference
grep -v "network.proxy.\(socks\|socks_port\|type\)" prefs.js > temp && mv temp prefs.js
#3. set user preference with the current ip/port in the list
cat <<EOT >> prefs.js
user_pref("network.proxy.socks", "$IP");
user_pref("network.proxy.socks_port", $PORT);
user_pref("network.proxy.type", 1);
EOT
#4.launch firefox
firefox;
#5 is it working?
timeout 10s watch -n 1 'netstat -a | grep "ESTA http"'&;
#6 from there I don't how to traduce in bash the following:
if (output of #5)
break;
if !(output of #5) kill $(ps aux | grep 'firefox' | awk '{print $2}')
done;
exit;
thanx guys !
---------- Post updated at 11:24 AM ---------- Previous update was at 08:22 AM ----------
actually, there should be IFS=' '; before the while loop.
I have improved a little bit; the only thing is that it does not break where I want it to break:
#!/bin/bash
IFS=' ';
sort -R sokslist | while read -r IP PORT
do
grep -v "network.proxy.\(socks\|socks_port\|type\)" prefs.js > temp && mv temp prefs.js
cat <<EOT >> prefs.js
user_pref("network.proxy.socks", "$IP");
user_pref("network.proxy.socks_port", $PORT);
user_pref("network.proxy.type", 1);
EOT
firefox;
timeout 10s watch -n 1 'netstat -a | grep "http ESTA"' | break;
#it should break there, but it does not
kill $(ps aux | grep 'firefox' | awk '{print $2}');
done;
exit