【问题标题】:How to execute the command zenity in a script executed by a service (systemd) (file .service)如何在服务(systemd)(文件.service)执行的脚本中执行命令zenity
【发布时间】:2020-02-26 17:49:33
【问题描述】:

我有一个小问题。我正在用 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 --notification --text "IP 地址 ${sublista[0]} 在 $date 被拒绝 - sshield"

我相信这是因为,scrpt 是由 /lib/systemd/system/sshield.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 被拒绝 - sshield"

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

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

标记:zenity --notification --text "$2"

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

错误在于zenity: image: journalctl -u sshield

【问题讨论】:

  • 请检查错误日志。它可能不知道要显示在哪个显示器上,因为您在 UI 之外运行它。
  • 什么错误日志?
  • 查看单个systemd服务的日志,可以使用journalctl -u sshield
  • zenity --notification --text "Whatever" 在终端中运行时的输出是什么?
  • 输出zenity --notification --text "whatever" show a popup with the text specified。它可以工作,但在服务中不行。

标签: linux bash service systemd


【解决方案1】:

用于在服务 (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"

【讨论】:

  • 这应该被标记为答案。它解决了它。十分感谢!对于我的情况,我不需要在脚本中添加 export DISPLAY=":0"Environment variables 帮我搞定了,谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-20
  • 1970-01-01
相关资源
最近更新 更多