How to do ping `whoami`.google.com in windows? - windows

I want to ping a server with the input of the first command being the the subdomain for the ping command.
In a simple term (Linux way of doing the same thing).
ping `whoami`.google.com
Now I want the same thing in windows CMD. How can I do this?

You would have to parse the WhoAmI from Windows in a FOR /F Loop then use the FOR /F Loop variable in a Ping Command as part of the FOR /F Loop's DO Clause.
You can Dump this directly into CMD:
FOR /F "Delims=\" %_ IN ('
WhoAmI
') DO (
Ping %_.Google.com
)
Or as a single line (which is easier to re-run by hitting the UP arrow)
FOR /F "Delims=\" %_ IN ('WhoAmI') DO ( Ping %_.Google.com )
Assuming your domain is "Contoso.com" the Result is you ping Contoso.Google.com If you domain is "Adventureworks.Contoso.com" you ping AdventureWorks.Google.com

Related

Windows: How to programatically connect to wireless networks?

With Windows 10 is it possible to setup up known networks and be able to connect to them without all the mouse movement and click?
Using Windows batch files, you can set it up to connect to networks you already know (Network1 or Network2, below) without ever touching the mouse.
#echo off
setlocal EnableDelayedExpansion
for %%i in ("Network1"
"Network2") do (
netsh wlan show networks mode=ssid | findstr /C:%%i
if !ERRORLEVEL! EQU 0 (
echo "Found %%~i - connecting..."
netsh wlan connect name=%%i
exit /b
) else (
echo "Did not find %%~i"
)
)
#echo on
Save the above to .bat and run it from cmd.exe or a program like Listary.
Some comments about the code:
If more than one of your listed networks are available, it will connect to whichever is first in the for loop list. You could also put the list in a file and change for %%i to for /F %%i
EnableDelayedExpansion and "!" around ERRORLEVEL
are needed to keep the variable ERRORLEVEL from being assigned
whatever it was at the beginning of the script. Since I don't
normally program Windows batch files, this is 2 hours of my life
gone that you won't have to deal with.
All the echoing is for debugging; the echo off at the top squelches it.
%% needed for variables in Windows batch files. The variable is referenced with % at the command line.
%%~i strips the quotation marks around the string when outputting to stdout.

Is there any way to run a script for Windows that allows ping -a from a ping result?

Good Morning,
I'm looking for a way to speed up my work with a batch file and I was wondering if anyone can help. What I'm looking for is a script to run on Command Prompt that will perform a ping on a computer name and then use the resulting ip address of that ping in a ping -a command.
At best all I can think to do is this, despite knowing that it's wrong:
#echo off
set /p asset="Enter asset: "
ping %asset%
read result
ping %result%
#pause
Any help is gratefully received.
The usual way of reading the output of a command is for/f. Choosing the correct delimiters and the right token gets you exactly what you want:
for /f "tokens=2 delims=[]" %%i in ('ping -n 1 -4 %asset%') do set result=%%i

Cannot use PStool for more than 7 PCs over LAN Network

Below is the batch file command I am currently using to shutdown the remote computers through LAN Network.
Here is what it does.
I have specified the remote computers IP address in text file named
list.txt
I have added an IP as 0.0.0.0 at the bottom of all the remote
computer IPs.
The below batch file will check if the computers are available over
LAN.
If the computer is available it will shutdown the remote PC else it
will pass on to next IP.
When the batch file reads 0.0.0.0 at last it will self shutdown the
master computer.
My problem is I cannot run this batch for more than 7 remote computers. If I add more than 7 remote PC IP in list.txt the batch file hangs and action does not complete. Please let me know if i made any mistake in the code or How i can fix this issue.
I want to run this batch file for minimum of 12 remote PCs
#echo off
setlocal enableextensions enabledelayedexpansion
for /f %%a in (C:\Users\calcopm\Desktop\list.txt) do (
SET IP =%%a
SET C=0
IF %%a equ 0.0.0.0 (
shutdown /s
) ELSE (
ping -n 1 %%a | find "TTL=" >NUL: && SET C=1
IF !C! equ 1 (
psshutdown \\%%a
) else (
ECHO REMOTE %%a IS NOT REACHABLE
)
)
)
I changed my script as below and converted from BAT to EXE using an application
#echo off
setlocal enableextensions enabledelayedexpansion
for /f %%a in (C:\Users\calcopm\Desktop\list.txt) do (
IF %%a equ 0.0.0.0 (
shutdown /s
) ELSE (
ping -n 1 -w 100
IF errorlevel 1 (
ECHO REMOTE %%a IS NOT REACHABLE
) else (
psshutdown \\%%a
)
)
)
Still I was facing the same problem. As I was running the scripts using exe file(converted using BAT to EXE), I executed using the BAT file it was fine.Then I realised that the BAt to EXE converter had some issues which was affecting the EXE file inturn.
Then I converted BAt to EXE with different application and IT WORKED LIKE A CHARM.
I solved the issue ATLAST!!!!!!!!!!!!! phew!!!!!

