15줄짜리 IP 알아내기 프로그램

segfault의 이미지

귀차니즘 버젼입니다... :)
하도 귀찮아서 그냥 popen으로.. 흐흐

#include <stdio.h>
int main(void)
{
  FILE *fp;
  char buffer[256], *p;
  int i;
  fp = popen("/sbin/ifconfig eth0", "r");
  fgets(buffer, 255, fp);
  fgets(buffer, 255, fp);
  p = buffer+20;
  for(i = 0; i < strlen(p); i++)
    if(p[i] == ' ') { p[i] = '\0'; break; }
  printf("%s\n", p);
  fclose(fp);
}

KDE 사용자라면 kdialog -msgbox `myip` 이렇게 하면 GUI로도 뜹니다 :)

Forums: 
익명 사용자의 이미지

import httplib

query = "/"
hostname = "checkip.dyndns.org"
agent = "ifconfig"

http = httplib.HTTP(hostname)

http.putrequest("GET", query)
http.putheader("User-Agent", agent)
http.putheader("Host", hostname)
http.putheader("Accept", "*/*")
http.endheaders()

errcode, errmsg, headers = http.getreply()

file = http.getfile()

print file.read()

파이썬으로 만든 귀차니즘 버전입니다. HTML 코드 파싱은 귀찮아서 생략.

맹고이의 이미지

앗, 윗글은 제가 쓴 겁니다. 이런 건 이름을 남겨야 제 맛... -_-;

맹고이의 이미지

아, 라인 수를 줄이는 게 목표인 것 같아서 다시 해봤습니다.

import urllib
print urllib.urlopen("http://checkip.dyndns.org").read()
markboy의 이미지

재미삼아서..

#!/bin/sh

/sbin/ifconfig | egrep -e "(Link|inet)"
markboy의 이미지

첫번째 interface의 ip만 뽑아낸다면 대충

#!/bin/sh

/sbin/ifconfig | grep "inet" | awk '{print $2}' | sed 's/^.*://g' | head --lines=1
mudori의 이미지

hostname -i

markboy의 이미지

python으로 다르게 하면

import socket

print socket.getaddrinfo(socket.gethostname(), None)[0][4][0]

덧) hostname -i 하면 /etc/hosts 파일에 적힌대로 나오지 않나요?

nonots의 이미지

[root@nalle ~]# ifconfig | grep "inet addr" | grep -v "127.0.0" | cut -d: -f2 | cut -d" " -f1
192.168.0.9


=== 건달의 경지를 꿈꾸며 ===

atie의 이미지

lynx -dump http://checkip.dyndns.org | grep "IP Address"

----
I paint objects as I think them, not as I see them.
atie's minipage

아빠곰의 이미지

:twisted: atie님 코드를 카피했습니다.
w3m -dump http://checkip.dyndns.org | grep "IP Address"

----
아발발다빠따반반나다발딸발발다빠따따맣밤밤따받따발발다따밝다발발다빠따따밤반다빠따다맣밥발
발다따밥다발발다따박다발발다빠따따밞밭밭다따다맣아희

byteme의 이미지

atie wrote:
lynx -dump http://checkip.dyndns.org | grep "IP Address"

lynx -dump http://checkip.dyndns.org |grep "IP Address" | awk {'print $4'}

댓글 달기

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