shell get ip address netmask dns

本文展示了如何使用Linux命令行工具ifconfig获取网络接口信息,包括MAC地址、IP地址及子网掩码等,并通过resolv.conf文件查看DNS服务器设置。
shell.albert@yantai:~/temporary> /sbin/ifconfig  enp8s0 | awk '/HWaddr/{print $5}'
C8:0A:A9:2F:BE:7B
shell.albert@yantai:~/temporary> /sbin/ifconfig  enp8s0
enp8s0    Link encap:Ethernet  HWaddr C8:0A:A9:2F:BE:7B  
          UP BROADCAST MULTICAST  MTU:1500  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:0 (0.0 b)  TX bytes:0 (0.0 b)
shell.albert@yantai:~/temporary> /sbin/ifconfig  wlp5s0 | awk '/inet addr:/{print $2}'|awk -F: '{print $2}'
192.168.3.106
shell.albert@yantai:~/temporary> /sbin/ifconfig wlp5s0
wlp5s0    Link encap:Ethernet  HWaddr 00:26:C7:24:28:F4  
          inet addr:192.168.3.106  Bcast:192.168.3.255  Mask:255.255.255.0
          inet6 addr: fe80::226:c7ff:fe24:28f4/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:40545 errors:0 dropped:0 overruns:0 frame:0
          TX packets:41291 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:21620676 (20.6 Mb)  TX bytes:12678240 (12.0 Mb)

shell.albert@yantai:~/temporary> /sbin/ifconfig wlp5s0 | awk -F : '/Mask/{print $4}'
255.255.255.0
shell.albert@yantai:~/temporary> /sbin/ifconfig wlp5s0
wlp5s0    Link encap:Ethernet  HWaddr 00:26:C7:24:28:F4  
          inet addr:192.168.3.106  Bcast:192.168.3.255  Mask:255.255.255.0
          inet6 addr: fe80::226:c7ff:fe24:28f4/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:40731 errors:0 dropped:0 overruns:0 frame:0
          TX packets:41485 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:21646315 (20.6 Mb)  TX bytes:12714410 (12.1 Mb)

shell.albert@yantai:~/temporary> awk '/^nameserver/{print $2}' /etc/resolv.conf
202.106.196.115
202.106.0.20
shell.albert@yantai:~/temporary> cat /etc/resolv.conf
### /etc/resolv.conf file autogenerated by netconfig!
#
# Before you change this file manually, consider to define the
# static DNS configuration using the following variables in the
# /etc/sysconfig/network/config file:
#     NETCONFIG_DNS_STATIC_SEARCHLIST
#     NETCONFIG_DNS_STATIC_SERVERS
#     NETCONFIG_DNS_FORWARDER
# or disable DNS configuration updates via netconfig by setting:
#     NETCONFIG_DNS_POLICY=''
#
# See also the netconfig(8) manual page and other documentation.
#
# Note: Manual change of this file disables netconfig too, but
# may get lost when this file contains comments or empty lines
# only, the netconfig settings are same with settings in this
# file and in case of a "netconfig update -f" call.
#
### Please remove (at least) this line when you modify the file!
search china
nameserver 202.106.196.115
nameserver 202.106.0.20
shell.albert@yantai:~/temporary>


下面这个程序有什么亮点 def get_dns_servers(): """获取系统配置的 DNS 服务器地址列表""" try: dns_servers = [] resolver = socket.getaddrinfo(socket.gethostname(), None) for family, _, _, _, sockaddr in resolver: if family == socket.AF_INET: # 只获取 IPv4 地址 dns_servers.append(sockaddr[0]) return list(set(dns_servers)) except Exception as e: return [f"无法获取 DNS: {str(e)}"] class Test(TestCase): def __init__(self): TestCase.__init__(self) self.logger = logging.getLogger(self.__class__.__name__) self.case_info = CaseInfo() self.case_info.name = 't1' self.case_info.id = '1' self.case_info.version = '202508171551' self.case_info.author = 'gongxing@tp-link.com.hk' self.case_info.runtime = '1min' self.case_info.testbeds = self.get_testbed_list() self.case_info.description = ''' ''' def add_task(self): self.register_case(self.t1, self.case_info) def t1(self): interfaces=netifaces.interfaces() default_gateway = netifaces.gateways().get('default', {}).get(netifaces.AF_INET, [None])[0] nic_id=1 dns_servers = get_dns_servers() for iface in interfaces: try: result = ResultInfo() addrs = netifaces.ifaddresses(iface).get(netifaces.AF_INET, [{}])[0] gateways = netifaces.gateways().get(netifaces.AF_INET, []) gateway = next((gw[0] for gw in gateways if gw[1] == iface), default_gateway) #获取该网卡IP ip = addrs.get('addr', '') self.logger.info(f'当前网卡是第{nic_id}张,网卡IP是{ip}\n') result.add_result(passfail=ResultInfo.PASS, actual_result=ip, expected_result='本机网卡IP不为空', test_comment=f'当前网卡是第{nic_id}张,正使用netifaces读取本网卡IP', item_id='step1') #获取该网卡Mask netmask = addrs.get('netmask', '') self.logger.info(f'当前网卡是第{nic_id}张,子网掩码是{netmask}\n') result.add_result(passfail=ResultInfo.PASS, actual_result=netmask, expected_result='本机网卡IP掩码不为空', test_comment=f'当前网卡是第{nic_id}张,正使用netifaces读取本网卡IP掩码', item_id='step2') #获取该网卡Gateway self.logger.info(f'当前网卡是第{nic_id}张,网关是{gateway}\n') result.add_result(passfail=ResultInfo.PASS, actual_result=gateway, expected_result='本机网卡网关不为空', test_comment=f'当前网卡是第{nic_id}张,正使用netifaces读取本网卡gateway', item_id='step3') # 输出系统DNS列表 self.logger.info(f'当前网卡是第{nic_id}张,DNS是{", ".join(dns_servers)}\n') result.add_result(passfail=ResultInfo.PASS,actual_result=", ".join(dns_servers), expected_result='系统 DNS 服务器不为空', test_comment='当前系统配置的 DNS 服务器地址',item_id='step4') #网卡ID自增 nic_id=nic_id+1 except Exception as e: result = ResultInfo() result.add_result(passfail=ResultInfo.FAIL, test_comment='Test fail: %s' % e) self.break_test('Test fail: %s' % e) return result
最新发布
08-20
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值