gpt4 book ai didi

windows - 使用批处理文件遍历列表

转载 作者:可可西里 更新时间:2023-11-01 11:24:20 24 4
gpt4 key购买 nike

我正在尝试编写一个批处理文件来自动重启服务器上的一些 IP 摄像机。

我已经成功地通过 ftp 获得了一个批处理文件来重启一台相机,但我无法理解如何为范围内的每个 ip 执行此操作。

我目前有:

open IP
user
pass
quote site reboot
quote site reboot
quit
echo

保存在 .txt 文件 (ftp.txt) 中,我使用 .bat 文件调用它:

@echo off
set user = Username
set pass = Password
ftp -s:ftp.txt

我想要一个使用过的IP地址列表,然后让bat脚本一个一个地执行重启,等待2分钟再进行下一个。

我想我需要将列表放在 .txt 文件中,并在 bat 中使用 for 循环。

有点像

set ip = iplist(??)  <-- Part causing confusion
for each ip in ftp.txt
open ip
user
pass
quote site reboot
quote site reboot
pause 2 min
quit

如有任何帮助,我们将不胜感激!

最佳答案

这是我的做法:

  • 将所有相机 IP 存储在名为 cameras.txt 的文件中(一个 IP每行)

  • 将您的批处理文件 (script.bat) 修改为:

    @echo off

    for /F %%f in (cameras.txt) do (
    ftp -s:ftp.txt %%f
    )
    • 注意事项:
      1. 我删除了 2 个环境变量(userpass)设置,因为它们根本没有被使用
      2. > [SS64]: FOR /F用于遍历 cameras.txt 文件的每一行
      3. 注意 ftp 的额外参数(它是 IP)
  • 由于第 3 个rd 注释,IP 作为参数传递给 ftp,因此 ftp.txt 文件,因此您应该删除第 1st 行 (open IP)

发布其他文件内容。

cameras.txt:

mirrors.kernel.org
ftp.kr.freebsd.org

ftp.txt:

anonymous
pass
quote site reboot
quote site reboot
quit
echo

输出:

e:\Work\Dev\StackOverflow\q045327836>script.bat
Connected to mirrors.pdx.kernel.org.
220 Welcome to mirrors.kernel.org.
200 Always in UTF8 mode.
User (mirrors.pdx.kernel.org:(none)):
331 Please specify the password.

230 Login successful.
ftp> quote site reboot
550 Permission denied.
ftp> quote site reboot
550 Permission denied.
ftp> quit
221 Goodbye.
Connected to daemon.kr.freebsd.org.
220 daemon.kr.freebsd.org FTP server (Version 6.00LS) ready.
500 OPTS UTF8 ON: command not understood.
User (daemon.kr.freebsd.org:(none)):
331 Guest login ok, send your email address as password.

230 Guest login ok, access restrictions apply.
ftp> quote site reboot
500 SITE REBOOT: command not understood.
ftp> quote site reboot
500 SITE REBOOT: command not understood.
ftp> quit
221 Goodbye.

我认为这就是您所追求的。

关于windows - 使用批处理文件遍历列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45327836/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com