
Estoy intentando crear un archivo por lotes que hará ping a una variable determinada por la entrada de los usuarios.
por ejemplo, "Ingrese el nombre de la PC:" nombre de la PC =123
Luego hace ping a 123.dominio.com
@echo off
set /p UserInputPath = Enter chosen PC:
ping %UserInputPath%
ping MyServerName
PAUSE
Ping MyServerName
funciona bien
Pero ping %UserInputPath%
no es así y simplemente muestra el menú "Ayuda" para Ping en CMD
Cualquier ayuda sería apreciada ::::EDITAR:::
ping %UserInputPath% -n 5
Devuelve este error:IP address must be specified.
¿No puedes hacer ping a un nombre de host? (COMO eso es lo que estoy tratando de hacer)
EDITAR 2::
Este es mi último:
@echo off
set /p UserInputPath = "Enter chosen PC: "
PAUSE
ping %UserInputPath%.store.domain.company.com
ping MyServerName
PAUSE
Respuesta1
set /p UserInputPath = Enter chosen PC:
^ This space is included in the name of the variable
Entonces terminas con una variable llamada%UserInputPath %
Mejor uso
set /p "UserInputPath=Enter Chosen PC: "
ping "%UserInputPath%.store.domain.company.com"
Respuesta2
El siguiente script funciona, al menos para mí en Win7.
@echo off
set /p name="Enter name:"
ping %name%.google.com
Primero le pedimos al usuario que ingrese el nombre, luego lo almacenamos en name
una variable y lo pasamos a ping
(ver %name%
) agregar google.com
(¡solo como ejemplo!).
Respuesta3
Aquí tienes un script bash elegante. Lo corté hoy para la pregunta original antes de darme cuenta de que era para Windows. Consideré convertirlo en un archivo por lotes, pero parecía doloroso. Me recordó a escribir guiones de inicio en la universidad.
Necesita usar Powershell o activar un host *nix en alguna parte. Powershell v3 es realmente bueno. En realidad, podrías convertir esto en PowerShell con bastante facilidad.
Habrá algunos votos negativos para esto, pero a quién le importa. Alguien encontrará útil este script. Como mínimo, puedes saquear la lógica y las expresiones regulares.
Probado en 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
Respuesta4
Parece que caíste en eltrampa de expansión retardaday su fragmento de código está entre (
y )
. Cuando usa una variable cambiada dentro de dicho bloque, entonces necesitapermitir la expansión retrasada...
@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!
Producción:
1st percent-expansion AnotherServer
1st delayedexpansion MyServerName
2nd percent-expansion MyServerName
2nd delayedexpansion MyServerName