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)

在这里插入图片描述