Redirect command as well as output to text file in Windows CLI?

I need to redirect both the command as well as its output to a text file in Windows CLI. For instance, I am running the nslookup command on a subnet using a FOR loop,
for /L %i IN (1,1,254) DO nslookup 192.168.1.%i >> nslookup.txt
However, this only redirects the output of the command.
Is there a way to redirect both the command as well as the output to a text file? Please do not tell me about clip and select all/copy commands.
You can proceed the command with "cmd /c" to start a new command prompt, and redirect the output of the command prompt:
cmd /c for /L %i IN (1,1,254) DO nslookup 192.168.1.%i > nslookup.txt
Note that you only need to use a single greater than (>) since the output of cmd is going to nslookup.txt. Sadly, this misses the error output, so you are not seeing the ***Request to UnKnown timed-out for each failed address.
Your FOR loop is right on and it sounds like you are already getting the output you want, so all you need to do is ECHO the command before running it:
for /L %i IN (1,1,254) DO ECHO nslookup 192.168.1.%i&nslookup 192.168.1.%i >> nslookup.txt
The & chains the commands together so the ECHO is run before the nslookup.
If you want to use a batch file, it becomes a bit more clear:
#ECHO OFF
SETLOCAL EnableDelayedExpansion
SET Outfile=nslookup.txt
REM Log the date/time.
ECHO %DATE% - %TIME%>%Outfile%
FOR /L %%i IN (1,1,254) DO (
SET Command=nslookup 192.168.1.%%i
REM Print the command being run.
ECHO !Command!>>%Outfile%
REM Run the command.
!Command!>>%Outfile%
)
ENDLOCAL
for /L %i IN (1,1,254) DO (#echo nslookup 192.168.1.%i & nslookup 192.168.1.%i) >> nslookup.txt
This works. Still I'm sure there are smarter ways to do this.

Windows: Obtaining and Storing a Machine Name in a Variable

I have a script that is run over a network, with VPN being the same as a LAN environment.
The script previously worked fine, as we had variables that stored the username and password for the administrator. However, due to a recent change, when we map a drive over the network and whatnot, the machine name is now needed in front of the administator username, E.g. machinename2343\administrator.
What I would like to do is take an existing command - perhaps such as nbtstat - and after entering the ip address, have the program pull the machine name and insert it into a variable.
I have found that Nbtstat can give me the machine name, but provides large amounts of unnecessary information for my task. Is there a way to filter out just the machine name in a reliable and consistent manner, or is there perhaps another network related command that perform in the same capacity?
`#echo off
FOR /f "tokens=1* delims= skip=23 " %%a IN ('nbtstat -a IPADDRESS) DO (
SET VARIABLE=%%a
GOTO Done
)
:Done
echo Computer name: %VARIABLE%`
You could do ping /a. The computer name is resolved. And this computer name is the second token. I haven't taken care of Error checking. I believe you could implement that yourself.
Try this:
#ECHO OFF
FOR /f "tokens=2* delims= " %%a IN ('PING -a -n 1 IPADDRESS') DO (
SET Variable=%%a
GOTO Done
)
:Done
echo Computer name: %Variable%
Put this in your batch file where it would fit.
You could just use the %computername% environment variable.
When I first read your post I thought you were running the batch file remotely on each machine. If that were the case having %computername% in the batch file would work, because when the batch file is executed remotely %computername% would be expanded based on the remote machine's environment variable not the local machine.
Looking back on it, it's still not very clear, but based on your comment I assume the batch file is running locally and then connecting to a set of machines to perform some operation(s).
You could use tool the WMI command-line tool to get the computer name. The solution would look similar to #Thrustmaster's, but I think it's a little cleaner since the output of wmic, in this case, does "filter out just the machine name in a reliable consistent manner." Of course you'd replace the 127.0.0.1 with the ip you want to query.
#ECHO OFF
FOR /F "tokens=*" %%A IN ('wmic /node:127.0.0.1 ComputerSystem Get Name /Value ^| FIND "="') DO (
SET COMP.%%A
)
ECHO %COMP.NAME%

Resources