Board logo

标题: [网络连接] 【已解决】新人求教批处理指定ping, [打印本页]

作者: 深蓝幻灭    时间: 2022-3-9 13:47     标题: 【已解决】新人求教批处理指定ping,

本帖最后由 深蓝幻灭 于 2022-3-10 22:46 编辑

想写个批处理,作用:ping指定IP,通则运行1.EXE,并关闭批处理。
如果不通就提示[网络故障并注明错误次数],再重新ping。
连续10次没有ping通,提示网络故障,关闭批处理
PS:公司经常需要切换内外网,有些妹子比较憨,就想写个批处理,大佬帮帮我

之前自己写了个,各种问题,干脆删了,免得影响大佬们看:'(
作者: Batcher    时间: 2022-3-9 13:57

回复 1# 深蓝幻灭


    这个代码实现的功能和你的期望之间有哪些差异?
作者: 深蓝幻灭    时间: 2022-3-9 14:01

本帖最后由 深蓝幻灭 于 2022-3-9 14:11 编辑

回复 2# Batcher


ping通,会提示连接成功,1.exe会运行,但批处理不会自动关闭。
ping不通 批处理反而关闭了,不会重复ping
另外希望把重复ping的提示改成依次递增的数字,1.2.3这样。
重复10次后,不再ping,并关闭批处理
作者: Batcher    时间: 2022-3-9 14:59

回复 3# 深蓝幻灭


把第12行改成这样试试能否解决第1个问题:
  1. ping -n 1 192.168.1.22 >nul 2>nul
  2. if errorlevel 1 (
  3.     echo 网络不通请稍等
  4. ) else (
  5.     echo 连接成功
  6.     start "" "D:\1.exe"
  7.     goto :eof
  8. )
复制代码

作者: 深蓝幻灭    时间: 2022-3-9 15:10

回复  深蓝幻灭


把第12行改成这样试试能否解决第1个问题:
Batcher 发表于 2022-3-9 14:59



12行换成这样,ping通后会运行1.exe,批处理也会关闭。
但是不通也会运行1.exe,批处理也关闭了,不会重新ping
作者: Fit    时间: 2022-3-10 21:02

@echo off
set "ip=192.168.1.1"
set "inter=False"
set /a cout=0

:StartPing
set /a cout+=1
echo,第(%cout%)次测试 ip【%ip%】是否连通!
ping -n 1 "%ip%" | find "TTL" 1>nul 2>nul && set "inter=True"
if "%inter%"=="False" (
        if %cout% equ 10 (
                echo,***********************************
                echo,ip【%ip%】网络无法联通
                echo,***********************************
                goto:end
        )
        echo,------- 失败 -------
        goto StartPing
)else (
        echo,***********************************
        echo,ip【%ip%】网络通畅!!!!
        echo,***********************************
        goto:end
)
:end
pause&exit
作者: 深蓝幻灭    时间: 2022-3-10 22:46

@echo off
set "ip=192.168.1.1"
set "inter=False"
set /a cout=0

:StartPing
set /a cout+=1
ech ...
Fit 发表于 2022-3-10 21:02



    感谢大佬,疯狂学习




欢迎光临 批处理之家 (http://bbs.bathome.net/) Powered by Discuz! 7.2