Results 1 to 6 of 6

Thread: ban ips from txt file

  1. #1
    Private First Class
    Join Date
    Nov 2015
    Posts
    128
    Thanks
    37
    Thanked 14 Times in 14 Posts

    ban ips from txt file

    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
    Last edited by feanor; 31st August 2017 at 17:35.

  2. #2
    Assadministrator kung foo man's Avatar
    Join Date
    Jun 2012
    Location
    trailerpark
    Posts
    2,010
    Thanks
    2,102
    Thanked 1,084 Times in 753 Posts
    Something like (untested):

    php banips.php
    PHP Code:
    $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.
    timescale 0.01

  3. #3
    Private First Class
    Join Date
    Nov 2015
    Posts
    128
    Thanks
    37
    Thanked 14 Times in 14 Posts
    ./ban.sh: line 4: syntax error near unexpected token `('
    ./ban.sh: line 4: ` $ips = file("ips.txt");'

  4. #4
    Private First Class
    Join Date
    Nov 2015
    Posts
    128
    Thanks
    37
    Thanked 14 Times in 14 Posts
    PHP Code:
    #!/bin/bash 
    _input=/pathto/ips.txt
    IPT
    =/sbin/iptables

    egrep 
    -"^#|^$" $_input | while IFSread -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
    Last edited by feanor; 1st September 2017 at 18:20.

  5. #5
    Private First Class
    Join Date
    Nov 2015
    Posts
    128
    Thanks
    37
    Thanked 14 Times in 14 Posts
    PHP Code:
    #!/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
    Last edited by feanor; 2nd September 2017 at 01:52.

  6. The Following User Says Thank You to feanor For This Useful Post:

    kung foo man (2nd September 2017)

  7. #6
    Private Paho's Avatar
    Join Date
    Feb 2014
    Location
    Russia
    Posts
    101
    Thanks
    43
    Thanked 48 Times in 37 Posts

  8. The Following User Says Thank You to Paho For This Useful Post:

    kubislav23 (6th September 2017)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •