RESULT="1"
PING=$(ping www.msftncsi.com -c 1 | grep -E -o '[0-9]+ received' | cut -f1 -d' ')
if [ "$RESULT" != "$PING" ]
then
DO SOMETHING
else
DO SOMETHING
fi
but is it not possible to use ping in a bash script
But definitely not to that host as many people block it...No idea what you are tryinig to do, but is it not possible to use ping in a bash script? Why not do the same ping wanduck is doing to that microsoft ncsi address?
Code:RESULT="1" PING=$(ping www.msftncsi.com -c 1 | grep -E -o '[0-9]+ received' | cut -f1 -d' ') if [ "$RESULT" != "$PING" ] then DO SOMETHING else DO SOMETHING fi
So scrapped the nvram solution so that other distroes can use this script so far the script is only in my git as always its a test version for anyone brave enough to try
Ah, thanks. Didn't see your post.I believe he has already taken that approach in his git repo
#!/bin/sh
# Author: Toast
# Contributers: Octopus, Tomsk, Neurophile, jimf, spalife, visortgw, Cedarhillguy, redhat27
# Testers: shooter40sw
# Supporters: lesandie
# Revision 19
blocklist=/jffs/malware-filter.list # Set your path here
retries=3 # Set number of tries here
regexp=`echo "\b([0-9]{1,3}\.){3}[0-9]{1,3}\b"` # Dont change this value
case $(ipset -v | grep -o "v[4,6]") in
v6)
MATCH_SET='--match-set'; CREATE='create'; ADD='add'; SWAP='swap'; IPHASH='hash:ip'; NETHASH='hash:net family inet'; FLUSH='flush'; DESTROY='destroy';
lsmod | grep -q "xt_set" || \
for module in ip_set ip_set_nethash ip_set_iphash xt_set; do
insmod $module
done;;
v4)
MATCH_SET='--set'; CREATE='--create'; ADD='--add'; SWAP='--swap'; IPHASH='iphash'; NETHASH='nethash'; FLUSH='--flush'; DESTROY='--destroy'
lsmod | grep -q "ipt_set" || \
for module in ip_set ip_set_nethash ip_set_iphash ipt_set; do
insmod $module
done;;
*) echo "unsupported version"; exit 1 ;;
esac
check_online () {
ping -q -c 1 google.com >/dev/null 2>&1 && get_list || exit 1
}
get_list () {
url=https://gitlab.com/swe_toast/malware-filter/raw/master/malware-filter.list
if [ ! -f $blocklist ]
then wget $url -O $blocklist; get_source; else get_source; fi
}
get_source () {
wget -q --tries=$retries --show-progress -i $blocklist -O /tmp/malware-filter-raw.part
awk '!/(^127\.)|(^10\.)|(^172\.1[6-9]\.)|(^172\.2[0-9]\.)|(^172\.3[0-1]\.)|(^192\.168\.)/' /tmp/malware-filter-raw.part > /tmp/malware-filter-presort.part
cat /tmp/malware-filter-presort.part | grep -oE "$regexp" | sort -u > /tmp/malware-filter-sorted.part
}
run_ipset () {
echo "adding ipset rule to firewall this will take time."
ipset -L malware-filter >/dev/null 2>&1
if [ $? -ne 0 ]; then
if [ "$(ipset --swap malware-filter malware-filter 2>&1 | grep -E 'Unknown set|The set with the given name does not exist')" != "" ]; then
nice -n 2 ipset $CREATE malware-filter $IPHASH
if [ -f /opt/bin/xargs ]; then
/opt/bin/xargs -P10 -I "PARAM" -n1 -a /tmp/malware-filter-sorted.part nice -n 2 ipset $ADD malware-filter PARAM
else cat /tmp/malware-filter-sorted.part | xargs -I {} ipset $ADD malware-filter {}; fi
fi
else
nice -n 2 ipset $CREATE malware-update $IPHASH
if [ -f /opt/bin/xargs ]; then
/opt/bin/xargs -P10 -I "PARAM" -n1 -a /tmp/malware-filter-sorted.part nice -n 2 ipset $ADD malware-update PARAM
else cat /tmp/malware-filter-sorted.part | xargs -I {} ipset $ADD malware-update {}; fi
nice -n 2 ipset $SWAP malware-update malware-filter
nice -n 2 ipset $DESTROY malware-update
fi
iptables -L | grep malware-filter > /dev/null 2>&1
if [ $? -ne 0 ]; then
nice -n 2 iptables -I FORWARD -m set $MATCH_SET malware-filter src,dst -j REJECT
else
nice -n 2 iptables -D FORWARD -m set $MATCH_SET malware-filter src,dst -j REJECT
nice -n 2 iptables -I FORWARD -m set $MATCH_SET malware-filter src,dst -j REJECT
fi }
cleanup () {
logger -s -t system "Malware Filter loaded $(ipset -L malware-filter | wc -l | awk '{print $1-7}') unique ip addresses."
find /tmp -name 'malware-filter-*.part' -exec rm {} +
}
check_online
run_ipset
cleanup
exit $?
pset v4.5: 181.214.63.133 is already in set malware-update.
ipset v4.5: 181.215.113.46 is already in set malware-update.
ipset v4.5: 181.215.244.14 is already in set malware-update.
system: Malware Filter loaded 37930 unique ip addresses.
Here is the update as promised
Code:#!/bin/sh # Author: Toast # Contributers: Octopus, Tomsk, Neurophile, jimf, spalife, visortgw, Cedarhillguy, redhat27 # Testers: shooter40sw # Supporters: lesandie # Revision 19 blocklist=/jffs/malware-filter.list # Set your path here retries=3 # Set number of tries here regexp=`echo "\b([0-9]{1,3}\.){3}[0-9]{1,3}\b"` # Dont change this value case $(ipset -v | grep -o "v[4,6]") in v6) MATCH_SET='--match-set'; CREATE='create'; ADD='add'; SWAP='swap'; IPHASH='hash:ip'; NETHASH='hash:net family inet'; FLUSH='flush'; DESTROY='destroy'; lsmod | grep -q "xt_set" || \ for module in ip_set ip_set_nethash ip_set_iphash xt_set; do insmod $module done;; v4) MATCH_SET='--set'; CREATE='--create'; ADD='--add'; SWAP='--swap'; IPHASH='iphash'; NETHASH='nethash'; FLUSH='--flush'; DESTROY='--destroy' lsmod | grep -q "ipt_set" || \ for module in ip_set ip_set_nethash ip_set_iphash ipt_set; do insmod $module done;; *) echo "unsupported version"; exit 1 ;; esac check_online () { ping -q -c 1 google.com >/dev/null 2>&1 && get_list || exit 1 } get_list () { url=https://gitlab.com/swe_toast/malware-filter/raw/master/malware-filter.list if [ ! -f $blocklist ] then wget $url -O $blocklist; get_source; else get_source; fi } get_source () { wget -q --tries=$retries --show-progress -i $blocklist -O /tmp/malware-filter-raw.part awk '!/(^127\.)|(^10\.)|(^172\.1[6-9]\.)|(^172\.2[0-9]\.)|(^172\.3[0-1]\.)|(^192\.168\.)/' /tmp/malware-filter-raw.part > /tmp/malware-filter-presort.part cat /tmp/malware-filter-presort.part | grep -oE "$regexp" | sort -u > /tmp/malware-filter-sorted.part } run_ipset () { echo "adding ipset rule to firewall this will take time." ipset -L malware-filter >/dev/null 2>&1 if [ $? -ne 0 ]; then if [ "$(ipset --swap malware-filter malware-filter 2>&1 | grep -E 'Unknown set|The set with the given name does not exist')" != "" ]; then nice -n 2 ipset $CREATE malware-filter $IPHASH if [ -f /opt/bin/xargs ]; then /opt/bin/xargs -P10 -I "PARAM" -n1 -a /tmp/malware-filter-sorted.part nice -n 2 ipset $ADD malware-filter PARAM else cat /tmp/malware-filter-sorted.part | xargs -I {} ipset $ADD malware-filter {}; fi fi else nice -n 2 ipset $CREATE malware-update $IPHASH if [ -f /opt/bin/xargs ]; then /opt/bin/xargs -P10 -I "PARAM" -n1 -a /tmp/malware-filter-sorted.part nice -n 2 ipset $ADD malware-update PARAM else cat /tmp/malware-filter-sorted.part | xargs -I {} ipset $ADD malware-update {}; fi nice -n 2 ipset $SWAP malware-update malware-filter nice -n 2 ipset $DESTROY malware-update fi iptables -L | grep malware-filter > /dev/null 2>&1 if [ $? -ne 0 ]; then nice -n 2 iptables -I FORWARD -m set $MATCH_SET malware-filter src,dst -j REJECT else nice -n 2 iptables -D FORWARD -m set $MATCH_SET malware-filter src,dst -j REJECT nice -n 2 iptables -I FORWARD -m set $MATCH_SET malware-filter src,dst -j REJECT fi } cleanup () { logger -s -t system "Malware Filter loaded $(ipset -L malware-filter | wc -l | awk '{print $1-7}') unique ip addresses." find /tmp -name 'malware-filter-*.part' -exec rm {} + } check_online run_ipset cleanup exit $?
Just a suggestion: in FORWARD chain use REJECT that prevent DROP wait out timing and drop after n-seconds.
Code:iptables -I FORWARD -m set $MATCH_SET malware-filter src,dst -j REJECT
If there's option to choose REJECT or DROP, it will suit everyone.well thats easily changed i can make a simple value for it in the settings does that sound better ?
nice -n 2 iptables -I FORWARD -m set $MATCH_SET malware-filter src,dst -j REJECT
else
nice -n 2 iptables -D FORWARD -m set $MATCH_SET malware-filter src,dst -j REJECT
nice -n 2 iptables -I FORWARD -m set $MATCH_SET malware-filter src,dst -j REJECT
pkts bytes target prot opt in out source destination
5 915 REJECT all -- * * 0.0.0.0/0 0.0.0.0/0 set malware-filter src,dst reject-with icmp-port-unreachable
7865K 2324M ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED
Everyone can do what they want with their skills and experience. Feel free to do what suits the individual.I do not agree with this suggestion. As this is for blocking known bad, why we need to use REJECT to actively respond to all malicious requests? REJECT only increases the router load and potentially allows more malicious requests.
In company firewall configuration, it is common sense to use the DROP action instead of REJECT.
This is not like M$ privacy filter that timeout will be a concern for web page loading. As malware filter, it is more IP based, not URL based. Timeout is not usually observed by normal users. It's the time to check your system security if you see long waiting time loading a suspicious website.
Welcome To SNBForums
SNBForums is a community for anyone who wants to learn about or discuss the latest in wireless routers, network storage and the ins and outs of building and maintaining a small network.
If you'd like to post a question, simply register and have at it!
While you're at it, please check out SmallNetBuilder for product reviews and our famous Router Charts, Ranker and plenty more!