从/ proc找到网络地址

时间:2015-07-01 10:09:28

标签: linux networking tcp ip proc

因为每个Linux系统管理员都知道我们可以获得的Linux机器的IP地址

ifconfig -a|grep inet | awk -F":" '{print $2}' | head -1 | awk '{print $1}'

ip addr

或者只查看ifcfg文件

但如何从 / proc / net / tcp 文件中了解IP?

如何从tcp中了解IP地址?

  cat  /proc/net/tcp
     sl  local_address rem_address   st tx_queue rx_queue tr tm->when retrnsmt   uid  timeout inode                                                     
     0: 0100007F:F780 00000000:0000 0A 00000000:00000000 00:00000000 00000000 10501        0 17506 1 efd8e040 3000 0 0 2 -1                            
     1: 0100007F:F781 00000000:0000 0A 00000000:00000000 00:00000000 00000000 10501        0 17508 1 efd8e4c0 3000 0 0 2 -1                            
     2: 00000000:5281 00000000:0000 0A 00000000:00000000 00:00000000 00000000 10501        0 17195 1 efd8f6c0 3000 0 0 2 -1                            
     3: 00000000:02A1 00000000:0000 0A 00000000:00000000 00:00000000 00000000     0        0 8255 1 f69796c0 3000 0 0 2 -1                             
     4: 0100007F:F782 00000000:0000 0A 00000000:00000000 00:00000000 00000000 10501        0 17510 1 f4a6fb40 3000 0 0 2 -1                            
     5: 0100007F:F783 00000000:0000 0A 00000000:00000000 00:00000000 00000000 10501        0 17512 1 f4a6f6c0 3000 0 0 2 -1                            
     6: 79176A0A:13C4 00000000:0000 0A 00000000:00000000 00:00000000 00000000 14201        0 18908 1 f06864c0 3000 0 0 2 -1                            
     7: 0100007F:F784 00000000:0000 0A 00000000:00000000 00:00000000 00000000 10501        0 17514 1 f4a6f240 3000 0 0 2 -1                            
     8: 00000000:D804 00000000:0000 0A 00000000:00000000 00:00000000 00000000 23200        0 12839 1 f64d2040 3000 0 0 2 -1                            
     9: 0100007F:F785 00000000:0000 0A 00000000:00000000 00:00000000 00000000 10501        0 17516 1 f4a6edc0 3000 0 0 2 -1                            
     10: 00000000:D805 00000000:0000 0A 00000000:00000000 00:00000000 00000000 23200        0 13004 1 f6773b40 3000 0 0 2 -1                            
     11: 00000000:1E25 00000000:0000 0A 00000000:00000000 00:00000000 00000000     0        0 9568 1 f760d240 3000 0 0 2 -1                             
     12: 0100007F:F786 00000000:0000 0A 00000000:00000000 00:00000000 00000000 10501        0 17518 1 f4a6e940 3000 0 0 2 -1                            
     13: 0100007F:D806 00000000:0000 0A 00000000:00000000 00:00000000 00000000 23200        0 13121 1 f6773240 3000 0 0 2 -1                            
     14: 0100007F:F787 00000000:0000 0A 00000000:00000000 00:00000000 00000000 10501        0 17520 1 f4a6e4c0 3000 0 0 2 -1                            
     15: 0100007F:00C7 00000000:0000 0A 00000000:00000000 00:00000000 00000000     0        0 8921 1 f69784c0 3000 0 0 2 -1                             
     16: 0100007F:F788 00000000:0000 0A 00000000:00000000 00:00000000 00000000 10501        0 17522 1 f4a6e040 3000 0 0 2 -1                            
     17: 0100007F:F789 00000000:0000 0A 00000000:00000000 00:00000000 00000000 10501        0 17524 1 f1ec1b40 3000 0 0 2 -1                            
     18: 00000000:1389 00000000:0000 0A 00000000:00000000 00:00000000 00000000 23200        0 13090 1 f64d2940 3000 0 0 2 -1                            
     19: 0100007F:F78A 00000000:0000 0A 00000000:00000000 00:00000000 00000000 10501        0 17526 1 f1ec16c0 3000 0 0 2 -1                            
     20: 00000000:1A0A 00000000:0000 0A 00000000:00000000 00:00000000 00000000     0        0 9026 1 f6978040 3000 0 0 2 -1                             
     21: 0100007F:F78B 00000000:0000 0A 00000000:00000000 00:00000000 00000000 10501        0 17528 1 f1ec1240 3000 0 0 2 -1                            
     22: 0100007F:F78C 00000000:0000 0A 00000000:00000000 00:00000000 00000000 10501        0 17530 1 f1ec0dc0 3000 0 0 2 -1                            
     23: 0100007F:F78D 00000000:0000 0A 00000000:00000000 00:00000000 00000000 10501        0 17532 1 f1ec0940 3000 0 0 2 -1                            
     24: 00000000:060E 00000000:0000 0A 00000000:00000000 00:00000000 00000000 14201        0 18877 1 f0686dc0 3000 0 0 2 -1                            
     25: 0100007F:F78E 00000000:0000 0A 00000000:00000000 00:00000000 00000000 10501        0 17534 1 f1ec04c0 3000 0 0 2 -1                            
     26: 00000000:006F 00000000:0000 0A 00000000:00000000 00:00000000 00000000     0        0 8130 1 f6979b40 3000 0 0 2 -1                             
     27: 0100007F:0610 00000000:0000 0A 00000000:00000000 00:00000000 00000000 14201        0 18909 1 f0687240 3000 0 0 2 -1                            
     28: 0100007F:D994 00000000:0000 0A 00000000:00000000 00:00000000 00000000    48        0 14482 1 f64d2dc0 3000 0 0 2 -1                            
     29: 0100007F:0035 00000000:0000 0A 00000000:00000000 00:00000000 00000000    25        0 7961 1 f760db40 3000 0 0 2 -1                             
     30: 00000000:0016 00000000:0000 0A 00000000:00000000 00:00000000 00000000     0        0 8854 1 f6978dc0 3000 0 0 2 -1                             
     31: 00000000:F297 00000000:0000 0A 00000000:00000000 00:00000000 00000000 10101        0 9618 1 f7bd8040 3000 0 0 2 -1                             
     32: 0100007F:03B9 00000000:0000 0A 00000000:00000000 00:00000000 00000000    25        0 7962 1 f760d6c0 3000 0 0 2 -1                             
     33: 0100007F:F77B 00000000:0000 0A 00000000:00000000 00:00000000 00000000 10501        0 17496 1 f0510040 3000 0 0 2 -1                            
     34: 0100007F:F77C 00000000:0000 0A 00000000:00000000 00:00000000 00000000 10501        0 17498 1 f05104c0 3000 0 0 2 -1                            
     35: 0100007F:F77D 00000000:0000 0A 00000000:00000000 00:00000000 00000000 10501        0 17500 1 f0511b40 3000 0 0 2 -1                            
     36: 0100007F:F77E 00000000:0000 0A 00000000:00000000 00:00000000 00000000 10501        0 17502 1 f05116c0 3000 0 0 2 -1                            
     37: 0100007F:F77F 00000000:0000 0A 00000000:00000000 00:00000000 00000000 10501        0 17504 1 f0511240 3000 0 0 2 -1                            
     38: 00000000:D8FF 00000000:0000 0A 00000000:00000000 00:00000000 00000000 23200        0 13023 1 f67736c0 3000 0 0 2 -1                            
     39: 0100007F:041F 00000000:0000 0A 00000000:00000000 00:00000000 00000000     0        0 8920 1 f6978940 3000 0 0 2 -1                             
     40: 79176A0A:0016 7863770A:8C1E 01 00000000:00000000 02:0000473D 00000000     0        0 19488326 2 f363f240 202 40 14 3 100                       
     41: 0100007F:952C 0100007F:060E 06 00000000:00000000 03:00000C08 00000000     0        0 0 2 e96deec0                                              
     42: 0100007F:952F 0100007F:060E 06 00000000:00000000 03:000013D9 00000000     0        0 0 2 e96dee40                                              
     43: 0100007F:952E 0100007F:060E 06 00000000:00000000 03:00000FF0 00000000     0        0 0 2 e96de8c0                                              
     44: 0100007F:952B 0100007F:060E 06 00000000:00000000 03:00000820 00000000     0        0 0 2 e96def40                                              
     45: 0100007F:952A 0100007F:060E 06 00000000:00000000 03:00000438 00000000     0        0 0 2 e96deb40                                              
     46: 0100007F:D6D4 0100007F:041F 01 00000000:00000000 00:00000000 00000000 10101        0 9610 1 f7bd8940 202 40 30 2 100                           
     47: 0100007F:D6D1 0100007F:041F 01 00000000:00000000 00:00000000 00000000 10101        0 9594 1 f64d3b40 201 40 28 2 100                           
     48: 0100007F:D6D0 0100007F:041F 01 00000000:00000000 00:00000000 00000000 10101        0 9592 1 f760c940 208 40 32 2 100                           
     49: 0100007F:D6D3 0100007F:041F 01 00000000:00000000 00:00000000 00000000 10101        0 9597 1 f7bd9240 201 40 24 2 100                           
     50: 0100007F:D6D2 0100007F:041F 01 00000000:00000000 00:00000000 00000000 10101        0 9596 1 f7bd9b40 201 40 26 2 100                           
     51: 0100007F:D6E5 0100007F:041F 01 00000000:00000000 00:00000000 00000000 10501        0 15008 1 f67724c0 201 40 30 2 100                          
     52: 0100007F:D6E4 0100007F:041F 01 00000000:00000000 00:00000000 00000000 10501        0 14626 1 f64d3240 201 40 30 2 100                          
     53: 0100007F:D6E7 0100007F:041F 01 00000000:00000000 00:00000000 00000000 10501        0 16296 1 f0510dc0 201 40 30 2 100                          
     54: 0100007F:D6F2 0100007F:041F 01 00000000:00000000 00:00000000 00000000 14201        0 18883 1 f0686940 201 40 30 2 100                          
     55: 0100007F:041F 0100007F:D6D4 01 00000000:00000000 00:00000000 00000000     0        0 9611 1 f7bd84c0 201 40 31 2 100                           
     56: 0100007F:041F 0100007F:D6D0 01 00000000:00000000 00:00000000 00000000     0        0 9593 1 f760c040 211 65 33 2 100                           
      57: 0100007F:041F 0100007F:D6D1 01 00000000:00000000 00:00000000 00000000     0        0 9595 1 f64d36c0 212 46 29 2 100                           
      58: 0100007F:041F 0100007F:D6D2 01 00000000:00000000 00:00000000 00000000     0        0 9599 1 f7bd96c0 205 40 27 2 100                           
      59: 0100007F:041F 0100007F:D6D3 01 00000000:00000000 00:00000000 00000000     0        0 9600 1 f7bd8dc0 205 40 25 2 100                           
      60: 0100007F:DB38 0100007F:D804 06 00000000:00000000 03:00000C44 00000000     0        0 0 2 e96de9c0                                              
      61: 0100007F:041F 0100007F:D6E4 01 00000000:00000000 00:00000000 00000000     0        0 14627 1 f6772040 201 40 31 2 100                          
      62: 0100007F:041F 0100007F:D6E5 01 00000000:00000000 00:00000000 00000000     0        0 15009 1 f6772940 201 40 31 2 100                          
      63: 0100007F:041F 0100007F:D6E7 01 00000000:00000000 00:00000000 00000000     0        0 16301 1 f0510940 201 40 31 2 100                          
      64: 79176A0A:0016 58326A0A:82EB 01 00001ACC:00000000 01:00000017 00000000     0        0 19529421 4 f6979240 240 62 3 4 100                        
      65: 0100007F:041F 0100007F:D6F2 01 00000000:00000000 00:00000000 00000000     0        0 18884 1 f0687b40 201 40 31 2 100          

