如何在 178.000 行的文件中的 IP 之前添加自定义字符?
How to add custom chars before IPs in a file with 178.000 lines?
我有一个文本文件,其中包含来自 myip.ms 的 178.000 行列入黑名单的 IP (12 MB),格式如下:
1.2.81.113 # 2014-09-10, 1.2.81.113, CHN, 51
1.2.82.108 # 2014-09-10, 1.2.82.108, CHN, 51
1.2.83.179 # 2014-09-11, 1.2.83.179, CHN, 51
1.2.86.210 # 2014-09-07, 1.2.86.210, CHN, 51
1.2.109.22 # 2014-09-06, 1.2.109.22, CHN, 51
现在我需要像这样在每一行之前添加 "Require not ip ":
Require not ip 1.2.81.113 # 2014-09-10, 1.2.81.113, CHN, 51
Require not ip 1.2.82.108 # 2014-09-10, 1.2.82.108, CHN, 51
Require not ip 1.2.83.179 # 2014-09-11, 1.2.83.179, CHN, 51
Require not ip 1.2.86.210 # 2014-09-07, 1.2.86.210, CHN, 51
Require not ip 1.2.109.22 # 2014-09-06, 1.2.109.22, CHN, 51
如何以最少的努力实现最佳效果以满足我的 apache 配置,或者是否有更好的方法来包含黑名单?
<Directory /var/www/>
<RequireAll>
Require all granted
# IP Blacklists
Include full_blacklist_database.txt
</RequireAll>
</Directory>
这里是 python 中的示例,在每行前面添加 "Require not ip "。
fr = open("/tmp/full_blacklist_database.txt", "r")
fw = open("/tmp/full_blacklist_database_require_not_ip.txt", "w")
for line in fr.readlines():
fw.write("Require not ip " + line.strip() + "\n")
fr.close();
fw.close();
文件 full_blacklist_database_require_not_ip.txt
将具有所需的结果。
之后您可以将文件内容复制到您的 apache 配置文件中。
希望对您有所帮助。
您可以使用高级文件编辑器 Notepd++ then follow the example here 将示例第 4 点的方式更改为这个:
结果如下:
在 Java 中,您可以这样做:
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import javax.swing.JOptionPane;
public class StringAppender {
public static void main(String[] args) {
String fileIn = JOptionPane.showInputDialog("Enter the path to the file");
try{
if(new File(fileIn).exists()){
StringBuilder sb = new StringBuilder();
BufferedReader br = new BufferedReader(new FileReader(fileIn));
String tmp;
while((tmp = br.readLine()) != null)
sb.append("Require not ip " + tmp + "\n");
br.close();
BufferedWriter out = new BufferedWriter(new FileWriter(fileIn + ".new.txt"));
out.write(sb.toString());
out.close();
JOptionPane.showMessageDialog(null, "Done! New file saved as: " + fileIn + ".new.txt");
}else{
JOptionPane.showMessageDialog(null, "Requested file doesn't exist!");
}
}catch(Exception e){
JOptionPane.showMessageDialog(null, "Unable to perform changes!");
}
}
}
预编译版可以下载here。要 运行 它 cd 到目录并执行 java StringAppender
我有一个文本文件,其中包含来自 myip.ms 的 178.000 行列入黑名单的 IP (12 MB),格式如下:
1.2.81.113 # 2014-09-10, 1.2.81.113, CHN, 51
1.2.82.108 # 2014-09-10, 1.2.82.108, CHN, 51
1.2.83.179 # 2014-09-11, 1.2.83.179, CHN, 51
1.2.86.210 # 2014-09-07, 1.2.86.210, CHN, 51
1.2.109.22 # 2014-09-06, 1.2.109.22, CHN, 51
现在我需要像这样在每一行之前添加 "Require not ip ":
Require not ip 1.2.81.113 # 2014-09-10, 1.2.81.113, CHN, 51
Require not ip 1.2.82.108 # 2014-09-10, 1.2.82.108, CHN, 51
Require not ip 1.2.83.179 # 2014-09-11, 1.2.83.179, CHN, 51
Require not ip 1.2.86.210 # 2014-09-07, 1.2.86.210, CHN, 51
Require not ip 1.2.109.22 # 2014-09-06, 1.2.109.22, CHN, 51
如何以最少的努力实现最佳效果以满足我的 apache 配置,或者是否有更好的方法来包含黑名单?
<Directory /var/www/>
<RequireAll>
Require all granted
# IP Blacklists
Include full_blacklist_database.txt
</RequireAll>
</Directory>
这里是 python 中的示例,在每行前面添加 "Require not ip "。
fr = open("/tmp/full_blacklist_database.txt", "r")
fw = open("/tmp/full_blacklist_database_require_not_ip.txt", "w")
for line in fr.readlines():
fw.write("Require not ip " + line.strip() + "\n")
fr.close();
fw.close();
文件 full_blacklist_database_require_not_ip.txt
将具有所需的结果。
之后您可以将文件内容复制到您的 apache 配置文件中。
希望对您有所帮助。
您可以使用高级文件编辑器 Notepd++ then follow the example here 将示例第 4 点的方式更改为这个:
结果如下:
在 Java 中,您可以这样做:
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import javax.swing.JOptionPane;
public class StringAppender {
public static void main(String[] args) {
String fileIn = JOptionPane.showInputDialog("Enter the path to the file");
try{
if(new File(fileIn).exists()){
StringBuilder sb = new StringBuilder();
BufferedReader br = new BufferedReader(new FileReader(fileIn));
String tmp;
while((tmp = br.readLine()) != null)
sb.append("Require not ip " + tmp + "\n");
br.close();
BufferedWriter out = new BufferedWriter(new FileWriter(fileIn + ".new.txt"));
out.write(sb.toString());
out.close();
JOptionPane.showMessageDialog(null, "Done! New file saved as: " + fileIn + ".new.txt");
}else{
JOptionPane.showMessageDialog(null, "Requested file doesn't exist!");
}
}catch(Exception e){
JOptionPane.showMessageDialog(null, "Unable to perform changes!");
}
}
}
预编译版可以下载here。要 运行 它 cd 到目录并执行 java StringAppender