Windows Powershell Set IP Address on network adapter - windows

I need to be able to take my laptop and plug into several different networks across several locations. Each network requires I use a static address. I currently have a shell script that prompts for a network location and uses netsh to set the IP address. However, Microsoft is warning that they might remove netsh and to use Powershell so I'm trying to recreate my script in Powershell.
The problem I'm having is that if I go from dhcp to a static address I can use:
New-NetIPAddress -InterfaceAlias $myAdapter -AddressFamily IPv4 10.1.2.3 -PrefixLength 24 -Type Unicast -DefaultGateway 10.1.2.1
However, when going from a static address to another static address using New-NetIPAddress just adds another IP address to the adapter (and I cannot connect to anything as it appears to only use the first IP address). To get around that I can use Set-NetIPAddress, but that appears to not accept the -DefaultGateway parameter so I'm assigned the new address, but with the old gateway. I tried using Remove-NetIPAddress, but that appears to leave the gateway parameter so the New-NetIPAddress command fails with "Instance DefaultGateway already exists". How can I either remove the gateway so I can start over with New-NetIPAddress or replace the gateway when using Set-NetIPAddress?

Use Remove-NetRoute to remove the gateway:
# Remove the static ip
Remove-NetIPAddress -InterfaceAlias $myAdapter
# Remove the default gateway
Remove-NetRoute -InterfaceAlias $myAdapter
# Add the new IP and gateway
New-NetIPAddress -InterfaceAlias $myAdapter -AddressFamily IPv4 10.1.2.4 -PrefixLength 24 -Type Unicast -DefaultGateway 10.1.2.255

Related

Restrict access to router VPN client to a single IP address

I have setup openvpn client on a asus router, it is running padavan firmware, which is similar to tomato and other.
The VPN client works, but I would like to limits it's use to one or 2 ips on my LAN (i.e. AppleTV) and all other clients bypass the VPN connection.
The padavan vpn client has a custom script that is executed with the interface goes up and down on tun0 which is the interface.
I have attempted to route the IP address of the client that I want to use, but it does not prevent access via all of the other clients:
#!/bin/sh
### Custom user script
### Called after internal VPN client connected/disconnected to remote VPN server
### $1 - action (up/down)
### $IFNAME - tunnel interface name (e.g. ppp5 or tun0)
### $IPLOCAL - tunnel local IP address
### $IPREMOTE - tunnel remote IP address
### $DNS1 - peer DNS1
### $DNS2 - peer DNS2
# private LAN subnet behind a remote server (example)
peer_lan="192.168.0.130"
peer_msk="255.255.255.253"
### example: add static route to private LAN subnet behind a remote server
func_ipup()
{
# route add -net $peer_lan netmask $peer_msk gw $IPREMOTE dev $IFNAME
# route add -net $peer_lan gw $IPREMOTE dev $IFNAME
route add default dev tun0 table 200
rule add from 192.168.0.130 table 200
return 0
}
func_ipdown()
{
# route del -net $peer_lan netmask $peer_msk gw $IPREMOTE dev $IFNAME
return 0
}
logger -t vpnc-script "$IFNAME $1"
case "$1" in
up)
func_ipup
;;
down)
func_ipdown
;;
esac
I realise that this is very specific to the padavan firmware, but I think that the commands that are executed when it goes up should be universal, and my routing skills are very limited !
Maybe I need to block / allow using ip tables instead?
Any suggestions or help gratefully appreciated !

What is this command prompt code in MacOS?

I know this code works on Windows, but how do I get workable code executable on MacOS?
netsh -c interface ipv4 add neighbors "(connection name)" "(router
address)" "(mac address)" store=persistent
Any suggestions please...
try typing "man arp" into the terminal. Something like this might work :
arp -s hostname ether_addr
Create an ARP entry for the host called hostname with the Ethernet address ether_addr. The Ethernet address is
given as six hex bytes separated by colons. The entry will be permanent unless the word temp is given in the
command. If the word pub is given, the entry will be ``published''; i.e., this system will act as an ARP server,
responding to requests for hostname even though the host address is not its own. In this case the ether_addr can
be given as auto in which case the interfaces on this host will be examined, and if one of them is found to
occupy the same subnet, its Ethernet address will be used. If the only keyword is also specified, this will cre-
ate a ``published (proxy only)'' entry. This type of entry is created automatically if arp detects that a rout-
ing table entry for hostname already exists.

