commit d61c05b3d255ca4151dcb87afaa19c11b951e569 Author: jasonfoknxu Date: Sat Jan 14 18:59:44 2023 +0800 initial commit diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..91be417 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2022 NXU + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..4632fe7 --- /dev/null +++ b/README.md @@ -0,0 +1,43 @@ +# Switch Ethernet (PowerShell) + +A simple script to change the priority of two Network Interfaces (NICs). + +Useful for switching between two networks or change the priority of Ethernet and Wi-Fi. + + +## :sparkles: Requirements +- Text Editor +- PowerShell +- Only support **Windows** +- Admin Permission +- Basic technical knowledge + + +## :notebook: How To Use? +1. Open the ps1 file with text editor +2. Open setting / control panel to view the name of both NIC +3. Change the `$net1_name` & `$net2_name` to your NIC's name, change the `$check_ip_url` if you want +4. Create a shortcut to the script `powershell -f ` (For example, *`powershell -f "C:\Users\jasonfoknxu\Documents\switch-ethernet.ps1"`*) +5. Right-click the shortcut :arrow_right: Properties :arrow_right: Shortcut (tab) :arrow_right: Advanced :arrow_right: Select **`Run as Administrator`** +6. Execute the script by clicking the shortcut +7. Input `1` to select first network, `2` for the second network, `0` for returning back to default (auto) +8. Done + + +## :star: Example +![Two NIC](_img/nic.png) + +There are two networks, the name are `LAN` and `Wi-Fi` + +You can select your favorite IP checking server or empty (`$check_ip_url = ""`) to disable IP checking + +The config would be like the following: +```powershell +$net1_name = "LAN" +$net2_name = "Wi-Fi" +$check_ip_url = "https://api.ipify.org" +``` + + +## :gear: How it works? +The script will change the Interface Metric of target NIC to lower, another NIC to higher Metric diff --git a/_img/nic.png b/_img/nic.png new file mode 100755 index 0000000..f691690 Binary files /dev/null and b/_img/nic.png differ diff --git a/switch-ethernet.ps1 b/switch-ethernet.ps1 new file mode 100755 index 0000000..b5ffd1c --- /dev/null +++ b/switch-ethernet.ps1 @@ -0,0 +1,36 @@ +# switch-ethernet (PowerShell) +# - Developer: NXU (GitHub: @jasonfoknxu) +# - https://github.com/jasonfoknxu/ +# - Switch network with changing the Metric (Lower = Higher Priority) + +### ---------- ### + +$net1_name = "Ethernet" # Input the name of first network interface +$net2_name = "Ethernet 2" # Input the name of second network interface +$check_ip_url = "http://ipinfo.io/ip" # URL to check the current IP address (empty to disable checking) + +### ---------- ### + +$option = Read-Host "Networks:`n0 - Auto`n1 - $net1_name`n2 - $net2_name`nSwitch to" + +if ($option -eq 1) { + Write-Host "Switching to $net1_name" + Get-NetAdapter -Name $net1_name | Set-NetIPInterface -InterfaceMetric "5" + Get-NetAdapter -Name $net2_name | Set-NetIPInterface -InterfaceMetric "10" +} +elseif ($option -eq 2) { + Write-Host "Switching to $net2_name" + Get-NetAdapter -Name $net1_name | Set-NetIPInterface -InterfaceMetric "10" + Get-NetAdapter -Name $net2_name | Set-NetIPInterface -InterfaceMetric "5" +} +else { + Write-Host "Switching to Auto LAN" + Get-NetAdapter -Name $net1_name | Set-NetIPInterface -AutomaticMetric enabled + Get-NetAdapter -Name $net2_name | Set-NetIPInterface -AutomaticMetric enabled +} +if ($check_ip_url -ne "") { + $currentIP = (Invoke-WebRequest -UseBasicParsing -Uri $check_ip_url).Content.Trim() + Write-Host "Current IP: $currentIP" +} + +CMD /c PAUSE \ No newline at end of file