2 个答案:

答案 0 :(得分:1)

每行以十六进制形式的IPv4地址和端口号开头,低字节代表地址的第一部分:

0100007F:0016 == 01.00.00.7F:0016 == 127.0.0.1:22
79176A0A:13C4 == 79.17.6A.0A:13C4 == 10.106.23.121:5060

Reference

  

套接字的本地IP地址和端口号。 IP地址显示为little-endian四字节十六进制数;也就是说,首先列出最低有效字节,因此您需要反转字节的顺序以将其转换为IP地址。端口号是一个简单的双字节十六进制数。

答案 1 :(得分:0)

这是一个从/ proc / net / tcp转换为ip:port值的python版本。您可以扩展和添加您可能感兴趣的任何其他信息

cv@thunder:~/dev/py$ cat parsetcp.py 
#!/usr/bin/python 
tcpconn = open('/proc/net/tcp','r').readlines()

def hexip2str(hexip):
  return '.'.join([ str(int('0x'+ hexip[j:j+2],16)) for j in range(6,-2,-2)])

for conn in tcpconn:
  str_conn = [ x.strip() for x in conn.replace(':',' ').split() ]
  if str_conn[0].isalpha():
    continue
  print hexip2str(str_conn[1]) + ':' + str(int(str_conn[2],16)),' => ',hexip2str(str_conn[3]) + ':' + str(int(str_conn[4],16))