如何以固定顺序而不是随机地连接到bat文件中列出的服务器?

发布于 2025-01-16 22:46:55 字数 1247 浏览 2 评论 0原文

该bat文件一键连接35台服务器,但它们是随机连接的,而不是我在bat中提到的。我需要连接第一台服务器,我在蝙蝠上列出了该服务器,但它们无法连接。如果您能给我任何帮助,我将不胜感激

start cmdkey /generic:"10.151.12.201" /user:"nas" /pass:"itti@123$"
start cmdkey /generic:"10.151.13.201" /user:"nas" /pass:"itti@123$"
start cmdkey /generic:"10.151.27.11" /user:"nas" /pass:"itti@123$"
start cmdkey /generic:"10.151.28.11" /user:"nas" /pass:"itti@123$"
start cmdkey /generic:"10.151.29.11" /user:"nas" /pass:"itti@123$"
start cmdkey /generic:"10.151.31.11" /user:"nas" /pass:"itti@123$"
start cmdkey /generic:"10.151.32.11" /user:"nas" /pass:"itti@123$"
start cmdkey /generic:"10.151.33.11" /user:"nas" /pass:"itti@123$"
start cmdkey /generic:"10.151.35.11" /user:"nas" /pass:"itti@123$"
start cmdkey /generic:"10.151.36.11" /user:"nas" /pass:"itti@123$"

start mstsc /admin /w:1600 /v:"10.151.12.201"
start mstsc /admin /w:1600 /v:"10.151.13.201"
start mstsc /admin /w:1600 /v:"10.151.27.11"
start mstsc /admin /w:1600 /v:"10.151.28.11"
start mstsc /admin /w:1600 /v:"10.151.29.11"
start mstsc /admin /w:1600 /v:"10.151.31.11"
start mstsc /admin /w:1600 /v:"10.151.32.11"
start mstsc /admin /w:1600 /v:"10.151.33.11"
start mstsc /admin /w:1600 /v:"10.151.35.11"
start mstsc /admin /w:1600 /v:"10.151.36.11"

The bat file connects 35 servers at one click, but they connect randomly instead of what I mention in the bat. I need to connect the first server, which I list on the bat, but they won't connect. I would appreciate any help you can give me

start cmdkey /generic:"10.151.12.201" /user:"nas" /pass:"itti@123
quot;
start cmdkey /generic:"10.151.13.201" /user:"nas" /pass:"itti@123
quot;
start cmdkey /generic:"10.151.27.11" /user:"nas" /pass:"itti@123
quot;
start cmdkey /generic:"10.151.28.11" /user:"nas" /pass:"itti@123
quot;
start cmdkey /generic:"10.151.29.11" /user:"nas" /pass:"itti@123
quot;
start cmdkey /generic:"10.151.31.11" /user:"nas" /pass:"itti@123
quot;
start cmdkey /generic:"10.151.32.11" /user:"nas" /pass:"itti@123
quot;
start cmdkey /generic:"10.151.33.11" /user:"nas" /pass:"itti@123
quot;
start cmdkey /generic:"10.151.35.11" /user:"nas" /pass:"itti@123
quot;
start cmdkey /generic:"10.151.36.11" /user:"nas" /pass:"itti@123
quot;

start mstsc /admin /w:1600 /v:"10.151.12.201"
start mstsc /admin /w:1600 /v:"10.151.13.201"
start mstsc /admin /w:1600 /v:"10.151.27.11"
start mstsc /admin /w:1600 /v:"10.151.28.11"
start mstsc /admin /w:1600 /v:"10.151.29.11"
start mstsc /admin /w:1600 /v:"10.151.31.11"
start mstsc /admin /w:1600 /v:"10.151.32.11"
start mstsc /admin /w:1600 /v:"10.151.33.11"
start mstsc /admin /w:1600 /v:"10.151.35.11"
start mstsc /admin /w:1600 /v:"10.151.36.11"

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

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

发布评论

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

