
ユーザーの入力によって決定される変数をpingするバッチファイルを作成しようとしています
例: 「PC名を入力してください:」pc name =123
次に123.domain.comにpingします
@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