Configure static routes on Windows - windows

There is a netsh and a route command on Windows. From their help text it looks like both can be used to configure static routes. When should you use one and not the other? Is IPv6 a distinguishing factor here?

route is a very old and basic tool for displaying and modifying the entries in the local IP routing table while netsh is the newer, more robust command-line scripting utility that allows you to, either locally or remotely, manipulate the network configuration.
netsh has a zillion more features than route; it can even save your current settings as a script that another instance of netsh can parse. Check out Using netsh to see the giant feature set and compare it to how very basic and simple routes is.

Related

Is there an programmatic way to create a custom network profile in Windows and assign a virtual network adapter to it?

I'd like to programmatically create a new Network Profile (in addtion to those listed under HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\NetworkList\Profiles). Then I'd like to programmatically assign a particular network adapter to it (e.g., vEthernet (WSL)), and set the profile to Private network mode to follow the firewall rules, similar to what we can do for Wi-Fi networks. Is there a Windows or PowerShell API to help with that?
Eventually, the goal is to access Windows network from WSL2 Linux without tweaking the Windows Firewall settings manually.
More context for the question, including some things I've tried so far.
Currently, my solution is to call New-NetFirewallRule -DisplayName "WSL" -Direction Inbound -InterfaceAlias "vEthernet (WSL)" manually each time I need access the Windows host network from WSL2.
Personally, I would avoid P/Invoke if possible - it's usually the most fiddly approach.
I suggest using netsh and parsing the text. Netsh is using the Win32 API under the hood, so that changes your problem from P/Invoke to regex (which I think is usually an easier tool to use).
This may be a useful pointer for the netsh invocation. The salient bits:
netsh lan show profiles
netsh lan export profile folder=PATH_TO_FOLDER interface="INTERFACE_NAME"
netsh lan add profile filename="PATH_AND_FILENAME.xml" interface="INTERFACE_NAME"
IIUC, you don't need a new profile every time, you only need to define that once. From my knowledge of WSL, you get a new IP on every boot, so the issue is assigning that to the one profile that you have created for the task.
If that's correct, then I would export a profile to XML for an existing profile, edit it once manually, then automate applying that XML when you see the WSL adapter.
Not sure if you want to do this interactively, but for full automation, the approach that pops into my mind is to attach a task to the relevant event in Event Viewer. On my box, I didn't spot any obvious event sources for WSL, but the Hyper-V-VmSwitch source looks promising. Here's a sample event:
Log Name: System
Source: Microsoft-Windows-Hyper-V-VmSwitch
Date: 02/08/2021 09:59:40
Event ID: 264
Task Category: None
Level: Information
Keywords: (128)
User: SYSTEM
Computer: ******************
Description:
Port D76B3365-5A23-4960-B044-066E05FF3F2D (Friendly Name: A7DB3628-B2A6-4605-AAED-229DB96E064E) successfully created on switch 5EE23C35-0881-4967-A447-FF22139BE1F4 (Friendly Name: WSL).
I've elided over a lot for brevity, but happy to answer follow-up questions.

Joining a PNRP cloud on my local subnet

I'm experimenting with PNRP, just using netsh to execute basic commands to register peers on my machine. I am able to add multiple peers on my machine by opening multiple instances of a command prompt, and entering the following commands
netsh
p2p pnrp peer
add registration 0.mike
After that I switch to the cloud context and enter show names and I see a bunch of info, and the names of my 2 peers that I registered through cmd. However, when I ask another developer (who has a machine on my same IPv4 subnet) to run the same commands they do not appear. I would expect a third peer to show up on my cloud, since we're using the same name 0.mike
What do I not understand about what is going on here, and what should I be doing to get the behavior I'd expect?
Show Names is designed to only show you the names that are registered on your local machines, not the names that your computer is aware of.
Due to the way that name storage works (a one way hash algorithm) the nodes of a network aren't actually aware of the names that they are storing, just the hashed values.
The command that you want to run is resolve 0.mike to actually perform a lookup for the name and return the IP addresses. You can restrict this by cloud as well if you need to.

Configure network interfaces in Go

Does Go offer a way to configure network interfaces? I found a very easy to use net.Interfaces method to get the information, but i want to modify the network configuration.
In order to modify your network config, the best way would be to call external tools like ip, iptables, ifconfig, brctl, etc..
This is the way we do in within docker (https://github.com/dotcloud/docker/blob/master/network.go#L72)

How to change the binding order of network adapters using commandline(wmi, netsh, vbscript, etc...)

I have several network connections that are connected to different networks.
and I want to make sure that a certain network connection is at the top of the list.
so I want to programmatically script changes to the binding order of network adapters for a windows(win 2008 server and win 2003 server)
Is it any way to change the binding order programmatically?just use wmi, netsh, vbscript, edit regstry or other script or command line tools.
I found edit registry registry HKLM\system\currentcontrolset\services\TCPIP\linkage\ -> bind value,
is a way on windows 2008 server.
use "netstat -rn" can found binding order is changed. but the on the networking advenced UI(ncpa.cpl)
the order is not changed.
and looks it not working on windows 2003 server.
Is any one have idea How to change the binding order of network adapters using commandline?
Thanks
This looks to be what you are after:
http://realworldwindows.blogspot.com/2006/11/changing-network-provider-order-in.html
Alternatively, you could easily script a batch file that disables the primary adapter so that the adapter you want to be primary takes over. Then re-enable the original one again.
Example:
#echo off
netsh interface set interface "Local Area Connection" DISABLED
:: do something here on the secondary connection
netsh interface set interface "Local Area Connection" ENABLED

How do I get bind to use the DHCP dns for lookup?

I've got XAMPP setup on my laptop (OSX 10.6) for dev, and I wanted to use VirtualDocumentRoot so that I could do *.localhost and it would automap to the folder under my sites directory. I've got this all set up fine, and it works great, but when I got to work today, I found an issue with the way our LAN handles DNS.
Long story short, instead of checking the LAN DNS server for local domains, it goes out to the root. Is there a way to get bind to check the DHCP supplied DNS server for addresses it's not responsible for? Or alternatively, is there a way to get my os to use the DHCP DNS server first, and then fall back to the local with minimal performance hit?
Thanks!
I'm using Linux Arch, but as MacOSX is based on some *nix system - may this ideas helps you:
Take a look at the file /etc/resolv.conf. In my setup this file is automatically generated by NetworkManager.
This document writes about ways to update /etc/resolv.conf when dhcpcd, NetworkManager or dhclient is used: https://wiki.archlinux.org/index.php/Dnsmasq#DHCP_Setup
In this way you do just prepend the local dns before the dhcp's dns (or static if you're switching to a static configuration). Make sure you remove all forwarders from your dns-server.
If macos does not use them, may this workaround gives you a hint, even if it's very limited:
Add a global name-server (like google's one 8.8.8.8) to your dns-server's list of forwarders.

Resources