如何在由服务(systemd)(file.service)执行的脚本中执行命令zenity



我有一个小问题。我正在用systemd做一个服务(守护进程(。脚本如下:

if [ $intento = 5 ];then
iptables -I INPUT -s ${sublista[0]} -j DROP -m comment --comment "IP bloqueada por sshield"
date=$(date)
echo "${sublista[0]} $date" >> /var/cache/sshield.deny
zenity --notification --text "IP address ${sublista[0]} denied at $date - sshield"
email ivanherediaplanas@hotmail.com "Nueva regla iptables | ${sublista[0]} denied" "The ${sublista[0]} ip address is denied by brute force's attack ssh.<br><br>Date: $date"
declare -a ips=(${ips[@]/${sublista[0]}=>$intento/})
fi

想法如下:

如果尝试次数超过五次,它会给出ip地址并锁定它。发送邮件并显示zenity的弹出窗口

问题是,弹出窗口不会显示。

zenity——通知——文本";IP地址${sublista[0]}在$date-shield"处被拒绝

我相信这是因为,scrpt是由/lib/systemd/system/shield.service 中的服务文件执行的

[Unit]
Description=Service for protect attacks of brute force ssh's
[Service]
Type=simple
ExecStart=/etc/sshield/sshield.sh
ExecStop=/etc/sshield/sshield.sh stop
RemainAfterExit=yes
Restart=always
[Install]
WantedBy=multi-user.target

我认为问题在于:Type=simple

此外,我尝试使用这个:

echo "${sublista[0]} $date" >> /var/cache/sshield.deny
sshield --bell "IP address ${sublista[0]} denied at $date - sshield"
email ivanherediaplanas@hotmail.com "Nueva regla iptables | ${sublista[0]} denied" "The ${sublista[0]} ip address is denied by brute force's attack ssh.<br><br>Date: $date"

sshield--bell"IP地址${sublista[0]}在$date-shield"处被拒绝;

命令sshield是路径/bin/sshield中的一个脚本,我将遵循它:

elif [[ $argumento == "--bell" ]];then
if [[ $# -gt 3 ]];then
echo -e "33[1;31m[-]33[0m Only one value"
echo "You use '--help' or '-h' for more information"
elif [[ $# = 1 ]];then
echo -e "33[1;31m[-]33[0m It needs one value"
echo "You use '--help' or '-h' for more information"
else
zenity --notification --text "$2"
fi
else
[...]

标记:zenity--通知--文本"2〃

但是,它不起作用。我该如何解决?

错误在zenity中:图片:journalcl-u ssfield

用于在服务(systemd(中执行GUI(图形用户界面(。首先,您必须添加以下内容:

[Service]
Environment="DISPLAY=:0"
Environment="XAUTHORITY=/home/ivan/.Xauthority"

结果:

[Unit]
Description=Service for protect attacks of brute force ssh's
[Service]
Type=simple
Environment="DISPLAY=:0"
Environment="XAUTHORITY=/home/ivan/.Xauthority"
ExecStart=/etc/sshield/sshield.sh
ExecStop=/etc/sshield/sshield.sh stop
RemainAfterExit=yes
Restart=always
[Install]
WantedBy=multi-user.target

在脚本中,添加:

export DISPLAY=":0"

最新更新