יום שבת, 4 בפברואר 2012

Network load balancing using Windows 7


Following is the script: (Save with a .vbs extension), I've marked in red the places u need to change so your script will fit your needs.
The 0.8 number means that at 0.8% network utilization, the script will change the adapter's metric and thus lower its priority.

If you just want to change the metric of your network interface from command line,
use this command : (run CMD as admin)

netsh interface ipv4 set interface "wired" metric=5

=====================================================
On Error Resume Next 

strComputer = "."
strMainNIC = "Wired"
strMainNICDevice = "Marvell Yukon 88E8056 PCI-E Gigabit Ethernet Controller"


Set objWMIService = GetObject("winmgmts:" _ 
& "{impersonationLevel=impersonate}!\\" & strComputer & "\Root\CIMV2" ) 

Set colItems = objWMIService.ExecQuery _ 
("Select * From Win32_NetworkAdapter Where NetConnectionID = '" & strMainNIC  & "'") 

For Each objItem in colItems 
strMACAddress = objItem.MACAddress 
Wscript.Echo "MACAddress: " & strMACAddress 
Next 

Set colNetCard = objWMIService.ExecQuery _ 
("Select * From Win32_NetworkAdapterConfiguration where IPEnabled=TRUE" ) 

For Each objNetCard in colNetCard 
If objNetCard.MACAddress = strMACAddress Then 
For Each strIPAddress in objNetCard.IPAddress 
Wscript.Echo "Description: " & objNetCard.Description 
Wscript.Echo "IP Address: " & strIPAddress 
Wscript.Echo "IPConnectionMetric: " & objNetCard.IPConnectionMetric 
regValueDataMetric = objNetCard.IPConnectionMetric 
Next 
End If 
Next 

For X = 0 to 1000000000000000000 

Set objWMIService = GetObject("winmgmts:" _ 
& "{impersonationLevel=impersonate}!\\" & strComputer & "\Root\CIMV2" ) 
Set colItems = objWMIService.ExecQuery _ 
("select * from Win32_PerfFormattedData_TCPIP_NetworkInterface Where Name = '" & strMainNICDevice  & "'" ) 
For Each objItem in colItems 
bytesTotal = objitem.BytesTotalPersec 
bandwidth = objItem.CurrentBandwidth 
result = FormatNumber(( bytesTotal / bandwidth) * 1000) 
output = "Main NIC utilization : " & result & " % Utilized: " & objitem.BytesTotalPersec & " " & regValueDataMetric & " Total Bandwidth: " & bandwidth 
WScript.Echo output 

NEXT 

If result >= 0.8 Then 
Wscript.Echo "Utilizing Secondary NIC"
If regValueDataMetric = "1" Then 
regValueDataMetric = "50" 
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.Run  "netsh interface ipv4 set interface """"Wired"""" metric=" & regValueDataMetric, 0, TRUE
Set colItems = objWMIService.ExecQuery _ 
("Select * From Win32_NetworkAdapter Where NetConnectionID = '" & strMainNIC  & "'") 

End if 
End If 

If result <= 0.8 Then 

If regValueDataMetric = "50" Then 
   regValueDataMetric = "1" 

Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.Run  "netsh interface ipv4 set interface """"Wired"""" metric=" & regValueDataMetric, 0, TRUE

Set colItems = objWMIService.ExecQuery _ 
("Select * From Win32_NetworkAdapter Where NetConnectionID = '" & strMainNIC  & "'") 

For Each objItem in colItems 
strMACAddress = objItem.MACAddress 
Wscript.Echo "MACAddress: " & strMACAddress 
Next 

Set colNetCard = objWMIService.ExecQuery _ 
("Select * From Win32_NetworkAdapterConfiguration where IPEnabled=TRUE" ) 

End if 
End If 

WScript.Sleep 1000

Next 
==================================================================





16 תגובות:

  1. why when i type cd \netlb it says the system cannot find the path specified?

    השבמחק
    תשובות
    1. Because you need to cteate that folder and place the above script inside beforehand

      מחק
  2. תגובה זו הוסרה על ידי המחבר.

    השבמחק
  3. Hi
    .I'm trying out your script but it seems to stop for some reason
    After launching the script it displays the network card IP address etc, as on your video but it doesn't show the utilization information. Is there a line missing in the above code or will this not work in Windows 7 Home Premium edition

    השבמחק
    תשובות
    1. The script works as it us, no missing lines, should work fine with your os version as well.
      Make sure you run it as administrator..

      מחק
  4. I have the same problem as AndyUK300ZX!
    I did exactly as you said!
    All I ve got is:

    Changed IP and MAC!!

    MACAddress: 00:00:00:00:00:00
    Description: NVIDIA nForce 10/100/1000 Mbps Ethernet #2
    IP Address: 00.00.00.00
    IPConnectionMetric: 1
    Description: NVIDIA nForce 10/100/1000 Mbps Ethernet #2
    IP Address: 00::00:00:00:00
    IPConnectionMetric: 1

    do you have an Idea

    השבמחק
  5. Yes I did!
    I thought it's not working because of the IPv6 and I shut it down
    but nothing changed, It just stops after

    MACAddress: 00:00:00:00:00:00
    Description: NVIDIA nForce 10/100/1000 Mbps Ethernet #2
    IP Address: 00.00.00.00
    IPConnectionMetric: 1

    השבמחק
  6. I never played with vbscript but I might do it now because your script shows it wouldn't be so hard to create some basic load balancing under windows so thanks! I would focus on two enhancements :
    - for its calculations your script uses the maximum theoretical bandwith of an interface but in my case my ISP never reaches that bandwith so the load balancing never triggers because the ratio is never superated so the script needs to do some bandwith speed test to calculate the real maximum bandwith available.
    - the script compares the description of the available interfaces programmatically with the description of the interface the user pasted in the script but unfortunately windows internally replaces symbols like "/" or "#" with "_" so the script just hangs
    ex: "Realtek RTL8187 Wireless 802.11b/g 54Mbps USB 2.0 Network Adapter" becomes "Realtek RTL8187 Wireless 802.11b_g 54Mbps USB 2.0 Network Adapter" , "TP-LINK 150Mbps Wireless Lite N Adapter #2" becomes "TP-LINK 150Mbps Wireless Lite N Adapter _2"

    השבמחק
  7. The problem is in this each For Each objItem in colItem no objects

    השבמחק
  8. What if i have 3 Internet connections on 3 interfaces, will this script still works?

    השבמחק
  9. The script will need to be modified but the principal remains the same.
    route all traffic through Network interface A until it reaches 80% utilization, then change priorities so traffic is to be routed through network interface B,
    once both network interfaces are at 80%, route new connections to network interface C.

    This requires a bit more logic on the script, but in principal it should work.

    Another approach would be using PFSense as your internet router / gateway.
    PFSense can be installed as a virtual machine as well, and it includes a built-in network load balancer with fail-over capabilities. I think this would be more robust plus it'll let all your home LAN network enjoy all links you got.

    השבמחק
  10. Hi there, i changed the connection name, and it works,
    but the secondary connection never starts downloading
    it says "utilizing secondary nic"
    but it never starts to show any kind of load

    can this script be used with 2 wireless connections?

    Thanks

    השבמחק
  11. works like a sharm on windows 8 pro x64, utorrent.... tks

    השבמחק
  12. not working with TAP-Adapter as main NIC

    השבמחק
  13. Thanks for the script its work ferpect, can you add the script id aim using 2 wireless and 1 wired . thank you

    השבמחק