OS(운영체제)/리눅스 명령어
grep 명령어에서 AND, OR, NOT 조건 사용하기
hippo 데브옵스
2022. 7. 3. 13:42
- grep 명령어는 특정 파일에서 지정한 문자열이나 정규표현식을 포함한 행을 출력 → 들어오는 입력에 대해 주어진 패턴을 포함하는 줄들을 출력
- grep 명령어는 tail이나 ls, find 등 다양한 명령어와 조합하여 응용 가능
AND 조건 사용법
- 패턴들이 모두 포함된 줄들을 출력
1. grep 명령어에 AND를 사용하는 첫번째 방법 → grep을 여러번 사용
- Pipe( | )를 이용해 여러번 사용
$ iptables -L | grep -i 'DROP' | grep -i 'tcp dpt:snapenetio' DROP tcp -- anywhere anywhere tcp dpt:snapenetio
2. grep 명령어에 AND를 사용하는 두번째 방법 → grep에 -E옵션을 사용
- DROP과 tcp dpt:snapenetio이 모두 포함된 줄을 출력 → 순서에 상관 없이 두 패턴이 동시에 포함된 줄들을 출력
- .*을 사용하여 AND 사용
# .*을 사용하여 AND를 표시 $ iptables -L | grep -iE 'DROP.*tcp dpt:snapenetio' DROP tcp -- anywhere anywhere tcp dpt:snapenetio
OR 조건 사용법
- 패턴들 중 하나라도 포함하고 있는 줄들을 출력
1. grep 명령어에 AND를 사용하는 첫번째 방법 → grep에 -e 옵션을 사용
- DROP 또는 tcp dpt:snapenetio 중 하나라도 포함된 줄을 출력 → 순서에 상관 없이 두 패턴 중 하나만 있어도 출력
$ iptables -L | grep -e 'DROP' | grep -e 'tcp dpt:snapenetio' ACCEPT tcp -- 8.8.8.8 anywhere tcp dpt:snapenetio ACCEPT tcp -- 1.1.1.1 anywhere tcp dpt:snapenetio DROP tcp -- anywhere anywhere tcp dpt:snapenetio DROP tcp -- anywhere anywhere tcp dpt:rxmon DROP tcp -- anywhere anywhere tcp dpt:fmtp DROP tcp -- anywhere anywhere tcp dpt:radan-http DROP tcp -- anywhere anywhere tcp dpt:jetdirect DROP tcp -- anywhere anywhere tcp dpt:websm DROP all -- anywhere anywhere /* kubernetes firewall for dropping marked packets */ mark match 0x8000/0x8000
2. grep 명령어에 AND를 사용하는 두번째 방법 → grep에 -E옵션을 사용
- DROP 또는 tcp dpt:snapenetio 중 하나라도 포함된 줄을 출력 → 순서에 상관 없이 두 패턴 중 하나만 있어도 출력
- Pipe( | )를 이용해 여러번 사용
# 파이프라인( | )을 이용하여 OR 사용 $ iptables -L | grep -E 'DROP|tcp dpt:snapenetio' ACCEPT tcp -- 8.8.8.8 anywhere tcp dpt:snapenetio ACCEPT tcp -- 1.1.1.1 anywhere tcp dpt:snapenetio DROP tcp -- anywhere anywhere tcp dpt:snapenetio DROP tcp -- anywhere anywhere tcp dpt:rxmon DROP tcp -- anywhere anywhere tcp dpt:fmtp DROP tcp -- anywhere anywhere tcp dpt:radan-http DROP tcp -- anywhere anywhere tcp dpt:jetdirect DROP tcp -- anywhere anywhere tcp dpt:websm DROP all -- anywhere anywhere /* kubernetes firewall for dropping marked packets */ mark match 0x8000/0x8000
NOT 조건 사용법 → grep 명령어에 -v 옵션을 사용
ACCEPT가 들어있는 줄은 모두 제거
특정 패턴이 포함되지 않은 줄들을 출력
$ iptables -L | grep -vi ACCEPT target prot opt source destination KUBE-EXTERNAL-SERVICES all -- anywhere anywhere ctstate NEW /* kubernetes externally-visible service portals */ KUBE-FIREWALL all -- anywhere anywhere DROP tcp -- anywhere anywhere tcp dpt:snapenetio DROP tcp -- anywhere anywhere tcp dpt:rxmon DROP tcp -- anywhere anywhere tcp dpt:fmtp DROP tcp -- anywhere anywhere tcp dpt:radan-http DROP tcp -- anywhere anywhere tcp dpt:jetdirect DROP tcp -- anywhere anywhere tcp dpt:websm target prot opt source destination KUBE-FORWARD all -- anywhere anywhere /* kubernetes forwarding rules */ DOCKER-ISOLATION all -- anywhere anywhere DOCKER all -- anywhere anywhere target prot opt source destination KUBE-SERVICES all -- anywhere anywhere ctstate NEW /* kubernetes service portals */ KUBE-FIREWALL all -- anywhere anywhere Chain DOCKER (1 references) target prot opt source destination Chain DOCKER-ISOLATION (1 references) target prot opt source destination RETURN all -- anywhere anywhere Chain KUBE-EXTERNAL-SERVICES (1 references) target prot opt source destination Chain KUBE-FIREWALL (2 references) target prot opt source destination DROP all -- anywhere anywhere /* kubernetes firewall for dropping marked packets */ mark match 0x8000/0x8000 Chain KUBE-FORWARD (1 references) target prot opt source destination Chain KUBE-SERVICES (1 references) target prot opt source destination