评论(2

睫毛上残留的泪 2025-01-23 22:46:55

为了让生活稍微轻松一点,您实际上只需要使用 for 循环一次命令,并且仅将元变量传递给命令。在本例中,元变量 %%i 是一个临时变量,保存当前值,直到 do 之后的所有命令完成,它将把下一个值分配给 < code>%%i

然后,根据您的具体要求(并非 100% 清楚),有几个选项:

在每个命令之间添加超时,以确保它在下一个命令之前启动:

@echo off
set "range=10.151.12.201 10.151.13.201 10.151.27.11 10.151.28.11 10.151.29.11 10.151.31.11 10.151.32.11 10.151.35.11 10.151.36.11"
for %%i in (%range%) do (
   start "" cmdkey.exe /generic:"%%i" /user:"nas" /pass:"itti@123$"
   start "" mstsc.exe /admin /w:1600 /v:"%%i"
   timeout /t 1
)

但这将需要您由于延迟,每个远程会话需要 35 秒启动,因此接下来是强迫他们使用条件运算符。原理很简单 && 将告诉系统仅启动下一个,如果上一个成功或 %errorlevel%exitcode不大于 0。

因此我们使用相同的循环来构建宏,然后启动宏。

@echo off & set mac_cmdkey= & set mac_mstsc=
setlocal enabledelayedexpansion
set "range=10.151.12.201 10.151.13.201 10.151.27.11 10.151.28.11 10.151.29.11 10.151.31.11 10.151.32.11 10.151.35.11 10.151.36.11"
for %%i in (%range%) do (
   set "mac_cmdkey=!mac_cmdkey! start "" cmdkey.exe /generic:"%%i" /user:"nas" /pass:"itti@123$" &&"
   set "mac_mstsc=!mac_mstsc! start "" mstsc.exe /admin /w:1600 /v:"%%i" &&"
)
set "mac_cmdkey=!mac_cmdkey! echo cmdkey Done"
set "mac_mstsc=!mac_mstsc! echo mstsc Done"
%mac_cmdkey%
%mac_mstsc%

最后,如果打算一次打开一个,执行某些操作,然后关闭并启动下一个,请使用 start "" /wait

@echo off
set "range=10.151.12.201 10.151.13.201 10.151.27.11 10.151.28.11 10.151.29.11 10.151.31.11 10.151.32.11 10.151.35.11 10.151.36.11"
for %%i in (%range%) do (
   start "" /wait cmdkey.exe /generic:"%%i" /user:"nas" /pass:"itti@123$"
   start "" /wait mstsc.exe /admin /w:1600 /v:"%%i"
   timeout /t 1
)

To ease life a bit, you really only need the commands once off by utilizing a for loop and only passing the meta variables to the commands. The meta variable, %%i in this instance, is a temp variable holding the current value until all the commands after do is completed where it will assign the next value to %%i

Then, depending on your exact requirement, which is not 100% clear there are a few options:

add a timeout between each command to ensure it starts up before the next one:

@echo off
set "range=10.151.12.201 10.151.13.201 10.151.27.11 10.151.28.11 10.151.29.11 10.151.31.11 10.151.32.11 10.151.35.11 10.151.36.11"
for %%i in (%range%) do (
   start "" cmdkey.exe /generic:"%%i" /user:"nas" /pass:"itti@123
quot;
   start "" mstsc.exe /admin /w:1600 /v:"%%i"
   timeout /t 1
)

This however will take you 35 seconds to start up each remote session because of the delay, so the next would be to force them using conditional operators. Principal is simple && will tell the system to only start up the next, if the previous was successful or %errorlevel% or exitcode is not larger than 0.

So we use the same loop to build macros and and then launch the macros.

@echo off & set mac_cmdkey= & set mac_mstsc=
setlocal enabledelayedexpansion
set "range=10.151.12.201 10.151.13.201 10.151.27.11 10.151.28.11 10.151.29.11 10.151.31.11 10.151.32.11 10.151.35.11 10.151.36.11"
for %%i in (%range%) do (
   set "mac_cmdkey=!mac_cmdkey! start "" cmdkey.exe /generic:"%%i" /user:"nas" /pass:"itti@123
quot; &&"
   set "mac_mstsc=!mac_mstsc! start "" mstsc.exe /admin /w:1600 /v:"%%i" &&"
)
set "mac_cmdkey=!mac_cmdkey! echo cmdkey Done"
set "mac_mstsc=!mac_mstsc! echo mstsc Done"
%mac_cmdkey%
%mac_mstsc%

Lastly, if the intension was to open one at a time, do something, then close and start the next, use start "" /wait

@echo off
set "range=10.151.12.201 10.151.13.201 10.151.27.11 10.151.28.11 10.151.29.11 10.151.31.11 10.151.32.11 10.151.35.11 10.151.36.11"
for %%i in (%range%) do (
   start "" /wait cmdkey.exe /generic:"%%i" /user:"nas" /pass:"itti@123
quot;
   start "" /wait mstsc.exe /admin /w:1600 /v:"%%i"
   timeout /t 1
)
独享拥抱 2025-01-23 22:46:55

要按顺序运行命令,请将 /W/WAIT 添加到每一行。

start "" /WAIT cmdkey /generic:"10.151.12.201" /user:"nas" /pass:"itti@123$"
start "" /WAIT mstsc /admin /w:1600 /v:"10.151.12.201"

在批处理脚本中,不带 /wait 的 START 命令将运行程序并继续,因此仅包含 START 命令的脚本将关闭 CMD 控制台并让新程序继续运行。

START 的默认行为是实例化一个与主进程并行运行的新进程。

/WAIT 选项应反转 START 的默认“并行运行”行为

请参阅 https://ss64.com /nt/start.html

To run your commands in sequential order, add the /W or /WAIT to each line.

start "" /WAIT cmdkey /generic:"10.151.12.201" /user:"nas" /pass:"itti@123
quot;
start "" /WAIT mstsc /admin /w:1600 /v:"10.151.12.201"

In a batch script, a START command without /wait will run the program and just continue, so a script containing nothing but a START command will close the CMD console and leave the new program running.

The default behaviour of START is to instantiate a new process that runs in parallel with the main process.

The /WAIT option should reverse the default 'run in parallel' behaviour of START

See https://ss64.com/nt/start.html

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