ufw がプログラムで実行されているかどうかを確認する方法

ufw がプログラムで実行されているかどうかを確認する方法

ほとんどのサービス (例: privoxy) では、サービス ステータスを取得する簡単な方法があります。 ps -C [servicename] 次に、終了コードを確認します($?)。0: サービスは実行中 1: 実行されていません これは、ufw では当てはまりません (サービスとして認識されない?)。sudo service ufw statusufw が実行中かどうかに関係なく、常に 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

関連情報