简体   繁体   English

有没有办法在互联网丢失时禁用和启用网络连接

[英]Is there a way to disable and enable network connection when internet is lost

this is my first post so hopefully I am doing it right.这是我的第一篇文章,所以希望我做得对。

I am looking for a way to automate the process of disabling my Ethernet network connection and then reenable it.我正在寻找一种方法来自动化禁用我的以太网网络连接然后重新启用它的过程。

I am on a camp internet connection (work away for weeks at a time) and the internet connection is fine besides it dropping out every half an hour or so and requiring me to refresh the connection.我在营地互联网连接上(一次工作数周),互联网连接很好,除了每隔半小时左右就会掉线并要求我刷新连接。 Usually I just disconnect the Ethernet plug for a second and plug it back in but I am wanting to automate the process so I can remote connect to the pc when I am away.通常我只是断开以太网插头一秒钟然后重新插入,但我想自动化这个过程,这样我就可以在我离开时远程连接到电脑。

Any help on how to accomplish this would be appreciated.任何有关如何实现此目的的帮助将不胜感激。

Edit: I left a vital part out of my description.编辑:我在描述中遗漏了一个重要部分。 I am wanting the script to detect when the internet connection is lost and then refresh the connection.我希望脚本检测互联网连接何时丢失,然后刷新连接。 Once the connection is lost it never regains it until I refresh the connection.一旦连接丢失,它永远不会重新获得它,直到我刷新连接。

You can handle this using Get-NetAdapter , Enable-NetAdapter and Disable-NetAdapter commands.您可以使用Get-NetAdapterEnable-NetAdapterDisable-NetAdapter命令来处理这个问题。

If you are using DHCP如果您使用的是 DHCP

Then Dollars to donuts you are just needing to renew your DHCP lease.然后美元换成甜甜圈,您只需要更新您的 DHCP 租约。

That is something you can do in CMD easily (I'm sure there is likely as easy a method in PS, but I don't know it offhand)这是您可以在 CMD 中轻松完成的事情(我确信在 PS 中可能有同样简单的方法,但我不知道)

This will release the currently assigned DHCP Address wait a few seconds and then renew it.这将释放当前分配的 DHCP 地址,等待几秒钟,然后更新它。

IPConfig /Release & timeout 5 & IPConfig /Renew

At the CLI or in a batch script you could set up a do-while loop to loop until you return an expected up address if you wanted.在 CLI 或批处理脚本中,您可以设置一个 do-while 循环来循环,直到您返回预期的向上地址(如果需要)。

Example of a costume loop in cmd cli. cmd cli 中的服装循环示例。

For /L %L (1,1,2147483648) DO (
  IPConfig /Release & timeout 3 & IPConfig /Renew &timeout 3 &(
    Ping -n 2 4.2.2.2 | find /I "Reply From" | find /I " 4.2.2.2"  &&(
      Exit /b 0
    )
  )
)

Disabling and enabling your NIC can be done through CMD or Powershell.可以通过 CMD 或 Powershell 禁用和启用 NIC。

Here Powershell is easier.这里 Powershell 比较容易。

Get only currently enabled adapters, use that to disable them wait a few seconds and enable them again仅获取当前启用的适配器,使用它来禁用它们等待几秒钟并再次启用它们

$NIC_Original_State = get-NetAdapter | ? {$_.Status -eq "Enabled"}
$NIC_Original_State | Disable-NetAdapter -name $_.name -confirm:$false
Sleep 5
$NIC_Original_State | Enable-NetAdapter -name $_.name -confirm:$false

Combine with checking the ping to the internet (as in cmd example) to generate a loop until we have a good connection.结合检查对 Internet 的 ping(如 cmd 示例)生成一个循环,直到我们有一个良好的连接。

$NIC_Original_State = get-NetAdapter | ? {$_.Status -eq "Enabled"}

DO {
  $NIC_Original_State | Disable-NetAdapter -name $_.name -confirm:$false
  Sleep 5
  $NIC_Original_State | Enable-NetAdapter -name $_.name -confirm:$false
  Sleep 5
} While ( ! $(test-connection -TargetName 4.2.2.2 -Quiet ) )

Now, PowerShell will need to be instantiated from a CMD prompt, I usually write a .cmd script wrapper for the .ps1 script to be called from in task scedualer or by hand.现在,PowerShell 需要从CMD提示符中实例化,我通常为.ps1脚本编写一个.cmd脚本包装器,以便在任务中手动调用或调用。

Sometimes I will write a combo .cmd script with the ps code embedded and just create a hardline.有时我会编写一个嵌入ps代码的组合.cmd脚本,然后创建一个强硬路线。 To the original .cmd script as a.以原来的.cmd脚本作为一个。 .ps1 to execute the ps code. .ps1执行 ps 代码。

Although for something this simple it might be simplest to use PowerShell.exe to run the script as an in-line command.尽管对于这么简单的事情,使用 PowerShell.exe 将脚本作为内联命令运行可能是最简单的。

