Board logo

标题: [网络连接] 请问批处理能实现共享某一网卡吗,或共享某一连接 [打印本页]

作者: hundezhexue1    时间: 2013-11-7 15:18     标题: 请问批处理能实现共享某一网卡吗,或共享某一连接

比如我们店本地连接右键属性在点共享在选择要共享到的另一网卡 批处理能实现这一过程吗
作者: 522235677    时间: 2013-11-7 22:33

  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.             end if
  71.          end if
  72.         if objNCProps.Name = con2 then
  73.             bFound2 = true
  74.             if EveryConnection.SharingEnabled = False and switch="on" then
  75.                 szMsg = szMsg & vbCRLF & "Not Shared... Enabling public connection share..."
  76.                 WScript.Echo(szMsg)
  77.                 EveryConnection.EnableSharing CONNECTION_PUBLIC
  78.                 szMsg = " Shared!"
  79. elseif(switch = "off") then
  80. szMsg = szMsg & vbCRLF & "Shared... DisEnabling public connection share..."
  81.                 WScript.Echo(szMsg)
  82. EveryConnection.EnableSharing CONNECTION_ALL
  83.             end if
  84.         end if
  85.         WScript.Echo(szMsg & vbCRLF & vbCRLF)
  86.     next
  87.     if( con1 <> "list" ) then
  88.         if( bFound1 = false ) then
  89.             WScript.Echo( "Connection " & chr(34) & con1 & chr(34) & " was not found" )
  90.         end if
  91.         if( bFound2 = false ) then
  92.             WScript.Echo( "Connection " & chr(34) & con2 & chr(34) & " was not found" )
  93.         end if
  94.     end if
  95. end sub
  96. function Initialize()
  97.     DIM bReturn
  98.     bReturn = FALSE
  99.     set NetSharingManager = Wscript.CreateObject("HNetCfg.HNetShare.1")
  100.     if (IsObject(NetSharingManager)) = FALSE then
  101.         Wscript.Echo("Unable to get the HNetCfg.HnetShare.1 object")
  102.     else
  103.         if (IsNull(NetSharingManager.SharingInstalled) = TRUE) then
  104.             Wscript.Echo("Sharing isn't available on this platform.")
  105.         else
  106.             bReturn = TRUE
  107.         end if
  108.     end if
  109.     Initialize = bReturn
  110. end function
  111. function GetConnectionObjects()
  112.     DIM bReturn
  113.     DIM Item
  114.     bReturn = TRUE
  115.     if GetConnection(CONNECTION_PUBLIC) = FALSE then
  116.         bReturn = FALSE
  117.     end if
  118.     if GetConnection(CONNECTION_PRIVATE) = FALSE then
  119.         bReturn = FALSE
  120.     end if
  121.     if GetConnection(CONNECTION_ALL) = FALSE then
  122.         bReturn = FALSE
  123.     end if
  124.     GetConnectionObjects = bReturn
  125. end function
  126. function GetConnection(CONNECTION_TYPE)
  127.     DIM bReturn
  128.     DIM Connection
  129.     DIM Item
  130.     bReturn = TRUE
  131.     if (CONNECTION_PUBLIC = CONNECTION_TYPE) then
  132.         set Connection = NetSharingManager.EnumPublicConnections(ICSSC_DEFAULT)
  133.         if (Connection.Count > 0) and (Connection.Count < 2) then
  134.             for each Item in Connection
  135.                 set PublicConnection = NetSharingManager.INetSharingConfigurationForINetConnection(Item)
  136.             next
  137.         else
  138.             bReturn = FALSE
  139.         end if
  140.     elseif (CONNECTION_PRIVATE = CONNECTION_TYPE) then
  141.         set Connection = NetSharingManager.EnumPrivateConnections(ICSSC_DEFAULT)
  142.         if (Connection.Count > 0) and (Connection.Count < 2) then
  143.             for each Item in Connection
  144.                 set PrivateConnection = NetSharingManager.INetSharingConfigurationForINetConnection(Item)
  145.             next
  146.         else
  147.             bReturn = FALSE
  148.         end if
  149.     elseif (CONNECTION_ALL = CONNECTION_TYPE) then
  150.         set Connection = NetSharingManager.EnumEveryConnection
  151.         if (Connection.Count > 0) then
  152.             set EveryConnectionCollection = Connection
  153.         else
  154.             bReturn = FALSE
  155.         end if
  156.     else
  157.         bReturn = FALSE
  158.     end if
  159.     if (TRUE = bReturn)  then
  160.         if (Connection.Count = 0) then
  161.             Wscript.Echo("No " + CStr(ConvertConnectionTypeToString(CONNECTION_TYPE)) + " connections exist (Connection.Count gave us 0)")
  162.             bReturn = FALSE
  163.         'valid to have more than 1 connection returned from EnumEveryConnection
  164.         elseif (Connection.Count > 1) and (CONNECTION_ALL <> CONNECTION_TYPE) then
  165.             Wscript.Echo("ERROR: There was more than one " + ConvertConnectionTypeToString(CONNECTION_TYPE) + " connection (" + CStr(Connection.Count) + ")")
  166.             bReturn = FALSE
  167.         end if
  168.     end if
  169.     Wscript.Echo(CStr(Connection.Count) + " objects for connection type " + ConvertConnectionTypeToString(CONNECTION_TYPE))
  170.     GetConnection = bReturn
  171. end function
  172. function ConvertConnectionTypeToString(ConnectionID)
  173.     DIM ConnectionString
  174.     if (ConnectionID = CONNECTION_PUBLIC) then
  175.         ConnectionString = "public"
  176.     elseif (ConnectionID = CONNECTION_PRIVATE) then
  177.         ConnectionString = "private"
  178.     elseif (ConnectionID = CONNECTION_ALL) then
  179.         ConnectionString = "all"
  180.     else
  181.         ConnectionString = "Unknown: " + CStr(ConnectionID)
  182.     end if
  183.     ConvertConnectionTypeToString = ConnectionString
  184. end function
复制代码
  1. ics.vbs  本地连接 "无线网络连接 2" on
复制代码

作者: hundezhexue1    时间: 2013-11-11 10:21

相当感谢
作者: hundezhexue1    时间: 2013-11-11 10:31

前面是保存为vbs
ics.vbs  本地连接 "无线网络连接 2" on 这个保存为bat吧  

貌似没用呀




欢迎光临 批处理之家 (http://www.bathome.net/) Powered by Discuz! 7.2