How to make a script that adds a network route?

Scenic

SO Windows 7

I need to connect to a remote host through cisco vpn. Sometimes the host destination network is the same as the local network. Example (partial output from ipconfig command):

  Ethernet adapter Cisco Vpn Adapter:
    IPv4 Address.  .  .  .  .  .  .  .  .  .  .  : 192.168.100.12
    Subnet Mask.  .  .  .  .  .  .  .  .  .  .  : 255.255.255.0

 Wireless LAN adapter Wireless Network Connection:
    IPv4 Address.  .  .  .  .  .  .  .  .  .  .  : 192.168.1.74
    Subnet Mask.  .  .  .  .  .  .  .  .  .  .  : 255.255.255.0

I need to connect to the host 192.168.1.11 on the remote network via Vpn. Then I need to add a new route (in this configuration, all traffic up to 192.168.1.xxx is the route to the local network) The result of printing the route starts with:

  Interface list
  17 ... 00 05 9a 3c 78 00 ...... Cisco Systems VPN Adapter
  12 ... 00 16 44 ea 74 58 ...... Dell Wireless 1395 WLAN Mini-Card 

And the command:
route add 192.168.1.11 mask 255.255.255.255 192.168.100.12 metric 1 if 17

Problem:
The ip on the Cisco adapter is not static, I cannot route the addition using the -p modifier (persistent). When disconnecting and reconnecting, I need to look for NewIp and add the correct route:
route add 192.168.1.11 mask 255.255.255.255 Β«NewIPΒ» metric 1 if 17
For me its easy (but boring) every time they write all these comm messages:

  ipconfig etc
 route print etc
 route add etc

I need a bat or powershell script to add the correct route. The script looks for ip on the Cisco adapter and runs the route add command with the IP gateway base.

thanks

PD Sory my bad english

+4
source share
3 answers

I tried the renegm answer and the Index property gives the wrong value for me. This worked for me (Juniper VPN is here, but the same problem) - pay attention to InterfaceIndex instead of index:

 $adapter=Get-WmiObject Win32_NetworkAdapterConfiguration| Where-Object { $_.Description -match "Juniper Network Connect Virtual Adapter"} $Gateway=$adapter.IPaddress[0] $if=$adapter.InterfaceIndex route add 192.168.1.3 mask 255.255.255.255 $Gateway metric 1 $if 
+6
source

I solved it. (I'm a complete newbie to PowerShell, but the problem is very simple)

  $ adapter = Get-WmiObject Win32_NetworkAdapterConfiguration |
     Where-Object {$ _. Description -match "Cisco Systems VPN Adapter"}
 $ GateWay = $ adapter.IPaddress [0] 
 $ if = $ adapter.Index
 route add 192.168.1.11 mask 255.255.255.255 $ Gateway metric 1 if $ if 
+4
source

This is a pretty old discussion, but you can use the following PowerShell command to create a route AFTER connecting to a VPN:

 Add-VpnConnectionRoute -ConnectionName "You VPN Name Here" -DestinationPrefix 10.0.0.0/8 

It worked on Windows 10.

+1
source

Source: https://habr.com/ru/post/1316342/


All Articles