我正在尝试完成这项工作,但没有输出。我在这里尝试做的是来自一个具有多个名称的 txt 文件,它将是 grep 并导出与用户名匹配的 IP 地址。并将继续循环读取每一行并导出到 BadIP.out 有什么帮助吗?
#!/bin/sh
cat /Badusers.txt
while IFS= read -r LINE
do
grep '"$LINE"' /var/log/test.log
awk -F" " '{print $8}'
grep -o '^[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}' > /badIP.out
done </badIP.out
更新:
让我从头开始,因为我现在只发布了我遇到问题的地方。
我有一台运行 2 个相同程序的服务器。我给我的客户 2 个连接,让每个程序从一个设备连接。最近我注意到其中一些单独使用它们,所以我不得不从日志中发明一些东西。
该程序会记录自己的数据,例如 IP、用户名和 NODEID。从 nodeID 中,您可以验证用户是否在作弊。我想比较每个程序的日志并匹配提取数据以匹配。如果匹配则表示一切正常,如果 NodeID 失败则表示作弊。所以我做的是这个
1) 我通过匹配用户的 NodeID 来获取程序的两个日志 2) 然后将它们从该日志导出到另一个日志 我必须将用户与 nodeid 分开,因为日志将它们作为一个带有“@”的完整单词将它们结合起来 3) 将用户名与 NodeID 分开(导出到 txt 文件) 4) 使用脚本通过循环读取 TXT 文件并获取与用户名匹配的 IP 地址的日志并将它们导出到 BadIP.out 5 ) 然后使用循环读取 BADIP.out 来禁止使用 IPtables 的用户 IP
我已经创建了这些文件。PS我不是开发人员:)
这是我运行的程序
#!/bin/bash
./cam.sh
./cam2.sh
diff -i -b -B -q cam.txt cam2.txt
if [ ! $? -eq 0 ]; then
echo "**** File has changed*****"
diff <(sort /cam.txt) <(sort /cam2.txt)|awk '/^</{print $5>"temp1.out"}/^>/{print $5>"temp2.out"}'
#awk -F "@" 'BEGIN{while(getline<"temp1.out") a [$1]=1 } ; a[$1] !=2 {print $1}' temp2.out
awk -F'@' 'BEGIN{while(getline<"cam.txt") a[$2,$3]=1};a[$2,$3]!=1' cam2.txt >> /notify.txt
#diff -i -b -B -y temp1.out temp2.out >> /diff.txt
grep 'client' /notify.txt | awk -F" " '{print $4}' | cut -d@ -f1 >> /Badusers.txt
grep 'client' /notify.txt | awk -F" " '{print $4}' | cut -d@ -f2 | awk -F "," '{print $1}' >> /Badnodeid.txt
./mail.sh
#rm *.txt
#rm *.out
else
echo "same"
fi
这个从program1读取到log1
cam.sh
#!/bin/bash
for filename in /var/log/test.log; do
if [ "$(grep "xyz" $filename)" ]; then
awk '/xyz/ && $8 == version' $filename >> /cam.txt
else
echo "$time Boo!! Failure.."
fi
done
这个从program2读取到log2
cam2.sh
#!/bin/bash
for filename in /var/log/test2.log; do
if [ "$(grep "xyz" $filename)" ]; then
awk '/xyz/ && $8 == version' $filename >> /cam2.txt
else
echo "$time Boo!! Failure.."
fi
done
这个与用户发送电子邮件
邮件.sh
#!/bin/bash
mutt -s "Test mail" -a /notify.txt *****@gmail.com < /notify.txt
日志样本
cam.txt & cam2.txt
03:00:08.818 Prg: client xyz661s@e15279f57cc56c7f, running Prg 2.1.4
03:00:08.942 Prg: client xyz886s@1c8f2a6efe3963d7, running Prg 2.2.1
03:00:09.576 Prg: client xyz502s@165e25ac273d4751, running Prg 2.1.4
03:00:10.235 Prg: client xyz852s@6a16130252dea90a, running Prg 2.1.4
03:00:11.677 Prg: client xyz808s@ed52ddf03f1e7111, running Prg 2.1.3
03:00:11.685 Prg: client xyz034s@63007fd8e9591501, running Prg 2.1.4
03:00:11.687 Prg: client xyz885s@84ac60cf204e94a2, running Prg 2.2.1
03:00:11.796 Prg: client xyz687s@f6492af984a26f37, running Prg 2.1.4
03:00:11.818 Prg: client xyz584s@6b70bcc9670dd4f4, running Prg 2.1.1
03:00:11.891 Prg: client xyz544s@5c3284516ab8e072, running Prg 2.2.1
03:00:11.895 Prg: client xyz529s@f9c0fc6756d62f4f, running Prg 2.1.4
03:00:11.912 Prg: client xyz509s@dfb6da96a35a3022, running Prg 2.1.4
03:00:11.915 Prg: client xyz581s@6d7512ee647d3441, running Prg 2.1.2
坏用户.txt
xyz712s
xyz553s
xyz500s
xyz676s
xyz553s
xyz712s
xyz697s
坏节点id.txt
403a8a9fe084b6cb
d6fe8f201e4d854e
5a7321d7b49cef9b
6da486276fafe5f5
d6fe8f201e4d854e
通知.txt
03:00:11.715 Prg: client xyz712s@403a8a9fe084b6cb, running Prg 2.1.3
03:00:13.674 Prg: client xyz553s@d6fe8f201e4d854e, running Prg 2.2.1
03:00:16.260 Prg: client xyz500s@5a7321d7b49cef9b, running Prg 2.1.4
10:02:42.961 Prg: client xyz676s@6da486276fafe5f5, running Prg 2.2.1
10:53:55.374 Prg: client xyz553s@d6fe8f201e4d854e, running Prg 2.2.1
12:36:32.885 Prg: client xyz712s@403a8a9fe084b6cb, running Prg 2.1.3
12:36:54.283 Prg: client xyz697s@51724d66fec8da4c, running Prg 2.1.4
12:37:21.052 Prg: client xyz712s@403a8a9fe084b6cb, running Prg 2.1.3
temp1.out
xyz661s@e15279f57cc56c7f,
xyz886s@1c8f2a6efe3963d7,
xyz502s@165e25ac273d4751,
xyz852s@6a16130252dea90a,
xyz808s@ed52ddf03f1e7111,
xyz034s@63007fd8e9591501,
temp2.out
xyz661s@e15279f57cc56c7f,
xyz886s@1c8f2a6efe3963d7,
xyz502s@165e25ac273d4751,
xyz852s@6a16130252dea90a,
xyz021s@1ad450e34bc26dc7,
xyz712s@403a8a9fe084b6cb,
xyz544s@5c3284516ab8e072,
现在剩下的是为上面的脚本制作while循环,以便从test.log中为用户获取并找到他们的IP,将它们导出到BadIP.out然后我需要这样的东西来禁止
$logdir/badIP.out > $logdir/badIP.block
while IFS= read -r EachLine
do
command="iptables -A INPUT -s "$EachLine" -j DROP"
echo $command
$command
done < $logdir/badIP.block
rm $logdir/badIP.block
if [ -s $logdir/illegaluser.txt ] ; then
iptables-save -c > $logdir/iptables-save.new