변수를 사용한 ping

변수를 사용한 ping

사용자 입력에 따라 결정되는 변수를 ping하는 배치 파일을 만들려고 합니다.

예: "PC 이름 입력:" PC 이름 =123

그런 다음 123.domain.com으로 핑을 보냅니다.

@echo off
set /p UserInputPath = Enter chosen PC: 

ping %UserInputPath%

ping MyServerName

PAUSE

Ping MyServerName잘 작동해요

하지만 ping %UserInputPath%그렇지 않고 CMD에서 Ping에 대한 "도움말" 메뉴만 표시됩니다.

어떤 도움이라도 주시면 감사하겠습니다 ::::EDIT:::

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로 쉽게 변환할 수 있습니다.

이것에 대해 몇 가지 반대표가 있지만 누가 신경 쓰겠습니까? 누군가는 이 스크립트가 유용하다고 생각할 것입니다. 최소한 논리와 정규식을 약탈할 수 있습니다.

데비안에서 테스트되었습니다.

#!/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

관련 정보