是的,在Linux中,你可以使用Python命令行工具(即Python脚本)进行网络配置
- 获取网络接口信息:
import socket def get_interface_info(): hostname = socket.gethostname() local_ip = socket.gethostbyname(hostname) interfaces = socket.getaddrinfo(hostname, None) for interface in interfaces: print(interface) get_interface_info()
- 修改网络接口配置(以设置静态IP地址为例):
import os import subprocess def set_static_ip(interface, ip_address, subnet_mask, gateway, dns): config_file = f"/etc/network/interfaces" backup_file = f"{config_file}.bak" with open(config_file, "r") as file: lines = file.readlines() with open(backup_file, "w") as file: file.writelines(lines) new_config = f"""auto {interface} iface {interface} inet static address {ip_address} netmask {subnet_mask} gateway {gateway} dns-nameservers {dns} """ with open(config_file, "w") as file: file.write(new_config) subprocess.run(["ifdown", interface]) subprocess.run(["ifup", interface]) set_static_ip("eth0", "192.168.1.10", "255.255.255.0", "192.168.1.1", "8.8.8.8")
请注意,这些示例可能需要根据你的Linux发行版和网络配置进行调整。在执行这些脚本之前,请确保你了解它们的功能,并在测试环境中进行验证。