Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Shell 使用命令行发送电子邮件并保存IP规则_Shell_Email_Sendmail - Fatal编程技术网

Shell 使用命令行发送电子邮件并保存IP规则

Shell 使用命令行发送电子邮件并保存IP规则,shell,email,sendmail,Shell,Email,Sendmail,我正在编写一个脚本,从数据库中的一个表中选择一些IP,然后使用IPTables规则禁止这些IP,最后一步是通过电子邮件通知,但我得到两个错误: #!/bin/bash Now=$(date +"%d-%m-%Y %T") fileLocation="/var/lib/mysql/DBName/" fileName="ip2ban.txt" filePath=$fileLocation$fileName curLocation=$(pwd) #Connect to DB and select

我正在编写一个脚本,从数据库中的一个表中选择一些IP,然后使用IPTables规则禁止这些IP,最后一步是通过电子邮件通知,但我得到两个错误:

#!/bin/bash

Now=$(date +"%d-%m-%Y %T")

fileLocation="/var/lib/mysql/DBName/"
fileName="ip2ban.txt"
filePath=$fileLocation$fileName
curLocation=$(pwd)

#Connect to DB and select ban_ip
mysql -u root -pPASSWORD -D DBName -e 'SELECT ip INTO OUTFILE "'$filePath'" FROM ban_ip WHERE ip_tables = "0"' >> banIP.log 2>&1

selRes=$?

# If the command was successful
if [ $selRes -eq "0" ]
then   

    # We need to check if the file exists on the saved location
    #find $fileLocation -type f -iname "ip2ban.txt" -empty => To check if file empty or not

    if [ -f $filePath ]
then   
    mv $filePath $curLocation'/ip2ban.txt'

    # Connect to DB and update the ban_ip
    mysql -u root -pPASSWORD -D DBName -e 'UPDATE ban_ip SET ip_tables = "1" WHERE ip_tables = "0"' >> banIP.log 2>&1

     upRes=$?

    if [ $upRes -eq "0" ]
            then

    # Send message for succesful result
    echo -e "Database updated with new banned IPs on $Now \nThank you for using this script" 2>&1 | sed '1!b;s/^/To: myID@gmail.com\nSubject: New banned IPs[Success]\n\n/' | sendmail -t

    else

    # Send message for failure result
    echo -e "We cannot update the ban_ip table on $Now \nThank you for using this script" 2>&1 | sed '1!b;s/^/To: myID@gmail.com\nSubject: [Failure] New banned IPs\n\n/' | sendmail -t

    fi

fi

else
    echo 'Something wrong with Select statment on' $Now >> banIP.log
fi

# Save IPTables rules
iptables-save > /root/Scripts/IPTables/BannedIPs.conf // LIGNE 53
line 53: iptables-save: command not found 
line 37: sendmail: command not found
我有两个错误:

#!/bin/bash

Now=$(date +"%d-%m-%Y %T")

fileLocation="/var/lib/mysql/DBName/"
fileName="ip2ban.txt"
filePath=$fileLocation$fileName
curLocation=$(pwd)

#Connect to DB and select ban_ip
mysql -u root -pPASSWORD -D DBName -e 'SELECT ip INTO OUTFILE "'$filePath'" FROM ban_ip WHERE ip_tables = "0"' >> banIP.log 2>&1

selRes=$?

# If the command was successful
if [ $selRes -eq "0" ]
then   

    # We need to check if the file exists on the saved location
    #find $fileLocation -type f -iname "ip2ban.txt" -empty => To check if file empty or not

    if [ -f $filePath ]
then   
    mv $filePath $curLocation'/ip2ban.txt'

    # Connect to DB and update the ban_ip
    mysql -u root -pPASSWORD -D DBName -e 'UPDATE ban_ip SET ip_tables = "1" WHERE ip_tables = "0"' >> banIP.log 2>&1

     upRes=$?

    if [ $upRes -eq "0" ]
            then

    # Send message for succesful result
    echo -e "Database updated with new banned IPs on $Now \nThank you for using this script" 2>&1 | sed '1!b;s/^/To: myID@gmail.com\nSubject: New banned IPs[Success]\n\n/' | sendmail -t

    else

    # Send message for failure result
    echo -e "We cannot update the ban_ip table on $Now \nThank you for using this script" 2>&1 | sed '1!b;s/^/To: myID@gmail.com\nSubject: [Failure] New banned IPs\n\n/' | sendmail -t

    fi

fi

else
    echo 'Something wrong with Select statment on' $Now >> banIP.log
fi

# Save IPTables rules
iptables-save > /root/Scripts/IPTables/BannedIPs.conf // LIGNE 53
line 53: iptables-save: command not found 
line 37: sendmail: command not found
但是,sendamil已经安装,带有邮件、后缀:

# which sendmail
/usr/sbin/sendmail

# which mail
/usr/bin/mail

# which postfix
/usr/sbin/postfix
感谢您通常的支持

根据Linux手册页:

路径设置为“/usr/bin:/bin”

这意味着您的shell脚本将无法在/usr/sbin或/sbin下找到任何内容。通过在脚本顶部附近添加以下内容来更改此设置:

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

还可以从crontab中设置环境。有关如何执行此操作,请参阅。

执行此脚本时,$PATH的值是多少?我假设你是以root用户的身份运行它?我认为作为用户,它在crontab上,即使我尝试作为命令行发送邮件,它也不起作用。我认为除非你是root用户,否则你将无法使
iptables save
工作。所以您需要告诉cron以root身份运行它。如果要对iptables进行更改,您需要是root用户。我如何告诉cron以root用户身份运行它?从
root
的crontab运行它,或者使用
sudo
授予自己必要的权限。可能创建一个只有此权限的专用伪用户,并从该用户的crontab运行此脚本。我在脚本顶部添加了一行,我正在等待cron的执行,这样我就可以有一个邮件发送应答器,我编辑了main.cf文件以获取后缀,我对行#inet_protocols=all进行了注释,现在它工作得很好,就像一个符咒,我甚至用正确的路径测试它,即/sbin/save iptables和works file,谢谢你,伙计