使用变量进行Ping


0

我正在尝试创建一个批处理文件,该文件将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

看起来你的参数顺序错了。
Daniel B

这是我的最新消息:@echo off set / p UserInputPath =“输入选择的PC:”PAUSE ping%UserInputPath%。store.domain.company.com ping MyServerName PAUSE 10.88.132.72
Alex

请用 正确的格式 在写问题或答案时。
Daniel B

@DanielB对不起
Alex

当你使用 ping %UserInputPath%.store.domain.company.com,用户输入究竟是什么?如果你放了一个 echo %UserInputPath%.store.domain.company.com 什么打印?
Karan

Answers:


2
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"

不敢相信我没看到....你绝对的生命保护!
Alex

1

以下脚本工作,至少对我来说是Win7。

@echo off
set /p name="Enter name:"
ping %name%.google.com

我们首先要求用户输入名称,然后将其存储 name 变量,并将其传递给 ping (看到 %name% )添加 google.com (就像例子一样!)。


我试过并得到此错误:Ping请求找不到主机.store.domain.company.com请检查名称并重试。
Alex

Alex,确保您的域名可以直接从命令行ping。
user996142

@ user996142 - 我可以通过命令行ping机器没有问题
Alex

试试吧 ping %name% 并输入完整的域名
user996142

不幸的是,这不起作用......
Alex

0

这是一个花哨的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

-1

看起来你确实陷入了困境 延迟扩张陷阱 并且您的代码段被包含在其中 ()。当您使用在这样的块内部更改的变量时,您需要 启用延迟扩展 ...

@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
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.