找回密码
 注册
搜索
[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
查看: 20097|回复: 3

[网络连接] 请问批处理能实现共享某一网卡吗,或共享某一连接

[复制链接]
发表于 2013-11-7 15:18:58 | 显示全部楼层 |阅读模式
比如我们店本地连接右键属性在点共享在选择要共享到的另一网卡 批处理能实现这一过程吗
发表于 2013-11-7 22:33:24 | 显示全部楼层
  1. OPTION EXPLICIT
  2. DIM ICSSC_DEFAULT, CONNECTION_PUBLIC, CONNECTION_PRIVATE, CONNECTION_ALL
  3. DIM NetSharingManager
  4. DIM PublicConnection, PrivateConnection
  5. DIM EveryConnectionCollection

  6. DIM objArgs
  7. DIM priv_con, publ_con
  8. dim switch

  9. ICSSC_DEFAULT         = 0
  10. CONNECTION_PUBLIC     = 0
  11. CONNECTION_PRIVATE    = 1
  12. CONNECTION_ALL        = 2

  13. Main()

  14. sub Main( )
  15.     Set objArgs = WScript.Arguments

  16.     if objArgs.Count = 3 then
  17.         priv_con = objArgs(0)
  18.                 publ_con = objArgs(1)
  19.                 switch = objArgs(2)

  20.         if Initialize() = TRUE then
  21.             GetConnectionObjects()
  22.             FirewallTestByName priv_con,publ_con
  23.         end if
  24.     else
  25.         DIM szMsg
  26.         if Initialize() = TRUE then
  27.             GetConnectionObjects()
  28.             FirewallTestByName "list","list"
  29.         end if

  30.         szMsg = "To share your internet connection, please provide the name of the private and public connections as the argument." & vbCRLF & vbCRLF & _
  31.                 "Usage:" & vbCRLF & _
  32.                 "       " & WScript.scriptname & " " & chr(34) & "Private Connection Name" & chr(34) & " " & chr(34) & "Public Connection Name" & chr(34)
  33.         WScript.Echo( szMsg & vbCRLF & vbCRLF)
  34.     end if
  35. end sub

  36. sub FirewallTestByName(con1,con2)
  37.         on error resume next
  38.     DIM Item
  39.     DIM EveryConnection
  40.     DIM objNCProps
  41.     DIM szMsg
  42.     DIM bFound1,bFound2

  43.     WScript.echo(vbCRLF & vbCRLF)
  44.     bFound1 = false
  45.     bFound2 = false
  46.     for each Item in EveryConnectionCollection
  47.         set EveryConnection = NetSharingManager.INetSharingConfigurationForINetConnection(Item)
  48.         set objNCProps = NetSharingManager.NetConnectionProps(Item)
  49.         szMsg = "Name: "       & objNCProps.Name & vbCRLF & _
  50.                 "Guid: "       & objNCProps.Guid & vbCRLF & _
  51.                 "DeviceName: " & objNCProps.DeviceName & vbCRLF & _
  52.                 "Status: "     & objNCProps.Status & vbCRLF & _
  53.                 "MediaType: "  & objNCProps.MediaType
  54.         if EveryConnection.SharingEnabled then
  55.             szMsg = szMsg & vbCRLF & _
  56.                     "SharingEnabled" & vbCRLF & _
  57.                     "SharingType: " & ConvertConnectionTypeToString(EveryConnection.SharingConnectionType)
  58.         end if

  59.         if objNCProps.Name = con1 then
  60.             bFound1 = true
  61.             if EveryConnection.SharingEnabled = False and switch="on" then
  62.                 szMsg = szMsg & vbCRLF & "Not Shared... Enabling private connection share..."
  63.                 WScript.Echo(szMsg)
  64.                 EveryConnection.EnableSharing CONNECTION_PRIVATE
  65.                 szMsg = " Shared!"
  66.                                                 elseif(switch = "off") then
  67.                                                                 szMsg = szMsg & vbCRLF & "Shared... DisEnabling private connection share..."
  68.                 WScript.Echo(szMsg)
  69.                                                                 EveryConnection.EnableSharing CONNECTION_ALL
  70.                                
  71.             end if
  72.                         end if

  73.         if objNCProps.Name = con2 then
  74.             bFound2 = true
  75.             if EveryConnection.SharingEnabled = False and switch="on" then
  76.                 szMsg = szMsg & vbCRLF & "Not Shared... Enabling public connection share..."
  77.                 WScript.Echo(szMsg)
  78.                 EveryConnection.EnableSharing CONNECTION_PUBLIC
  79.                 szMsg = " Shared!"
  80.                                                 elseif(switch = "off") then
  81.                                                                 szMsg = szMsg & vbCRLF & "Shared... DisEnabling public connection share..."
  82.                 WScript.Echo(szMsg)
  83.                                                                 EveryConnection.EnableSharing CONNECTION_ALL
  84.                    end if
  85.         end if
  86.         WScript.Echo(szMsg & vbCRLF & vbCRLF)
  87.     next

  88.     if( con1 <> "list" ) then
  89.         if( bFound1 = false ) then
  90.             WScript.Echo( "Connection " & chr(34) & con1 & chr(34) & " was not found" )
  91.         end if
  92.         if( bFound2 = false ) then
  93.             WScript.Echo( "Connection " & chr(34) & con2 & chr(34) & " was not found" )
  94.         end if
  95.     end if
  96. end sub

  97. function Initialize()
  98.     DIM bReturn
  99.     bReturn = FALSE

  100.     set NetSharingManager = Wscript.CreateObject("HNetCfg.HNetShare.1")
  101.     if (IsObject(NetSharingManager)) = FALSE then
  102.         Wscript.Echo("Unable to get the HNetCfg.HnetShare.1 object")
  103.     else
  104.         if (IsNull(NetSharingManager.SharingInstalled) = TRUE) then
  105.             Wscript.Echo("Sharing isn't available on this platform.")
  106.         else
  107.             bReturn = TRUE
  108.         end if
  109.     end if
  110.     Initialize = bReturn
  111. end function

  112. function GetConnectionObjects()
  113.     DIM bReturn
  114.     DIM Item

  115.     bReturn = TRUE

  116.     if GetConnection(CONNECTION_PUBLIC) = FALSE then
  117.         bReturn = FALSE
  118.     end if

  119.     if GetConnection(CONNECTION_PRIVATE) = FALSE then
  120.         bReturn = FALSE
  121.     end if

  122.     if GetConnection(CONNECTION_ALL) = FALSE then
  123.         bReturn = FALSE
  124.     end if

  125.     GetConnectionObjects = bReturn

  126. end function


  127. function GetConnection(CONNECTION_TYPE)
  128.     DIM bReturn
  129.     DIM Connection
  130.     DIM Item
  131.     bReturn = TRUE

  132.     if (CONNECTION_PUBLIC = CONNECTION_TYPE) then
  133.         set Connection = NetSharingManager.EnumPublicConnections(ICSSC_DEFAULT)
  134.         if (Connection.Count > 0) and (Connection.Count < 2) then
  135.             for each Item in Connection
  136.                 set PublicConnection = NetSharingManager.INetSharingConfigurationForINetConnection(Item)
  137.             next
  138.         else
  139.             bReturn = FALSE
  140.         end if
  141.     elseif (CONNECTION_PRIVATE = CONNECTION_TYPE) then
  142.         set Connection = NetSharingManager.EnumPrivateConnections(ICSSC_DEFAULT)
  143.         if (Connection.Count > 0) and (Connection.Count < 2) then
  144.             for each Item in Connection
  145.                 set PrivateConnection = NetSharingManager.INetSharingConfigurationForINetConnection(Item)
  146.             next
  147.         else
  148.             bReturn = FALSE
  149.         end if
  150.     elseif (CONNECTION_ALL = CONNECTION_TYPE) then
  151.         set Connection = NetSharingManager.EnumEveryConnection
  152.         if (Connection.Count > 0) then
  153.             set EveryConnectionCollection = Connection
  154.         else
  155.             bReturn = FALSE
  156.         end if
  157.     else
  158.         bReturn = FALSE
  159.     end if

  160.     if (TRUE = bReturn)  then
  161.         if (Connection.Count = 0) then
  162.             Wscript.Echo("No " + CStr(ConvertConnectionTypeToString(CONNECTION_TYPE)) + " connections exist (Connection.Count gave us 0)")
  163.             bReturn = FALSE
  164.         'valid to have more than 1 connection returned from EnumEveryConnection
  165.         elseif (Connection.Count > 1) and (CONNECTION_ALL <> CONNECTION_TYPE) then
  166.             Wscript.Echo("ERROR: There was more than one " + ConvertConnectionTypeToString(CONNECTION_TYPE) + " connection (" + CStr(Connection.Count) + ")")
  167.             bReturn = FALSE
  168.         end if
  169.     end if
  170.     Wscript.Echo(CStr(Connection.Count) + " objects for connection type " + ConvertConnectionTypeToString(CONNECTION_TYPE))

  171.     GetConnection = bReturn
  172. end function

  173. function ConvertConnectionTypeToString(ConnectionID)
  174.     DIM ConnectionString

  175.     if (ConnectionID = CONNECTION_PUBLIC) then
  176.         ConnectionString = "public"
  177.     elseif (ConnectionID = CONNECTION_PRIVATE) then
  178.         ConnectionString = "private"
  179.     elseif (ConnectionID = CONNECTION_ALL) then
  180.         ConnectionString = "all"
  181.     else
  182.         ConnectionString = "Unknown: " + CStr(ConnectionID)
  183.     end if

  184.     ConvertConnectionTypeToString = ConnectionString
  185. end function
复制代码
  1. ics.vbs  本地连接 "无线网络连接 2" on
复制代码
 楼主| 发表于 2013-11-11 10:21:11 | 显示全部楼层
相当感谢
 楼主| 发表于 2013-11-11 10:31:31 | 显示全部楼层
前面是保存为vbs
ics.vbs  本地连接 "无线网络连接 2" on 这个保存为bat吧  

貌似没用呀
您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|手机版|小黑屋|批处理之家 ( 渝ICP备10000708号 )

GMT+8, 2026-3-20 23:24 , Processed in 0.030881 second(s), 17 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

快速回复 返回顶部 返回列表