![ufw がプログラムで実行されているかどうかを確認する方法](https://rvso.com/image/1083748/ufw%20%E3%81%8C%E3%83%97%E3%83%AD%E3%82%B0%E3%83%A9%E3%83%A0%E3%81%A7%E5%AE%9F%E8%A1%8C%E3%81%95%E3%82%8C%E3%81%A6%E3%81%84%E3%82%8B%E3%81%8B%E3%81%A9%E3%81%86%E3%81%8B%E3%82%92%E7%A2%BA%E8%AA%8D%E3%81%99%E3%82%8B%E6%96%B9%E6%B3%95.png)
ほとんどのサービス (例: privoxy) では、サービス ステータスを取得する簡単な方法があります。
ps -C [servicename]
次に、終了コードを確認します($?)
。0: サービスは実行中 1: 実行されていません これは、ufw では当てはまりません (サービスとして認識されない?)。sudo service ufw status
ufw が実行中かどうかに関係なく、常に 0 で終了します。ufw ステータスを確認するために使用されるコマンドの終了コードを使用して、プログラムで ufw を取得するための提案はありますか?
答え1
によって返される終了コードは常に 0 なので、ステータス値についてはsudo ufw status
次の操作を行うだけです。grep
$ sudo ufw status
Status: inactive
$ sudo ufw status | grep -qw active
$ echo $?
1
-w
正しく動作させるには、次のオプションを使用する必要がありますman grep
:
-w, --word-regexp
Select only those lines containing matches that form whole
words. The test is that the matching substring must either be
at the beginning of the line, or preceded by a non-word
constituent character. Similarly, it must be either at the end
of the line or followed by a non-word constituent character.
Word-constituent characters are letters, digits, and the
underscore.
-q
静かなモードなので、標準出力には何も書き込まれません
答え2
UFWの使用状況を確認するには
sudo ufw status verbose
出力は次のようになります
my@machine:~$ sudo ufw status verbose
[sudo] password for youruser:
Status: active
Logging: on (low)
Default: deny (incoming), allow (outgoing)
New profiles: skip