作为系统服务运行 shell 脚本

作为系统服务运行 shell 脚本

我正在将 shell 脚本作为系统服务运行,但它却无法正常运行

input="/home/sipl/inf"
while read -r line
do
  sudo iptables -t nat -i $line -A PREROUTING -p tcp --dport 1:65535 -j REDIRECT --to-port 5000
done < "$input"
Sep 06 17:48:44 sipl-NUC7PJYH Diskutilization[7384]: Bad argument `PREROUTING'
Sep 06 17:48:44 sipl-NUC7PJYH Diskutilization[7384]: Try `iptables -h' or 'iptables --help' for more information.
Sep 06 17:48:44 sipl-NUC7PJYH sudo[7383]: pam_unix(sudo:session): session closed for user root
Sep 06 17:48:44 sipl-NUC7PJYH sudo[7385]:     root : PWD=/ ; USER=root ; COMMAND=/usr/sbin/iptables -t nat -i -A PREROUTING -p udp --dport 1:65535 -j REDIRECT --to-port 5000
Sep 06 17:48:44 sipl-NUC7PJYH sudo[7385]: pam_unix(sudo:session): session opened for user root(uid=0) by (uid=0)
Sep 06 17:48:44 sipl-NUC7PJYH Diskutilization[7386]: Bad argument `PREROUTING'
Sep 06 17:48:44 sipl-NUC7PJYH Diskutilization[7386]: Try `iptables -h' or 'iptables --help' for more information.
Sep 06 17:48:44 sipl-NUC7PJYH sudo[7385]: pam_unix(sudo:session): session closed for user root

答案1

从日志中你可以看出:

COMMAND=/usr/sbin/iptables -t nat -i -A PREROUTING ...

您没有line接口的变量名称。更正此问题,您将获得成功。这应该是这样的:

COMMAND=/usr/sbin/iptables -t nat -i eth0 -A PREROUTING ...

相关内容