I'm trying to write a batch script for windows XP. It needs to build an IP address from ipconfig by getting the (192.168.XXX.30) XXX part of the address and supplanting it into a template. Then some fun stuff from a network shared folder.
SET _var=ipconfig |FIND "192.168"
SET _var=%_var:~25,-4%
net use z: \\192.168.%_var%.30\test_folder
... Do stuff
net use z: \DELETE
ECHO "Tasks completed"
PAUSE
At the moment I can't seem to get the result from ipconfig in to my variable, would be even better if I could get the substring from it in 1 line.
Any ideas?
The first task, per your question, is to get the IP address. To get one from www.google.com:
for /f "tokens=2 delims=[]" %f in ('ping -4 -n 1 www.google.com ^|find /i "pinging"') do echo IP=%f
Say the resulting value of IP is now 192.168.10.20. You can get the third octet of %IP% from a second FOR statement:
for /f "tokens=3 delims=." %f in ("%IP%") do echo ip3=%f
The result from the above statement, given the value of the example, would be:
ip3=10
Related
I've been researching this for about 2 hours now and and I have the basic cmd:
wmic path Win32_PerfRawData_Tcpip_NetworkInterface Get BytesReceivedPersec,BytesSentPersec,BytesTotalPersec
I don't want the bytes for all of my network interfaces.
I want to get the total bytes for a certain interface, does anybody know how to do this because I cannot find anything on here or the web.
I don't if this what you want to get or not ?
#echo off
Title Get TotalBytes
#for /f "skip=2 tokens=2 delims=," %%a in (
'wmic path Win32_PerfRawData_Tcpip_NetworkInterface Get BytesTotalPersec /format:csv'
) do (Set "TotalBytes=%%a")
echo TotalBytes = %TotalBytes%
pause
I am trying to think of an easier way, read less prone to messing up the commands, to resolve an IP address and then set a variable to said resolved name. I tried the following:
FOR /F "TOKENS=3 DELIMS=gw" %%A IN ('PING -a -n 1 IPADDRESS') DO SET "PC=%%A"
Which on:
Pinging PCNAME [IPADDRESS} with 32 bytes of data:
Reply from IPADDRESS: bytes=32 time =15ms TTL=122
Ping statistics for IPADDRESS:
Packets: Sent = 1, Received = 1, Lost = 0 (0% loss)
Approximate round trip times in milli-seconds:
Minimum = 15ms, Maximum = 15ms, Average = 15ms
Works for most pc names. There are only a few "g" and "w" so the only line with a 3rd token is the first. The problem I experienced is that we have A LOT of pc names and a few have a "g" in them. For those resolved addresses it cuts of the variable at the 3rd "g" of the line.
I tried using it on NSLOOKUP, but the command gives me:
C:\Windows\System32>nslookup IPADDRESS
Server: SERVERNAME
Address: SERVERIP
Name: PCNAME
Address: IPADDRESS
I can skip in FOR /F (ie skip 2) but then the it reads lines 3 AND 4; ie it sets the variable to the pc name for line 3, but then changes it on line 4 to garbage I don't need.
Like I said is there a way to this easily? I can do a for in a for with calls and delayed expansion, but that is much harder to keep track of.
Filter the info you need ("Name:")
for /f "tokens=2 delims=: " %%i in ('nslookup 192.168.178.1^|find "Name:"') do set name=%%i
There is a simple method that may be used with both PING or NSLOOKUP.
With PING:
FOR /F "TOKENS=2" %%A IN ('PING -a -n 1 IPADDRESS') DO SET "PC=%%A" & GOTO CONTINUE
:CONTINUE
With NSLOOKUP:
FOR /F "SKIP=2 TOKENS=2" %%A in ('NSLOOKUP IPADDRESS') DO SET "PC=%%A" & GOTO CONTINUE
:CONTINUE
The GOTO CONTINUE is used to break the FOR after the desired information was taken.
#echo off
setlocal enableextensions
set "ip=127.0.0.1"
set "pcName="
for /f "tokens=1-3 delims=[]" %%a in ('ping -n 1 -a %ip% 2^>nul') do if not defined pcName if not "%%c"=="" (
set "pcName=%%a"
setlocal enabledelayedexpansion
for %%d in ("!pcName: =\!.") do ( endlocal & set "pcName=%%~nxd")
)
echo %pcName%
This reads the output of ping command, and splits the lines using []. The only line containing square brackets is the line with the pc name (if found). We will skip lines that are not splitted in three tokens (%%c is empty).
The first token will contain the pc name prefixed by a string indicating a ping operation is being done. This string is localized and changes depending of the windows language (in my case, spanish windows, it is "Haciendo ping a"). So, asking for a fixed token is not reliable. To avoid it, the string is converted to a false path by replacing the spaces with backslashes. Then the standard for replaceable parameter modifiers are used to retrieve the name of the last element in the false path, that is, the pc name.
Using the original approach:
FOR /F "TOKENS=2* DELIMS=gw" %%A IN ('PING -a -n 1 IPADDRESS') DO if not "%%B"=="" for /f %%C in ("%%B") do SET "PC=%%C"
(just to demo)
I basically need a script (batch) that automatically converts the users on the local network to IP addresses. What I mean is basically set every user that shows up in the command "net view" to a different variable, for example:
If I had 4 different users on the network, I would need the file to list:
1: (%1%)
2: (%2%)
3: (%3%)
4: (%4%)
So I need the script to set each user on the network to a different variable (starting at 1)
Also...
How would I set the local IP address of each computer name as a variable?
update for second part: I need to know how to set the ip address as a variable. I just need to cut out the excess stuff. If I type
ping (computername) -4
I get: well, I guess it is just easier to show you...
I think I need to use the findstr command, but I don't know.
Is this what you're looking for?
#echo off
setlocal enabledelayedexpansion
for /f "tokens=1" %%a in ('net view') do (
set comp=%%a & set comp=!comp:\\=!
for /f "tokens=2 delims=[]" %%b in (
'ping -4 !comp!'
) do (Echo !comp! - %%b)
)
Hey well I'm here again and this time I want to know how to type in between bracets from an input variable for the input of an IP Address.
I want the code to look something like this.
set input=
set /p input=IP Address: [ ].[ ].[ ].[ ]
And once I've executed the batch and typed in my IP I want it to look like this.
IP Address: [ 127 ].[ 0 ].[ 0 ].[ 1 ]
Is there anyway of doing this?
I think this would be a very complicated thing to do.
If you would try to do this in Batch you need to rewrite your line with braces on every keyboardhit the user does. The only command I know so far which listens on a single KB-hit is the Choice command.
If you manage it somehow to repeat the users input on every KB-hit you need to bytehack your batch file with backspace characters and have a logic to combine the users input with the braces. This is not only very unconfortable but also very slow.
I would recommend to let your users enter any string and after this checking it by "regex" (windows regex is not very nice)
Here is an example function which I use to check IPs of printers.
%printer% would be the string your user enters. This functions also has an nslookup function. You can ommit this if you like.
This 2nd line does all the magic, but you can also enter some IPs which are not possible like 321.099.000.666
:CheckIP
echo %printer% | findstr /R "[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*">nul || goto :EOF
FOR /F "tokens=* delims=" %%A in ('nslookup %printer% 2^>^&1 ') do (
echo %%A | findstr /R /C:"Name: ">nul && set printer=%%A
echo %%A | findstr /R /C:"\*\*\*">nul && (set printerfqdn=%printer% & goto printerip)
)
rem ***************************************
rem * Name: printername.location.companydomain
rem * printerfqdn begins at position 10
rem ***************************************
set printer=%printer:~9%
goto :EOF
Consider the following result from a hypothetical reg query:
..>reg query HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\MediaPlayer /v "I
nstallation Directory"
HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\MediaPlayer
Installation Directory REG_EXPAND_SZ %ProgramFiles%\Windows Media Play
er
How do I grep the output so that I can assign the actual value of the setting (%ProgramFiles%\...) into a variable (or temp file)?
example for batch files:
#FOR /F "tokens=2* " %%A IN ('reg query HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\MediaPlayer /v "Installation Directory"') DO #SET PARAM=%%B
If you want to use it directly in console - use single percent sign to denote variables (i.e. %A and %B)