windows 使用python共享网络给另外一个网卡

# -*- coding: utf-8 -*-
import subprocess

def open_share(to_shared_adapter, from_shared_adapter):
    """
    打开以太网的网络共享
    :return: None
    """
    powershell_script = f"""
    # Register the HNetCfg library (once)
    # regsvr32 hnetcfg.dll

    # Create a NetSharingManager object
    $m = New-Object -ComObject HNetCfg.HNetShare

    # List connections
    $m.EnumEveryConnection |% {{ $m.NetConnectionProps.Invoke($_) }}

    # Find connection
    $c = $m.EnumEveryConnection |? {{ $m.NetConnectionProps.Invoke($_).Name -eq "{to_shared_adapter}" }}
    $c2 = $m.EnumEveryConnection |? {{ $m.NetConnectionProps.Invoke($_).Name -eq "{from_shared_adapter}" }}

    # Get sharing configuration
    $config = $m.INetSharingConfigurationForINetConnection.Invoke($c)
    $config2 = $m.INetSharingConfigurationForINetConnection.Invoke($c2)

    # See if sharing is enabled
    Write-Output $config.SharingEnabled
    Write-Output $config2.SharingEnabled

    # See the role of connection in sharing
    # 0 - public, 1 - private
    # Only meaningful if SharingEnabled is True

    Write-Output $config.SharingType
    Write-Output $config2.SharingType

    $config.EnableSharing(0)
    $config2.EnableSharing(1)
    """

    # 使用subprocess.Popen来执行PowerShell命令
    process = subprocess.Popen(
        ['powershell', '-Command', powershell_script],
        stdout=subprocess.PIPE,
        stderr=subprocess.PIPE,
        text=True  # 使用text=True可以直接处理字符串,而不是bytes
    )

    # 等待命令执行完成
    stdout, stderr = process.communicate()


def close_share(to_shared_adapter, from_shared_adapter):
    """
    打开以太网的网络共享
    :return: None
    """
    powershell_script = f"""
    # Register the HNetCfg library (once)
    # regsvr32 hnetcfg.dll

    # Create a NetSharingManager object
    $m = New-Object -ComObject HNetCfg.HNetShare

    # List connections
    $m.EnumEveryConnection |% {{ $m.NetConnectionProps.Invoke($_) }}

    # Find connection
    $c = $m.EnumEveryConnection |? {{ $m.NetConnectionProps.Invoke($_).Name -eq "{to_shared_adapter}" }}
    $c2 = $m.EnumEveryConnection |? {{ $m.NetConnectionProps.Invoke($_).Name -eq "{from_shared_adapter}" }}

    # Get sharing configuration
    $config = $m.INetSharingConfigurationForINetConnection.Invoke($c)
    $config2 = $m.INetSharingConfigurationForINetConnection.Invoke($c2)

    # Enable sharing (0 - public, 1 - private)
    $config.DisableSharing()
    $config2.DisableSharing()
    """

    # 使用subprocess.Popen来执行PowerShell命令
    process = subprocess.Popen(
        ['powershell', '-Command', powershell_script],
        stdout=subprocess.PIPE,
        stderr=subprocess.PIPE,
        text=True  # 使用text=True可以直接处理字符串,而不是bytes
    )

    # 等待命令执行完成
    stdout, stderr = process.communicate()

    # # 打印输出
    # print(stdout)
    #
    # # 如果有错误输出,打印错误信息
    # if stderr:
    #     print(stderr)

if __name__ == '__main__':
    # 共享与被共享的网络名
    to_shared_adapter = "以太网"
    from_shared_adapter = "以太网 7"
    # 开启共享
    open_share(to_shared_adapter, from_shared_adapter)
    close_share(to_shared_adapter, from_shared_adapter)

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值