다른 스크립트를 생성하는 bash 스크립트가 있습니다. 파일에만 에코 출력을 원하지만 시도한 모든 항목(예: &>)이 화면에 표시됩니다. 코드는 다음과 같습니다.
while read line; do echo "sudo ufw deny from "$line" to any"; done < /home/photobcdev/hack-ban.txt >>/home/photobcdev/hack-ban.tmp
잘 작동하지만 실행할 때마다 수백 줄이 표시됩니다. 어떻게 조용히 할 수 있나요? 상황별 정보에 대한 전체 스크립트는 다음과 같습니다.
#!/bin/bash
DATE=$(date +%Y-%m-%d--%H-%M)
echo $(date)" Suppression fichiers temporaires"
rm /home/photobcdev/hack.txt 2> /dev/null
rm /home/photobcdev/hack-ip.txt 2> /dev/null
rm /home/photobcdev/hack-ban.txt 2> /dev/null
rm /home/photobcdev/hack-ban.tmp 2> /dev/null
rm /home/photobcdev/hack-ban-diff.sh 2> /dev/null
echo $(date)" Génération liste IPs"
sudo grep 'Disconnected from invalid user' /var/log/auth.log>>/home/photobcdev/hack.txt
grep -o -E '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' /home/photobcdev/hack.txt>>/home/photobcdev/hack-ip.txt
sort -u /home/photobcdev/hack-ip.txt>>/home/photobcdev/hack-ban.txt
wc -l /home/photobcdev/hack-ban.txt
echo $(date)" Génération script ufw ban /IP"
#while read line; do echo "sudo ufw deny from "$line" to any"; done < /home/photobcdev/hack-ban.txt >>/home/photobcdev/hack-ban.tmp
while read line; do
echo "sudo ufw deny from $line to any" >> /home/photobcdev/hack-ban.tmp
done < /home/photobcdev/hack-ban.txt
echo $(date)" Création fichier final"
sort -u /home/photobcdev/hack-ban.tmp>>/home/photobcdev/hack-ban-${DATE}.sh
sort -u /home/photobcdev/hack-ban.sh
wc -l /home/photobcdev/hack-ban-${DATE}.sh
chmod +x /home/photobcdev/hack-ban-${DATE}.sh
grep -v -f /home/photobcdev/hack-ban.sh /home/photobcdev/hack-ban-${DATE}.sh >>/home/photobcdev/hack-ban-diff.sh
if [ -s /home/photobcdev/hack-ban-diff.sh ]; then
# Fichier non vide
echo $(date)" Fichier hack-ban-diff.sh généré"
wc -l /home/photobcdev/hack-ban-diff.sh
rm -f /home/photobcdev/hack-ban.sh 2> /dev/null
mv /home/photobcdev/hack-ban-${DATE}.sh /home/photobcdev/hack-ban.sh
chmod +x /home/photobcdev/hack-ban-diff.sh
echo $(date)" Exécution hack-ban-diff.sh"
/home/photobcdev/hack-ban-diff.sh
else
# Fichier vide
rm -f /home/photobcdev/hack-ban-diff.sh 2> /dev/null
rm -f /home/photobcdev/hack-ban-${DATE}.sh 2> /dev/null
echo $(date)" Pas de nouvelles IP détectées"
fi