리눅스 역참조 특정부분 추출

조회수 519회
Oct 8 21:40:19 cent sshd[1478]: Failed password for user1 from 10.211.55.21 port 53655 ssh2

저기에서 IP만 추출해서 결과를

3 10.211.55.18

이런 식으로 앞에는 숫자가 나오고 뒤에 추출 내용이 나오게 하고 싶은데 계속 전체 행이 나오더라고요...

제가 작성한 코드는 아래와 같습니다.

sed -n "/$SED_FAIL from\(.*\)/\1/p" abc.log | sort | uniq -c | sort -nr

여기서 어떻게 바꿔야 저런 결과가 나오게 되는 건가요?? 수정 부탁드립니다...

참고로 $SED_FAIL 변수에는 "Failed password for user1"을 저장했습니다.

  • (•́ ✖ •̀)
    알 수 없는 사용자
  • 저런 결과가 무엇인가요? 정규식과 sed 문서를 보면 충분히 해결할 수 있을 듯 한데요? 정영훈 2020.10.10 22:47
  • 3 10.211.55.18 이거요. 앞에 unic -c를 써서 수를 세고 뒤에는 ip 주소만 추출해서 출력하는 건데 아무리 고쳐봐도 계속 안 나와서요. 알 수 없는 사용자 2020.10.10 22:58
  • Python Script로 짜시는건 어떨까요. dbwodlf3 2020.10.10 23:14
  • 레코드 형식의 데이터라면, awk 로 출력하는게 가장 간편할 것 같습니다. dbwodlf3 2020.10.10 23:15
  • 파이썬은 제가 못해서 안될 것 같습니다... 조금 더 생각해보고 짜봐야겠네요 감사합니다 알 수 없는 사용자 2020.10.10 23:27
  • grep -Po 'from \K([0-9]{1,3}\.){3}[0-9]{1,3}' a.log | sort | uniq -c 같이 grep 이 더 간결할 수 있습니다. 정영훈 2020.10.10 23:40
  • 네 grep을 사용해서 할 수는 있는데 grep을 사용하지 않고 저렇게 시도해보고 싶었습니다. 아무튼 감사합니다. 알 수 없는 사용자 2020.10.11 00:08

1 답변

  • 아래 참고하세요.

    allinux@kaggle:~/workspace/projects/shellscript/ex01$ cat a.log
    Oct 8 21:40:00 cent unix_chkpwd[1480]: password check failed for user (user1)
    Oct 8 21:40:00 cent sshd[1478]: pam_unix(sshd:auth): authentication failure; logname= uid=0 euid=0 tty=ssh ruser= rhost=10.221.55.2 user=user1
    Oct 8 21:40:02 cent sshd[1478]: Failed password for user1 from 10.211.55.2 port 53639 ssh2
    Oct 8 21:40:05 cent sshd[1479]: Connection closed by 10.211.55.2
    Oct 8 21:40:08 cent sshd[1478]: Failed password for user1 from 10.211.55.21 port 53655 ssh2
    Oct 8 21:40:12 cent sshd[1478]: Failed password for user1 from 10.211.55.18 port 53619 ssh2
    Oct 8 21:40:19 cent sshd[1478]: Failed password for user1 from 10.211.55.21 port 53655 ssh2
    Oct 8 21:40:32 cent sshd[1478]: Failed password for user1 from 10.211.55.18 port 53619 ssh2
    Oct 8 21:40:37 cent sshd[1478]: Failed password for user1 from 10.211.55.18 port 53619 ssh2
    Oct 8 21:40:46 cent sshd[1478]: Failed password for user1 from 10.211.55.21 port 53655 ssh2
    Oct 8 21:40:49 cent sshd[1479]: Connection closed by 10.211.55.21
    Oct 8 21:40:52 cent sshd[1478]: Failed password for user1 from 10.211.55.18 port 53619 ssh2
    Oct 8 21:40:55 cent sshd[1478]: Failed password for user1 from 10.211.55.18 port 53619 ssh2
    Oct 8 21:41:02 cent sshd[1479]: Connection closed by 10.211.55.18
    allinux@kaggle:~/workspace/projects/shellscript/ex01$ sed -nr 's/.*from ([^ ]+).*/\1/p' a.log | sort | uniq -c
          5 10.211.55.18
          1 10.211.55.2
          3 10.211.55.21
    

답변을 하려면 로그인이 필요합니다.

프로그래머스 커뮤니티는 개발자들을 위한 Q&A 서비스입니다. 로그인해야 답변을 작성하실 수 있습니다.

(ಠ_ಠ)
(ಠ‿ಠ)