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

관련 정보