我正在尝试创建一个批处理文件,它将 ping 一个由用户输入决定的变量
例如“输入 PC 名称:”pc name =123
然后它 ping 123.domain.com
@echo off
set /p UserInputPath = Enter chosen PC:
ping %UserInputPath%
ping MyServerName
PAUSE
Ping MyServerName
工作正常
但ping %UserInputPath%
没有,只是在 CMD 中调出 Ping 的“帮助”菜单
任何帮助都将不胜感激::::编辑:::
ping %UserInputPath% -n 5
返回此错误:IP address must be specified.
您无法 ping 主机名吗?(因为这就是我正在尝试做的)
编辑2::
这是我的最新动态:
@echo off
set /p UserInputPath = "Enter chosen PC: "
PAUSE
ping %UserInputPath%.store.domain.company.com
ping MyServerName
PAUSE
答案1
set /p UserInputPath = Enter chosen PC:
^ This space is included in the name of the variable
因此你最终得到一个名为%UserInputPath %
更好地利用
set /p "UserInputPath=Enter Chosen PC: "
ping "%UserInputPath%.store.domain.company.com"
答案2
以下脚本有效,至少对于我在 Win7 上来说是这样。
@echo off
set /p name="Enter name:"
ping %name%.google.com
我们首先要求用户输入姓名,然后将其存储在name
变量中,并将其传递给ping
(参见%name%
)添加google.com
(仅作为示例!)。
答案3
这是给你的一个很棒的 bash 脚本。今天早些时候,我为原始问题编写了它,后来才意识到它是针对 Windows 的。考虑过将它转换为批处理文件,但看起来很麻烦。这让我想起了在大学时编写启动脚本。
您需要使用 Powershell 或在某处启动 *nix 主机。Powershell v3 确实很棒。实际上,您可以相当轻松地将其转换为 powershell。
这会招致一些反对票,但谁在乎呢。有人会发现这个脚本很有用。至少你可以窃取逻辑和正则表达式。
在 Debian 中测试。
#!/bin/bash
# ------------------------------------------------------------------
# superuserping.sh # Can be changed :P
# /usr/bin/superuserping.sh # Put it wherever you like...
# ------------------------------------------------------------------
# Usage: ./superuserping.sh [fqdn|shortname|ip]
# If no argument is passed it will ask for one
# Author: Alex Atkinson
# Author Date: May 25, 2015
# ------------------------------------------------------------------
# VARIABLES
# ------------------------------------------------------------------
domain="domain.com"
rxshort='^[A-Za-z0-9]{1,63}$'
rxfqdn='^([A-Za-z0-9-]{1,63}\.)+[A-Za-z]{2,6}$'
rxip='^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$'
# ------------------------------------------------------------------
# Check for argument. Get one if none found.
# ------------------------------------------------------------------
if [ -z $1 ]; then
echo -n "Enter the hostname or IP to ping and press [ENTER]: "
read host
else
host=$1
fi
# ------------------------------------------------------------------
# ARGUMENT VALIDATION
# ------------------------------------------------------------------
checkshort=$([[ $host =~ $rxshort ]])
checkshort=$?
checkfqdn=$([[ $host =~ $rxfqdn ]])
checkfqdn=$?
checkip=$([[ $host =~ $rxip ]])
checkip=$?
# ------------------------------------------------------------------
# FUNCTIONS
# ------------------------------------------------------------------
function check_userinput()
{
# Validate userinput against shortname, fqdn, and IP regex. If shortname then add domain.
if [[ $checkshort == '0' ]] || [[ $checkfqdn == "0" ]] || [[ $checkip == "0" ]] ; then
if [[ $checkip == 1 ]]; then
if [[ $host != *$domain ]]; then
host=$host.$domain
fi
fi
else
echo -e "\e[00;31mERROR\e[00m: ERROR:" $host "does not appear to be a valid shortname, fqdn, or IP."
exit 1
fi
}
function resolve_host()
{
# Check for DNS host resolution.
dnscheck=$(host $host)
if [[ $? -eq 0 ]]; then
echo -e "\n"$dnscheck "\n"
else
echo -e "\n\e[00;31mERROR\e[00m: DNS was unable to resolve the provided hostname or IP:" $host".\n"
echo -n "Press [ENTER] key to continue with ping, or [Q] to quit..."
read -n 1 -s key
if [[ $key = q ]] || [[ $key = Q ]]; then
echo -e "\nExiting...\n"
exit 1
fi
fi
}
# ------------------------------------------------------------------
# MAIN OPERATIONS
# ------------------------------------------------------------------
check_userinput
resolve_host
ping $host
答案4
看起来你确实陷入了延迟扩张陷阱并且您的代码片段包含在(
和之间)
。当您在这样的块中使用更改的变量时,您需要启用延迟扩展...
@echo off
SETLOCAL enableextensions enabledelayedexpansion
set "UserInputPath=AnotherServer"
(
set "UserInputPath=MyServerName"
rem set /p "UserInputPath = Enter chosen PC: "
echo 1st percent-expansion %UserInputPath%
echo 1st delayedexpansion !UserInputPath!
)
echo(
echo 2nd percent-expansion %UserInputPath%
echo 2nd delayedexpansion !UserInputPath!
输出:
1st percent-expansion AnotherServer
1st delayedexpansion MyServerName
2nd percent-expansion MyServerName
2nd delayedexpansion MyServerName