windows get IP dynamically

i want to implement the following logic. Does it possible to have such implementation using batch or power shell ? please share with me script for that.
Let say I have a configuration file with the following "config.propertis":
BOOTPRORO=statis or dhcp
IPADDR=192.168.10.10
NETMASK=255.255.255.0
GATEWAY=192.168.10.1
DNS=8.8.8.8
I want that at startup the system will check that file and configure network accordingly:
OS: Windows
if in BOOTPROTO=dhcp, when use DHCP in network configuration and ignore all another in config file, except DNS
if in BOOTPROTO=static, then use all variables from config file to configure IP as static.
So, I have such logic under the Linus, using shell. The script in configured in rc.d and execute before network service. Does it possible to implement such over the Windows ? Guys, please share the script !
We can definitely do this.
First things first, because a lot of systems have more than one network interface, you'll need to determine what the ifIndex is of the adapter that we want to change. Do that by running Get-NetIPInterface. You should see results like this:
In my example and going forward, I'll be using this index, 41. You should change this to match what you find on your own computer.
OK, now to read from the text file. Since you've provided the data in a key=value pair format, commonly called a hashtable, we can easily grab the data from there using ConvertFrom-Stringdata. This will give us a PowerShell hashtable, and we can pull the needed line out like this.
$values = get-content T:\config.properties | ConvertFrom-StringData
$values.BootProro
>statis
We can us this to set the PC in Dynamic IP mode, or to set static addresses. Now, for you to use this in your environment, you need to find the ifIndex, as I mentioned before. replace my index of 41 with your own, and then give it a shot. I've added -WhatIf to every line, so you will see what would happen when you run it. If you're happy with the changes it woudl make, remove -Whatif to make the script actually change the settings.
$values = gc T:\config.properties | ConvertFrom-StringData
if ($values.BOOTPRORO -eq "dhcp"){
Write-Output "---DHCP mode detected in 'config.properties' file"
Write-Output "---Setting Set-NetAdapter -DHCP Enabled"
Set-NetIPInterface –InterfaceIndex 41 –Dhcp Enabled -WhatIf
}
else{
Write-outPut "---static mode detected in 'config.properties' file"
Write-Output "---Removing network configuration"
Remove-NetIPAddress -InterfaceIndex 41 -whatif
Write-Output "---Setting new network configuration equal to"
$values
New-NetIPAddress -DefaultGateway $values.GATEWAY -IPAddress $values.IPADDR -PrefixLength 24 -InterfaceIndex 41 -WhatIf
Set-DnsClientServerAddress -ServerAddresses $values.DNS -InterfaceIndex 41 -WhatIf
}
The output looks like this:
in Windows we can set ip address via batch file or powershell script but when you use dhcp address your ip is Dynamic not static I Imposition you want static ip address
BAtch-file
netsh interface ip set address name=”Local Area Connection” static 192.168.10.10 255.255.255.0 192.168.10.1
netsh interface ip set dns name=”Local Area Connection” static 8.8.8.8
if you want be dhcp You should set
netsh interface ip set address name=”Local Area Connection” source=dhcp
note I Imposition you nic name is Local Area Connection
In powershell V3.0 and Later we Used
New-NetIPAddress –InterfaceAlias “Local Area Connection ” –IPv4Address “192.168.10.10” –PrefixLength 24 -DefaultGateway 192.168.10.1
Set-DnsClientServerAddress -InterfaceAlias “Local Area Connection” -ServerAddresses 8.8.8.8
and for startup you can put script .bat and .ps1 in startup windows but attention you should Set-ExecutionPolicy bypass before U run any script of powershell
for startup any script see link

