쉘 명령중 질문이 있어요

envy100의 이미지

로그에서 필요한 부분은 찾으면 메일로 알려주는걸 하려고 하는데요
tail -f /var/log/syslog | grep string
이렇게 하면 string을 찾을거 아니에요
그럼 그내용을 메일로 보내고싶은데 어떻게 해야하는지 좀 알려주세요

tail -f /var/log/syslog | grep string|mail 주소 이렇게 하면 될까요??

이런거 잘하고 싶으면 뭘 봐야 하나요??

정말 고급 유닉서가 되기는 힘들군요 :(

무한포옹의 이미지

...| mail -s 제목 받는이주소 [-c 참조자주소 -b 숨은 참조자주소]

man mail 이죠 머.. (궁지렁)

-------------------------------
== warning 대부분 틀린 얘기입니다 warning ===

envy100의 이미지

grep 으로 나온 결과를 메일로 받고 싶다고요
그런데 어떻게 해야하는지 그게 궁금해서요

도전하라

송지석의 이미지

답변하신 대로 하면 보내지는데요..

tail /var/log/messages |grep ssh | mail -s test root@localhost

예를 들면 이렇게요..

그러나..

tail -f 하면.. 음... 과연 될런지... tail에서 계속 잡고 있으니 그냥 쉘에서는 안될 듯 싶습니다만..

프로그램을 짜셔서 파이프로 받아오시는 것은 어떨까요?
http://bbs.kldp.org/viewtopic.php?t=20116&highlight=%C6%C4%C0%CC%C7%C1
man popen

while루프 안에서 읽을 때 마다 메일 보내는 쉘 스크립트를 실행시키거나 하면 될 듯...

송지석의 이미지

한번 짜봤는데요.. (윽 난 일 안하고 뭐하냐)

tail -f /var/log/messages |grep ssh로는파이프에 제대로 전달되지 않아서 그냥 strstr로 검색했습니다.

아래 소스대로 하면 검색되는 한줄마다 메일을 보내게 됩니다. 그리고 루트 권한으로만 실행 할 수 있네요. messages를 읽을 수 있는 권한이 root한테 있어서..

버퍼링을 하겠다면 좀 더 손보셔야 할 듯..

#include <stdio.h>
#include <string.h>

#define MSG_MAX 1024
#define PATTERN_MAX 1024
int main(int argc, char *argv[])
{
    FILE *fp;
    FILE *fp2;
    char read_str[MSG_MAX];
    char pattern[PATTERN_MAX];

    if (argc <2) {
        printf("Please specify interested string\n");
        printf("usage : mailsend interested_string\n");
        return 1;
    }
    strncpy(pattern, (const char*) argv[1], PATTERN_MAX -1);
    printf("pattern=%s", pattern);

    fp = popen("tail -f /var/log/messages", "r");

    if (0==fp) {
        perror("popen");
        return 1;
    }
    while (fgets(read_str, sizeof read_str -1, fp)) {
        if (strstr(read_str, pattern)) {
            fp2 = popen("mail -s subject_haha root@localhost", "w");
            if (0==fp2) {
                perror ("popen fp2");
                continue;
            }
            if (EOF == fputs(read_str, fp2)) {
                perror ("fputs");
            }
            pclose (fp2);
            //printf("%s\n", read_str);

        }
    }

    pclose(fp);
    return 0;
}

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.