batch list net view and write their ip address to a variable - variables

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)
)

Related

Registry find value, take key name then search and update another value

No idea if title helps here, so i'll be as descriptive as i can.
I need to update the "Use this connection’s DNS Suffix in DNS registration" value for a certain Network card, i only know what the NIC Name ("NIC3-Networkname") is and it will be different on each server i touch.
I have identified the required steps, just not sure on how to handle step 2.
1) Query for "NIC3-Networkname"
reg query "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Network" /f "NIC3-Networkname" /s
This then returns the following.
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Network\{4D36E972-E325-11CE-BFC1-08002BE10318}\{EFBE1796-C4F0-4612-B3D6-E94B794E84D4}\Connection Name REG_SZ NIC3-Networkname
2) Next i need to take the {EFBE1796-C4F0-4612-B3D6-E94B794E84D4} Key name, set it as a variable - lets say %NICUID%
3) Then need to take the above variable and add the DNS Name.
reg add "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\Tcpip\Parameters\Interfaces\%NICUID%" /f /v "Domain" /t REG_SZ /d "Company.com"
Any tips and advise welcomed!
Thanks
B
Parse the output of reg using \ as separator and take the 7th token:
for /f "delims=\ tokens=7" %%a in (
'reg query "HKLM\SYSTEM\CurrentControlSet\Control\Network" /f "NIC3-Networkname" /s'
) do set NICUID=%%a

How can I set the line a string is found on as a variable, and then find the contents of that line number in another file?

Okay, this is kind of complicated so ill try to explain it as best as is can.
I am currently writing a simple program for my own use using notepad.
I am using the language batch and running the program through Command Prompt.
Part of the Program lets you access an account you created with username and password:
set /p USERNAME1= Username?
findstr /n "%USERNAME1%" Usernames.txt
In Usernames.txt are the usernames of each account that has been created, one per line.
If your username is found, it is displayed along with the line number before it in the program.
It then asks for your password:
set /p PASSWORD1= Password?
This is where the problem starts. When the accounts are created, the usernames are stored, one per line, in Usernames.txt , and so are the passwords but in Passwords.txt
I need the program to check if the password you typed is the same as the password on the same line number the username is on, in Passwords.txt
I know this is complicated but if anyone can help it would be greatly appreciated!
Thanks
You're using findstr /N to get the line number of usernames.txt, which is a good start. You're getting the entire line, prefixed with the line number and a colon :.
So for /F can be used to extract the number only. The option string "tokens=1 delims=:" defines to divide the found line at the (first) :, so the line number is separated from the user name.
Finally, another for /F can be used to get the line of the passwords.txt files.
Putting all those things together, the following code snippet emerges:
set /P USERNAME1=Username?
set /P PASSWORD1=Password?
set /A LINENUMBER=0
for /F "tokens=1 delims=:" %%I in (
'findstr /N /I /X /C:"%USERNAME1%" "\path\to\usernames.txt"'
) do (
set LINENUMBER=%%I
goto :CONTINUE1
)
:CONTINUE1
set /A LINENUMBER-=1
if %LINENUMBER% lss 0 (
exit /B
) else if %LINENUMBER% equ 0 (
set SKIPPING=
) else (
set SKIPPING=skip=%LINENUMBER%
)
for /F "usebackq %SKIPPING% delims=" %%I in (
"\path\to\passwords.txt"
) do (
if "%%I" equ "%PASSWORD1%" (
goto :CONTINUE2
)
exit /B
)
:CONTINUE2
rem do something...
So the variable USERNAME1 holds the entered user name to search and PASSWORD1 the entered password.
If the user name cannot be found in usernames.txt, or the entered password does not match the found one, the script is terminated using exit /B.
Note that user names are compared in a case-insensitive manner (/I switch), but the password are compared case-sensitively.

How can I resolve the IP address and then set a variable to the resolved pc name

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)

batch populate VAR from external list

Been trying to populate my internal VAR from an external CSV from "column 2", I've been trying different combinations and obviously have something fundamentally wrong as I am getting nowhere, I was thinking the for for /f "usebackq tokens=*" %%A in ("%IP_List%") do was the correct use, but I cannot set this against different variables (see below).
This CSV contains computer names and IP address, I would like to be able to read these in in order and have each 1 set against a different variable.
the list contains
computer1 10.1.14.09
computer2 10.1.14.10
computer3 10.1.14.11
computer4 10.1.14.12
I would like to set
set C1=1st IP address
set C2=2nd IP address
set C3=3rd IP address
set C4=4th IP address
So I can calls these later doing something like
ping %C1%
Or a compare (which I will probably need help with aswell)..
Based upon what you show, this will work:
#echo off
setlocal enabledelayedexpansion
set "IP_LIST=file.csv"
set "count=0"
for /f "tokens=2 usebackq" %%A in ("%IP_LIST%") do set /a "count+=1" & set "C!count!=%%A"
set C
pause
endlocal
exit /b 0
Output:
C1=10.1.14.09
C2=10.1.14.10
C3=10.1.14.11
C4=10.1.14.12
...

Batch file help needed - functions, variables and substrings

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