linux shell 新手求教

!/bin/bash

systemctl status xxxx -l > /tmp/ip.txt
$IP=(egrep -o '([0-9]{1,3}.){1,3}[0-9]{3}' /tmp/ip.txt >/tmp/ip_block.txt)
firewall-cmd --add-rich-rule="rule family="ipv4" destination address="$IP" reject"
firewall-cmd --add-rich-rule="rule family="ipv4" destination address="$IP" reject" --per
firewall-cmd --reload

用这个脚本想自动屏蔽非法IP时,提示如图错误,求解?
image.png

阅读 1.7k
2 个回答

修改后的代码如下

#!/bin/sh
systemctl status xxxx -l > /tmp/ip.txt  
$IP=`egrep -o '(\[0-9\]{1,3}.){1,3}\[0-9\]{3}' /tmp/ip.txt >/tmp/ip\_block.txt`
firewall-cmd --add-rich-rule="rule family="ipv4" destination address="$IP" reject"  
firewall-cmd --add-rich-rule="rule family="ipv4" destination address="$IP" reject" --per  
firewall-cmd --reload

!/bin/bash

systemctl status shadowsocks-libev -l > /tmp/ip.txt

IP=$(grep -o '[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}' /tmp/ip.txt > /tmp/ip_block.txt)

IP=$(more /tmp/ip_block.txt)

firewall-cmd --add-rich-rule="rule family="ipv4" destination address=$IP reject"
firewall-cmd --add-rich-rule="rule family="ipv4" destination address=$IP reject" --per
firewall-cmd --reload
~
感谢回答。
脚本改成这样后,对IP的提取完美了,但是对于提取的IP结果是多行的就提示如图的错误。image.png
已测试如果文本是单行的,无异常。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题