Let's start from the beginning: I have which wakes up my server when в request ты ешь Пи in on tcpdump
, and it works like в treat!
My problem is that I want my server to sleep when there is не incoming датирует for, let's say, 30 mins. The server is used only by я, both from within and outside my ЛВС.
I think that I need to использовал tcpdump
in в shell рукописный шрифт to монитор the port or the IP and, if there is не incoming, it will react to it. I have an разрабатывает to do this, because I have looked around on the Интернет for 2 days, and tried в lot of things, but I can't succeed.
How хан I do this?
Я предполагаю, что "никакие входящие данные" не означают: tcpdump
не печатает строки. Поэтому мы должны запустить 30-минутный таймер и сбросить его каждый раз, когда tcpdump
печать строка.
Запуск его довольно прост:
sleep 30m && poweroff
, Как мы сбрасываем его? Простой: мы уничтожаем sleep
и перезапускаем его.
, Как мы обнаруживаем новые строки от tcpdump
? Старое доброе while read -r
цикл.
Соединение всего:
#!/bin/bash
set -e
shutdown-timer() {
# Waits 30 minutes and shuts down the computer.
sleep 30m && poweroff
}
reset-timer() {
# Kills 'shutdown-timer' (if it's running) and
# restarts it, saving its PID in 'timerpid'.
if [[ -n "$timerpid" ]]
then
kill "$timerpid" || true
fi
shutdown-timer &
timerpid="$!"
}
# Start the timer for the first time.
reset-timer
tcpdump | while read -r line
do
# A new packet was sent or received.
reset-timer
done
Примечание, что там мог бы нарушать пакеты время от времени. Например, Вы, возможно, настроили APT для проверки на обновления каждые 5 минут. Это будет препятствовать тому, чтобы компьютер закрылся, потому что таймер сбрасывался бы каждые 5 минут.
, Если Вам нужно, можно отфильтровать вывод tcpdump
использование grep
или безотносительно инструмента, как это:
tcpdump | grep -v archive.ubuntu.com | while read -r line