#!/usr/bin/env python3 """ 检查Windows节点网络连通性的脚本 """ import socket import subprocess import time def test_ping(): """测试ping命令""" print("=== 测试ping命令 ===") try: result = subprocess.run( ["ping", "-c", "3", "192.168.2.33"], capture_output=True, text=True, timeout=30 ) print("输出:") print(result.stdout) if result.returncode == 0: print("\n✅ Ping测试成功") else: print("\n❌ Ping测试失败") except Exception as e: print(f"❌ 错误: {e}") print() def test_ssh(): """测试SSH连接""" print("=== 测试SSH连接 ===") try: result = subprocess.run( ["ssh", "-o", "ConnectTimeout=5", "administrator@192.168.2.33", "echo 'Connected'"], capture_output=True, text=True, timeout=10 ) print("输出:") print(result.stdout) if result.returncode == 0: print("\n✅ SSH连接成功") else: print(f"\n❌ SSH连接失败 (代码: {result.returncode})") print(f"错误: {result.stderr}") except Exception as e: print(f"❌ 错误: {e}") print() def test_arp(): """测试ARP表""" print("=== 测试ARP表 ===") try: result = subprocess.run( ["arp", "-a"], capture_output=True, text=True, timeout=10 ) print("ARP表中是否包含192.168.2.33:") if "192.168.2.33" in result.stdout: print("✅ 找到192.168.2.33的ARP记录") # 提取该IP的MAC地址 lines = result.stdout.split("\n") for line in lines: if "192.168.2.33" in line: mac = line.split()[3] print(f"MAC地址: {mac}") else: print("❌ 未找到192.168.2.33的ARP记录") except Exception as e: print(f"❌ 错误: {e}") print() def test_port_scan(): """测试端口扫描""" print("=== 测试端口扫描 ===") ports_to_test = [22, 3389, 5000, 8080] for port in ports_to_test: try: sock = socket.create_connection(("192.168.2.33", port), timeout=2) print(f"✅ 端口 {port}: 开放") sock.close() except Exception as e: print(f"❌ 端口 {port}: 关闭 ({e})") print() def check_network_interface(): """检查网络接口""" print("=== 检查网络接口 ===") try: result = subprocess.run( ["ifconfig", "-a"], capture_output=True, text=True, timeout=10 ) print("网络接口:") print(result.stdout) except Exception as e: print(f"❌ 错误: {e}") print() if __name__ == "__main__": print("开始检查Windows节点网络连通性...") print("=" * 50) test_ping() test_ssh() test_arp() test_port_scan() check_network_interface() print("=" * 50) print("检查完成")