如何制作添加网络路由的脚本?

发布于 2024-09-11 00:53:28 字数 1111 浏览 7 评论 0原文

场景

SO Windows 7

我需要通过 cisco VPN 连接到远程主机。有时主机目标网络与本地网络相同。 示例(ipconfig 命令的部分输出):

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

我需要通过 Vpn 连接到远程网络上的主机 192.168.1.11 然后我需要添加新路由(在此配置中,所有到 192.168.1.xxx 的流量都是到本地网络的路由) 路由打印的输出开头为:

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

命令为:
路由添加 192.168.1.11 掩码 255.255.255.255 192.168.100.12 度量 1 如果 17

问题:
Cisco 适配器上的 IP 不是静态的,我无法使用修饰符 -p (永久)添加路由。断开连接并重新连接时,我需要查找 NewIp 并添加正确的路由:
路由添加 192.168.1.11 掩码 255.255.255.255 «NewIP» 度量 1 if 17
对我来说,每次都编写所有这些命令很容易(但很无聊):

ipconfig  etc
route print etc
route add etc

我需要一个 bat 或 PowerShell 脚本来添加正确的路线。该脚本在 Cisco 适配器上查找 IP,并在建立的 IP 网关的情况下运行命令 Route add。

谢谢

Scenario

SO Windows 7

I need to connect to remote host via cisco VPN. Sometimes the host destination network is the same of local network.
Example (Partial output of 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 host 192.168.1.11 on the remote network via Vpn
Then I need to add new route (in this configuration all traffic to 192.168.1.xxx are route to local network)
The output of route print begining by:

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 is:
route add 192.168.1.11 mask 255.255.255.255 192.168.100.12 metric 1 if 17

Problem:
The Ip on Cisco adapter is not static, i can't route add with modifier -p (permanent) When disconected and reconnect I need look for the 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) write all this commmands every time:

ipconfig  etc
route print etc
route add etc

I need a bat or PowerShell script to add the correct route. The scrip look for the IP on the Cisco adapter and run the command route add with IP gateway founded.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

满天都是小星星 2024-09-18 00:53:28

我尝试了 renegm 的答案,并且 Index 属性为我提供了错误的值。这对我有用(这里是 Juniper VPN,但有同样的问题) - 注意 InterfaceIndex 而不是 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 $if

I tried renegm's answer, and the Index property gives the wrong value for me. This worked for me though (Juniper VPN here, but the same problem) - note 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 $if
ヅ她的身影、若隐若现 2024-09-18 00:53:28

我解决了。 (我是PowerShell的新手,但问题很简单)

$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

I solved it. (I'm a complete newbie in PowerShell, but 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
挽容 2024-09-18 00:53:28

这是一个相当老的讨论,但您可以在连接到 VPN 后使用以下 PowerShell 命令创建路由:

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

它适用于 Windows 10。

This is a pretty old discussion, but you could use the following PowerShell command to create a route AFTER you're connected to VPN:

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

It worked on Windows 10.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文