
我正在嘗試創建一個批次文件,該文件將 ping 由用戶輸入確定的變量
例如「輸入電腦名稱:」電腦名稱=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