PDA

View Full Version : ban ips from txt file



feanor
31st August 2017, 16:28
how can i do that with bash script or other way? i am using ubuntu 14.04 , ips will be added constantly

txt file will be look like this :

123.123.123.123
345.231.123.342
123.123.452.231

Thanks

kung foo man
31st August 2017, 19:44
Something like (untested):

php banips.php


$ips = file("ips.txt");
foreach ($ips as $ip) {
$cmd = "iptables -A INPUT -s $ip -j DROP";
echo "$cmd\n";
system($cmd);
}


Edit: to constantly do this, simply make a endless loop around it and start it in a root screen session. Your home work.

feanor
1st September 2017, 14:23
./ban.sh: line 4: syntax error near unexpected token `('
./ban.sh: line 4: ` $ips = file("ips.txt");'

feanor
1st September 2017, 15:50
#!/bin/bash
_input=/pathto/ips.txt
IPT=/sbin/iptables

egrep -v "^#|^$" $_input | while IFS= read -r ip
do
$IPT -A INPUT -s $ip -j DROP
done < "$_input"

i made this taking example from google search but if i put in an infinite loop same ip will be banned and banned again am i right? how can i edit it to work properly?


------------

well , if we dont select ips start as ** with egrep/grep command and if we put ** starts of banned ips it can work properly. but i dont know how these commands work properly

feanor
2nd September 2017, 00:26
#!/usr/bin/php -q
<?php

$TxtDosyaYol="/pathto/ip.txt";

while(1)
{
echo "working";
$IpListe=file_get_contents($TxtDosyaYol);
$IpArray=explode("\n", $IpListe);
for($i=0;$i<count($IpArray);$i++)
{
if(strlen($IpArray[$i])>5)
{
$Komut="/sbin/iptables -A INPUT -s ".$IpArray[$i]." -j DROP";
shell_exec($Komut);
}
}
unlink($TxtDosyaYol);
sleep(20); // wait 20 seconds
}

?>

it deletes ip.txt file when banned all ips , it works for me :)

Paho
5th September 2017, 19:33
duplicate topic
https://killtube.org/showthread.php?2662-Banip-from-game-to-FairWall&highlight=banip