But all roads lead to Rome, you do you on executing PowerShell, but in order to run it periodically, you will need to schedule the script to run, and by default there is no way to run ps scripts directly, so pick a method that you like that works for running the script and then use windows scheduler to schedule the task to run every x interval you like (say hourly)但是条条大路通罗马,你在执行PowerShell,但是为了定期运行它,你需要调度脚本运行,并且默认没有办法直接运行ps脚本,所以选择一个方法您喜欢运行脚本,然后使用 windows 调度程序安排任务每隔 x 间隔运行一次(比如每小时)

Make surw you select a new task (not a basic task) and select run with highest priviledges" checkbox, and select the newest version of windows available in the compatibility list-box. Make surw you select a new task (not a basic task) and select run with highest priviledges" checkbox, and select the newest version of windows available in the compatibility list-box.

Make sure to provide a useename and password of an administrator, and select to save the password/run whether the user is logged on or not.确保提供管理员的用户名和密码,以及 select 以保存密码/运行无论用户是否登录。

Set your triggers and put in the method you chose to execute the script and run some tests to make sure it works as expected.设置触发器并放入您选择的方法来执行脚本并运行一些测试以确保它按预期工作。

Edit:编辑:

The original method is int he form DO { Action } WHILE ( Condition To Check Returns True ) Since the While is at the end it guarantees one iteration.原始方法采用DO { Action } WHILE (检查条件返回 True ) ,因为 While 在末尾,它保证了一次迭代。

Since you don't want to disable and enable the NIC even once if the internet is pingable, you can use a WHILE ( Condition To Check Returns True ) { Action }由于即使 Internet 可 ping,您也不想禁用和启用 NIC,因此您可以使用WHILE ( Condition To Check Returns True ) { Action }

$NIC_Original_State = get-NetAdapter | ? {$_.Status -eq "Enabled"}

While ( ! $( test-connection -ComputerName 4.2.2.2 -Quiet ) ) {
  $NIC_Original_State | Disable-NetAdapter -name $_.name -confirm:$false
  Sleep 5
  $NIC_Original_State | Enable-NetAdapter -name $_.name -confirm:$false
  Sleep 5
}

You absolutely need to have a scheduled task to run this regularly.你绝对需要有一个计划任务来定期运行它。

Even if you want to only start it once and leave it in the background, and havit checking always (which we could do) you'll need to set up a scheduled task to kill it if still running and start it again at some interval as it's possible to have a process become unresponsive or be killed or have the computer restart etc and you forget to start the script etc.即使您只想启动一次并将其留在后台,并且始终进行检查(我们可以这样做),您也需要设置一个计划任务以在仍在运行时将其杀死并在某个时间间隔重新启动它可能有一个进程变得无响应或被杀死或让计算机重新启动等,而您忘记启动脚本等。

This version will just loop every one minute to check, and only executes when the internet is not reachable.此版本将每隔一分钟循环检查一次,并且仅在无法访问 Internet 时执行。

while ($true) {
  $NIC_Original_State = get-NetAdapter | ? {$_.Status -eq "Enabled"}

  While ( ! $( test-connection -ComputerName 4.2.2.2 -Quiet ) ) {
    $NIC_Original_State | Disable-NetAdapter -name $_.name -confirm:$false
    Sleep 5
    $NIC_Original_State | Enable-NetAdapter -name $_.name -confirm:$false
    Sleep 5
  }
 sleep 60
}

Again, you ca just run this in powershell, sure, btu then if it gets stopped for any reason it won;t be started again.同样,您可以在 powershell 中运行它,当然,btu 如果它因任何原因停止,它将不会再次启动。

Use task scheduler ad set up an action to run this o Startup and once an hour, killing any previously running copy of the script.使用任务调度程序广告设置一个操作来运行这个 o 启动并每小时运行一次,杀死任何以前运行的脚本副本。

Usually for running a CMD script I just write a simple cmd script so if I want to edit any part of anything I only change the script never the task.通常为了运行 CMD 脚本,我只写一个简单的 cmd 脚本,所以如果我想编辑任何东西的任何部分,我只更改脚本而不是任务。

<# ## & REM Script Name:
@(
    SETLOCAL ENABLEDELAYEDEXPANSION
    ECHO OFF
    (
        NET SESSION 2>&1 >NUL
    ) || (
        powershell.exe -Command "Start-Process cmd \"/k %~dpnx0\" -Verb RunAs"
        pause
        GOTO :EOF
    )
    IF EXIST "%~dpn0.ps1" (
        DEL /Q /F %~dpn0.ps1"
    )
    MKLINK /H "%~dpn0.ps1" "%~f0"
)

Powershell.exe -ExecutionPolicy Bypass -File "%~dpn0.ps1"

(
  ENDLOCAL
  EXIT /B %_eLvl%
)

#>

## Script: 
## Powershell Portion Begins

while ($true) {
  $NIC_Original_State = get-NetAdapter | ? {$_.Status -eq "Enabled"}

  While ( ! $( test-connection -ComputerName 4.2.2.2 -Quiet ) ) {
    $NIC_Original_State | Disable-NetAdapter -name $_.name -confirm:$false
    Sleep 5
    $NIC_Original_State | Enable-NetAdapter -name $_.name -confirm:$false
    Sleep 5
  }
 sleep 60
}


声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM