python 禁用网卡_如何以编程方式启用/禁用网络接口?(Windows XP)

本文介绍了一个实用的VBS脚本,该脚本能够实现在某些机器上通过NETSH无法实现的网络连接启用与禁用功能。脚本通过Shell.Application对象操作控制面板和网络连接文件夹来定位特定的本地区域连接,并调用相应的启用或禁用动词。

我在网上找到了这个.VBS脚本。它有一个很酷的优点,那就是在我无法让NETSH为此工作的机器上工作。Const ssfCONTROLS = 3

sConnectionName = "Local Area Connection"

sEnableVerb = "En&able"

sDisableVerb = "Disa&ble"

set shellApp = createobject("shell.application")

set oControlPanel = shellApp.Namespace(ssfCONTROLS)

set oNetConnections = nothing

for each folderitem in oControlPanel.items

if folderitem.name = "Network Connections" then

set oNetConnections = folderitem.getfolder: exit for

end if

next

if oNetConnections is nothing then

msgbox "Couldn't find 'Network Connections' folder"

wscript.quit

end if

set oLanConnection = nothing

for each folderitem in oNetConnections.items

if lcase(folderitem.name) = lcase(sConnectionName) then

set oLanConnection = folderitem: exit for

end if

next

if oLanConnection is nothing then

msgbox "Couldn't find '" & sConnectionName & "' item"

wscript.quit

end if

bEnabled = true

set oEnableVerb = nothing

set oDisableVerb = nothing

s = "Verbs: " & vbcrlf

for each verb in oLanConnection.verbs

s = s & vbcrlf & verb.name

if verb.name = sEnableVerb then

set oEnableVerb = verb

bEnabled = false

end if

if verb.name = sDisableVerb then

set oDisableVerb = verb

end if

next

'debugging displays left just in case...

'

'msgbox s ': wscript.quit

'msgbox "Enabled: " & bEnabled ': wscript.quit

'not sure why, but invokeverb always seemed to work

'for enable but not disable.

'

'saving a reference to the appropriate verb object

'and calling the DoIt method always seems to work.

'

if bEnabled then

' oLanConnection.invokeverb sDisableVerb

oDisableVerb.DoIt

else

' oLanConnection.invokeverb sEnableVerb

oEnableVerb.DoIt

end if

'adjust the sleep duration below as needed...

'

'if you let the oLanConnection go out of scope

'and be destroyed too soon, the action of the verb

'may not take...

'

wscript.sleep 1000

我通过抓包发现没有循环arp请求,请帮我看看怎么回事,并修改: // ARP条目结构 typedef struct arp_entry { struct in_addr ip; // IP地址 struct ether_addr mac; // MAC地址 time_t last_update; // 最后更新时间戳 struct arp_entry *next; // 链表指针 } arp_entry_t; // 扫描器配置 typedef struct arp_scanner { int enabled; // 功能开关 (1=启用, 0=禁用) time_t scan_cycle; // 扫描周期(秒) time_t entry_ttl; // 条目有效期(秒) int packet_interval; // 发包间隔(毫秒) struct in_addr ip_start;// 起始IP struct in_addr ip_end; // 结束IP arp_entry_t *arp_table; // ARP表头指针 pthread_mutex_t lock; // 线程安全锁 } arp_scanner_t; void build_arp_request(struct ether_header *ehdr, struct ether_arp *arp_req, struct in_addr target_ip, struct ether_addr local_mac) { // 以太网帧头 memset(ehdr->ether_dhost, 0xFF, ETHER_ADDR_LEN); // 广播地址 memcpy(arp_req->arp_sha, &local_mac, ETHER_ADDR_LEN); ehdr->ether_type = htons(ETHERTYPE_ARP); // ARP请求体 arp_req->arp_hrd = htons(ARPHRD_ETHER); // 硬件类型 arp_req->arp_pro = htons(ETHERTYPE_IP); // 协议类型 arp_req->arp_hln = ETHER_ADDR_LEN; // 硬件地址长度 arp_req->arp_pln = sizeof(in_addr_t); // 协议地址长度 arp_req->arp_op = htons(ARPOP_REQUEST); // 操作码=请求 memcpy(arp_req->arp_sha, &local_mac, ETHER_ADDR_LEN); memset(arp_req->arp_tha, 0x00, ETHER_ADDR_LEN); // 目标MAC初始为空 memcpy(arp_req->arp_spa, &scanner.ip_start, sizeof(in_addr_t)); // 发送方IP memcpy(arp_req->arp_tpa, &target_ip, sizeof(in_addr_t)); // 目标IP } // 扫描线程函数 void *scan_thread(void *arg) { struct ether_addr local_mac; if (get_local_mac("ens33", &local_mac) < 0) { // 替换为实际网卡名 perror("获取MAC失败"); return NULL; } // 创建原始套接字 int sockfd = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ARP)); if (sockfd < 0) { perror("socket创建失败"); return NULL; } while (scanner.enabled) { struct in_addr current_ip = scanner.ip_start; // 遍历IP范围 while (current_ip.s_addr <= scanner.ip_end.s_addr) { char packet[sizeof(struct ether_header) + sizeof(struct ether_arp)]; struct ether_header *ehdr = (struct ether_header *)packet; struct ether_arp *arp_req = (struct ether_arp *)(packet + sizeof(struct ether_header)); build_arp_request(ehdr, arp_req, current_ip, local_mac); // 发送ARP请求 if (sendto(sockfd, packet, sizeof(packet), 0, NULL, 0) < 0) { } // IP递增 current_ip.s_addr = htonl(ntohl(current_ip.s_addr) + 1); usleep(scanner.packet_interval * 1); // 发包间隔 } sleep(scanner.scan_cycle); // 等待扫描周期 } close(sockfd); return NULL; } // ARP响应处理 void handle_arp_reply(unsigned char *buffer) { struct ether_arp *arp_res = (struct ether_arp *)(buffer + sizeof(struct ether_header)); // 只处理ARP响应 if (ntohs(arp_res->arp_op) != ARPOP_REPLY) return; struct in_addr src_ip; memcpy(&src_ip, arp_res->arp_spa, sizeof(src_ip)); struct ether_addr src_mac; memcpy(&src_mac, arp_res->arp_sha, ETHER_ADDR_LEN); // 检查IP是否在扫描范围内 if (src_ip.s_addr < scanner.ip_start.s_addr || src_ip.s_addr > scanner.ip_end.s_addr) return; // 更新ARP表 arp_entry_t *entry = find_arp_entry(src_ip); if (entry) { entry->mac = src_mac; entry->last_update = time(NULL); } else { add_arp_entry(src_ip, src_mac); } } // 监听线程函数 void *listen_thread(void *arg) { int sockfd = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ARP)); if (sockfd < 0) { perror("监听socket失败"); return NULL; } unsigned char buffer[ETH_FRAME_LEN]; while (scanner.enabled) { ssize_t len = recvfrom(sockfd, buffer, sizeof(buffer), 0, NULL, NULL); if (len < (ssize_t)(sizeof(struct ether_header) + sizeof(struct ether_arp))) { continue; } handle_arp_reply(buffer); } close(sockfd); return NULL; }
08-27
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值