'route'에 해당되는 글 3건

  1. 2009.11.30 /proc/net/route 파싱
  2. 2009.08.06 busybox route 명령어
  3. 2009.06.09 ip 관련 정보 얻어내기 2
Linux2009. 11. 30. 16:53
본것중에 가장 간단하게 /proc/net/route를 파싱하는 소스인 듯하다.

# cat /proc/net/route
Iface   Destination     Gateway         Flags   RefCnt  Use     Metric  Mask            MTU     Window  IRTT
eth0    000AA8C0        00000000        0001    0       0       0       00FFFFFF        0       0       0
eth0    00000000        010AA8C0        0003    0       0       0       00000000        0       0       0

# route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.10.0    *               255.255.255.0   U     0      0        0 eth0
default         192.168.10.1    0.0.0.0         UG    0      0        0 eth0

아무튼 route로 출력하는 default가 UG 플래그로 default gateway이며,
파일에서는 Destination과 Mask가 0x00000000 인 녀석이 default gateway이다.

#include "stdio.h"
#include "stdlib.h"
#include "unistd.h"

#define IP_PRINT(a) \
    *((unsigned char *)(&a)), \
    *(((unsigned char *)(&a)) + 1), \
    *(((unsigned char *)(&a)) + 2), \
    *(((unsigned char *)(&a)) + 3)

int main()
{
    FILE* fp = fopen("/proc/net/route", "r");
    char buf[256];
    static char iface[256];
    unsigned int destination, gateway, flags, refcnt, use, metric, mask;
    int ret;

    if (fp == NULL)
        exit(-1);

    while (fgets(buf, 255, fp)) {
        if (!strncmp(buf, "Iface", 5))
            continue;

        ret = sscanf(buf, "%s\t%x\t%x\t%d\t%d\t%d\t%d\t%x",
                    iface, &destination, &gateway, &flags,
                    &refcnt, &use, &metric, &mask);

        if (ret < 8) {
            fprintf(stderr, "ERROR: line read error\n");
            continue;
        }

        if (destination != 0) {
            fprintf(stderr, "%s: gateway %u.%u.%u.%u, "
                    "destination %u.%u.%u.%u netmask %u.%u.%u.%u\n",
                    iface,
                    IP_PRINT(gateway),
                    IP_PRINT(destination),
                    IP_PRINT(mask));
            continue;
        }

        if (mask != 0) {
            fprintf(stderr, "%s: gateway %u.%u.%u.%u, default, "
                    "but have netmask %u.%u.%u.%u???\n",
                    iface,
                    IP_PRINT(gateway),
                    IP_PRINT(mask));
            continue;
        }

        fprintf(stderr, "%s: gateway %u.%u.%u.%u, default\n",
                iface,
                IP_PRINT(gateway));
    }
    fclose(fp);
}


[링크 : http://elenoa.tistory.com/72]

'Linux' 카테고리의 다른 글

lsof - list Open File  (0) 2009.12.07
filesystem - msdos, vfat, umsdos  (0) 2009.12.06
좀비 프로세스 생성하기(!)  (0) 2009.11.20
ps에서 [프로세스] 의 의미 - bracket process name in ps  (0) 2009.11.20
ps - Process Status  (2) 2009.11.20
Posted by 구차니
Linux2009. 8. 6. 17:22
# route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.10.0    *               255.255.255.0   U     0      0        0 eth0
default         192.168.10.1    0.0.0.0         UG    0      0        0 eth0

route 명령은 라우팅 테이블에 추가/삭제를 하는데 사용된다.
일단 눈에 들어 오는것은 Flags로 U / UG 라는 것이 있다.

       Flags  Possible flags include
              U (route is up)
              H (target is a host)
              G (use gateway)
              R (reinstate route for dynamic routing)
              D (dynamically installed by daemon or redirect)
              M (modified from routing daemon or redirect)
              A (installed by addrconf)
              C (cache entry)
              !  (reject route)

이러한 의미인데, UG라고 되어 있으면, route is up / use gateway라는 의미이다.
간단하게, 이 녀석이 게이트웨이라는 의미로 받아들이면 될 듯 하다.

아무튼,
ip route flush all로 모든 라우팅 테이블을 비우고 나서
route add default dev eth0를 하면

# route add default dev eth0
# route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
default         *               0.0.0.0         U     0      0        0 eth0

이렇게 Genmask 값도 다르고, Flags도 U로 체크가 된다.

아무튼 의도대로 UG가 찍히게 하려면,
조금은 거리가 멀어 보이는 ifconfig를 이용해야 한다.

udhcpc의 default.script 내용인데
#!/bin/sh
# Sample udhcpc renew script

RESOLV_CONF="/etc/resolv.conf"

[ -n "$broadcast" ] && BROADCAST="broadcast $broadcast"
[ -n "$subnet" ] && NETMASK="netmask $subnet"

/sbin/ifconfig $interface $ip $BROADCAST $NETMASK

if [ -n "$router" ]
then
        echo "deleting routers"
        while /sbin/route del default gw 0.0.0.0 dev $interface
        do :
        done

        metric=0
        for i in $router
        do
                /sbin/route add default gw $i dev $interface metric $((metric++))
        done
fi

이런식으로 라우팅 테이블을 추가하게 된다.



결론 : IP 관련 설정은 ifconfig - route 순으로 진행하면 된다.

----
2011.11.08 추가
여기서보면 flag 상태가 있는데 여기서 flag값의 의미는 다음과 같습니다.

U = Route is "UP"
H = Route is for a single host
G = Route requires a hop across a gateway

즉 다시 말씀드리자면 UP은 destination까지의 경로가 살아있다는 얘기입니다. 거의 대부분은 다 UP상태입니다. 그리고 H는 destination이 network 주소가 아니라 single host일 경우가 됩니다. 그리고 G는 destination까지의 경로가 router를 거친다는것을 의미합니다. 그리고 U만 있는경우는 destination이 network주소일 경우입니다.
 
[링크 : http://h30499.www3.hp.com/t5/HP-UX/route-add-및-delete-에-관한-자료/td-p/1165815?profile.language=ko ] 



Posted by 구차니
Linux2009. 6. 9. 10:43
MAC Address
$ /sbin/ifconfig eth0 | grep HWaddr | awk '{print $5}'

IP Address
$ /sbin/ifconfig eth0 | grep 'inet addr' | awk '{print $2}'

Broadcast Address
$ /sbin/ifconfig eth0 | grep 'inet addr' | awk '{print $3}'

SUBNET mask
$ /sbin/ifconfig eth0 | grep 'inet addr' | awk '{print $4}'

GATEWAY Address
$ netstat -rn | grep ^0.0.0.0 | awk '{printf $2}'

머.. 윈도우 비스므리하게 출력하기 위해서는 Broadcast Address는 별 의미를 가지지 않을테니
나머지 4가지면 충분할 듯!

awk
[참고: http://wiki.kldp.org/wiki.php/Awk]
[참고: http://kldp.org/HOWTO/html/Adv-Bash-Scr-HOWTO/awk.html]

sed
[참고: http://kldp.org/HOWTO/html/Adv-Bash-Scr-HOWTO/x12718.html]
[참고: http://stone.backrush.com/sunfaq/ljs007.html]

'Linux' 카테고리의 다른 글

ifconfig --help  (0) 2009.06.09
dhcp 작동중인지 확인하는 방법  (0) 2009.06.09
하드디스크 정보 얻어내기(model명)  (0) 2009.06.08
하드웨어 정보 받아오기  (0) 2009.06.08
리눅스에서 hex edit 하기  (0) 2009.05.28
Posted by 구차니