쉘 스크립트를 C로 변환할때 ...

운형의 이미지

[ -n `iptables -L | greb DROP | awk '{print $2}'` ] && do something

조건문을 C로 바꾸려고 하는데..
iptables -L | greb DROP | awk '{print $2}' 이놈이 문제군요.
system("iptables -L | greb DROP | awk '{print $2}'");
해버리면 iptables -L | greb DROP | awk '{print $2}'의 리턴값을 확인할 수 없네요.

좋은 수가 없을까요..

eminency의 이미지

int r;
r = system("[ -n `iptables -L | greb DROP | awk '{print $2}'`]");

이렇게 해서 r값을 확인해도 될 거 같은데요..-_-

노루가 사냥꾼의 손에서 벗어나는 것 같이, 새가 그물치는 자의 손에서 벗어나는 것 같이 스스로 구원하라 -잠언 6:5

운형의 이미지

system 함수는 127(exec 실패시) 다른 에러시 -1 성공시 0리턴하는데요...

제가 알고 싶은건 DROP세팅이 되있다면 어떤 페킷에 대해 드랍되있는지 알고 싶은데...

eminency님 말슴처럼 하면... 시스템 에러 때 빼고는 전부 0이거든요...

-_-

하여간 답변 감사요..

Do you think that's the air you are breathing now?

eminency의 이미지

음.. 즉, 'iptables -L | ~~'의 내용도 필요하단 말씀이신가요..?

그럼 popen을 쓰시면 될 것 같습니다.
popen은 쉘 명령을 프로세스를 하나 열어 실행하고 결과를 파일 포인터로 되돌려줍니다. popen의 하는 일을 조금 더 정확히 말씀드린다면 프로세스를 포크해서 하나 열고 파이프를 연결해주는 것입니다.

일반 파일 읽듯이 그 내용을 읽으면 코드 내에서 결과 확인이 가능하지요.
자세한 건 man 페이지를 확인해보시고..

다음은 ls의 결과를 읽어서 출력하는 예제코드입니다.

#include<stdio.h>

main()
{
        FILE *in;
        char temp[100];

        in = popen("ls","r");

        while(fgets(temp,100,in) != NULL)
                printf("%s",temp);

}

이 정도면 되나요...? :)

노루가 사냥꾼의 손에서 벗어나는 것 같이, 새가 그물치는 자의 손에서 벗어나는 것 같이 스스로 구원하라 -잠언 6:5

saxboy의 이미지

아... 혹시 bash나 csh 스크립트 컴파일러는 없을까요?
누군가 만들었을것 같은 느낌이...

운형의 이미지

저런 popen이란 함수가 있었군요.

저도 라이브러리 많이 안다고 생각했는데...

감사합니다.

Do you think that's the air you are breathing now?

netj의 이미지

saxboy wrote:
아... 혹시 bash나 csh 스크립트 컴파일러는 없을까요?
누군가 만들었을것 같은 느낌이...

예전에 비슷한게 필요해서 찾아봤던 것들입니다:
http://www.comeaucomputing.com/faqs/ccshfaq.html
http://www.datsi.fi.upm.es/~frosal/frosal.html
어느 수준까지 컴파일이 되는지는 확인해보지 못했습니다.

그런데 결국 셸 스크립트를 컴파일해봐야 system이나 fork/exec/wait 범벅이 될 뿐이라는 결론을 내리고 컴파일 안하기로 했었죠. :roll:

댓글 달기

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
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.