cant get dnsmasq to push multiple search prefixes

I'm trying to get dnsmasq to push multiple search prefixes to windows machines. If I look in the MS dhcp server, it looks to be using dhcp option 135, but any attempt to configure that eg
dhcp-option=135,domain.local1,domain.local2
doesnt get pushed at all (I'm using tcpdump -i br0 -lenx -s 1500 port bootps or port bootpc | dhcpdump to view wat dnsmasq is sending)
I have minor success using dhcp option 15, but it only pushes a single name into the search prefix as displayed by ipconfig /all on windows
Any suggestions ?
Checking the ISC dhcp option list I found this:
119 Domain Search domain-search
One or more domain names, each enclosed in quotes and separated by commas
But note that dnsmasq actually provides you special option (although I'm not sure from which version it starts)
dhcp-option=option:domain-search,eng.apple.com,marketing.apple.com
Our client machines (Ubuntu 18 server using netplan/systemd-resolve) were not requesting DHCP option 119, but I could solve the problem by forcing the server (dnsmasq) to sentd that option in the reply anyway:
dhcp-option-force=option:domain-search,internal,maindomain.com
dhcp-option=option:domain-name,maindomain.com
Using the dhcp-option-force parameter makes sure that the list is sent to the clients regardless of what they ask for.

How do I know the internal DNS name of an Amazon AWS instance?

I have a system that has N servers on the Amazon AWS cloud. They are all in the same zone. Instance A wants to talk to instance B, but it obviously doesn't go through the internet. As far as I understand, the internal IP changes every time I reboot the instance. Is there an internal, constant DNS name to all my instances, through which they can interact between themselves without worrying about restarts?
http://alestic.com/2009/06/ec2-elastic-ip-internal
No, there is no way to make use of 'fixed' IP addresses or DNS names using the out-of-the-box AWS instances. Even if you assign an EIP (Elastic IP) to the instance, this only affects the public-facing IP/DNS reference, not the internal one.
We use a pair of DNS servers in our EC2 estate (it's Windows, so they're Primary/Secondary AD Domain Controllers). By having all other instances use this pair as their DNS servers, we can assign unique machine names to each instance as they spin-up, and reference any/all other instances by these names.
So for example our EC2-based Subversion server has an EIP which means it's always at the same place when we talk to it from outside EC2, but the EC2-based CruiseControl server refers to it as [ourec2domain].SVNHOST because it registers that name with the DCs at startup.
I had the same issues when I first started using the cloud. I too use a setup of 2 DNS servers and add a tag to the two servers using the command ec2-create-tags <instance> --tag Purpose=DNS
Using the http://cloudinitnet.codeplex.com service I created the server runs a powershell script on startup. This powershell script checks amazon for the two dns servers and add them to the network interface. Assuming you have a list of dns servers at this point you can use the code below to add the entries to the dns server. To get a list of servers just query your account with the AWSSDKnet with powershell.
$connection = "Local Area Connection 2"
$registered = $false;
# Clean up the DNS entries incase there are any settings already
Write-Output "Clearing DNS Entries"
$X = netsh interface ip set dns $connection static none
$index = 1;
foreach ( $server in $servers)
{
# Set this server's
Write-Output "Adding server $server to DNS"
$X = netsh interface ip add dnsserver $connection $server index=$index
# Register the server's hostname with the dns server
if(-not ($registered))
{
$computer = hostname
$address = (netsh interface ip show address $connection | select-string "IP Address") -replace '^[^\d]+'
$rec = [WmiClass]"\\dns01\root\MicrosoftDNS:MicrosoftDNS_ResourceRecord"
$rec.CreateInstanceFromTextRepresentation("dns01", "network.cloud", "$($computer).network.cloud IN A $address")
$registered = $true;
}
$index++;
}
If your servers are not windows then you can use Ubuntu or Amazon Linux "Cloud-Init" to perform the same task.
From the instance:
curl http://169.254.169.254/latest/meta-data/local-hostname

Resources