Automatically set current dynamic IP address to static - Windows .bat - windows

I need to configure multiple machines to automatically set their IP addresses from dynamic to static.
I have a batch file to set the IP address to static like this :
netsh interface ipv4 set address name="Ethernet" source=static ^
addr=192.168.200.200 mask=255.255.255.0 gateway=192.168.200.1
pause
exit /b 0
But I have to manually write the IP address, mask and gateway in the .bat file.
How can I retrieve the current IP address of the machine (as well as the current mask and gateway) and automatically set them to static, without modifying the .bat file every time?

First you need to retrieve the current ip address.
Probably this is easy, if you use always the same name for the interface.
setlocal
set "interface_name=Ethernet"
REM *** Convert the output lines from netsh into variables
for /F "tokens=1,* delims=:" %%a in ('"netsh interface ipv4 show addresses name="%interface_name%""') do (
REM *** Get rid of leaing spaces and TABS
for /F %%a in ("%%a") DO for /F %%b in ("%%b") DO (
set "__%%a=%%b"
)
)
REM *** Show all variables beginning with "__"
set __
if "%__DHCP%" NEQ "Yes" goto :no_dhcp
:check_ip_prefix
set "ip_addr=%__IP-address%"
if "%ip_addr:192.168.=%" EQU "%ip_addr%" goto :wrong_ip_prefix
netsh interface ipv4 set address name="%interface_name%" source=static ^
addr=%ip_addr% mask=255.255.255.0 gateway=%__default_gateway%
Probably you need to change some variable names, because the names are language dependent (and I'm using a German version).

Related

Variable.1 unexpected at this time

I'm just trying to figure out why this block of code is giving me an unexpected variable..
#echo off
FOR /F "Skip=1Delims=" %%a IN (
'"wmic nic where (MacAddress="00:00:00:00:00:00") GET NetConnectionId"'
) DO FOR /F "Tokens=1" %%b IN ("%%a") DO SET nicName=%%b
echo Adding all IP ranges for X in 10.10.X.118 on adapter %nicName%
netsh interface ipv4 set address name=%nicName% static 192.168.1.118 255.255.255.0 192.168.1.1
FOR /L %A IN (0,1,255) DO netsh interface ipv4 add address %nicName% 10.10.%A%.118 255.255.255.0 10.10.%A%.1
netsh interface ipv4 add dnsserver %nicName% address=208.67.222.222 index=1
netsh interface ipv4 add dnsserver %nicName% address=208.67.220.220 index=2
exit
I think it has something to do with the first FOR loop interfering with the second, but I'm very new to using this type of search in a batch file.
The output I get is:
Adding all IP ranges for X in 10.10.X.118 on adapter Local Area Connection
nicNameAA.1 was unexpected at this time.
Thanks in advance!
FOR /L %A IN (0,1,255) DO netsh interface ipv4 add address %nicName% 10.10.%A%.118 255.255.255.0 10.10.%A%.1
As in the previous two for commands, the metavariable A must be specified as %%A.
As in the previous two for commands, the value substituted into the string must be specified as %%A - %A% is the value of an unspecified environment variable A.
The result of your code is this
FOR /L
%A IN (0,1,255) DO netsh interface ipv4 add address %
nicName
% 10.10.%
A
%.118 255.255.255.0 10.10.%
A
%.1
each %...% is interpreted as a non-existent environment variable, hence it's replaced by nothing
So the code appears to be
FOR /L nicNameAA%.1
So cmd sees nicNameAA%.1 where it's expecting %%? and complains.
BTW- since nicname's value contains spaces, you probably need "%nicname%" so that cmd will see one string. Can't vouch for it since I rarely use netsh...just be prepared.
As answered by #Magoo,
The resulting full code is as follows(In case anyone needs to do this in the future)
#echo off
FOR /F "Skip=1Delims=" %%a IN (
'"wmic nic where (MacAddress="00:00:00:00:00:00") GET NetConnectionId"'
) DO FOR /F "Tokens=1" %%b IN ("%%a") DO SET nicName=%%b
echo Adding all IP ranges for X in 10.10.X.118 on adapter "%nicName%"
netsh interface ipv4 set address name="%nicName%" static 192.168.1.118 255.255.255.0 192.168.1.1
FOR /L %%c IN (0,1,255) DO netsh interface ipv4 add address "%nicName%" 10.10.%%c.118 255.255.255.0 10.10.%%c.1
netsh interface ipv4 add dnsserver "%nicName%" address=208.67.222.222 index=1
netsh interface ipv4 add dnsserver "%nicName%" address=208.67.220.220 index=2
exit
Thanks again #Magoo!

prnmngr.vbs - parse and format output as csv from a windows batch file?

#ECHO OFF
setlocal enabledelayedexpansion
for /f "tokens=*" %%f in ('Cscript %WINDIR%\\System32\\Printing_Admin_Scripts\\en-US\\Prnmngr.vbs -l -s myserver1.com') do (
echo %%f
)
pause
output looks like :
Server name myserver1.com
Printer name myprinter1
Share name myprinter1
Driver name SHARP UD2 PCL6
Port name myprinter1.com
Comment
Location
Print processor winprint
Data type RAW
Parameters
Attributes 584
Priority 1
Default priority 0
Average pages per minute 0
Printer status Idle
Extended printer status Unknown
Detected error state Unknown
Extended detected error state Unknown
and so on for 100 printers
output expected in a csv file is the server name, printer name share name, driver name, port name as shown below:
myserver1.com,myprinter1,myprinter1,SHARP UD2 PCL6,myprinter1.com
myserver2.com,myprinter2,myprinter2,hp PCL6,myprinter2.com
myserver3.com,myprinter3,myprinter3,hp universal PCL5,myprinter3.com
Please suggest
#ECHO OFF
setlocal enabledelayedexpansion
CALL :zapvars
:: for /f "tokens=*" %%f in ('Cscript %WINDIR%\\System32\\Printing_Admin_Scripts\\en-US\\Prnmngr.vbs -l -s myserver1.com') do (
for /f "tokens=*" %%f in (q48077575.txt) do (
FOR /f "tokens=1,2*delims= " %%a IN ("%%f") DO IF "%%a"=="Comment" (
CALL :report
) ELSE IF "%%b"=="name" SET "%%a=%%c"
)
)
GOTO :EOF
:: report - use drop-through to zap variables used
:report
ECHO %server%,%printer%,%share%,%driver%,%port%
:: clear variables used
:zapvars
FOR %%z IN (server printer share driver port) DO SET "%%z="
GOTO :eof
I used a file named q48077575.txt containing your data for my testing, supplemented by some similar data.
Note the call to zapvars initially to clear the variables used.
You were already reading each line to %%f. Tokenise each line to %%a,%%b,%%c using spaces as delimiters then if the first token is not Comment, see whether the second is name and set a variable named %%a to the value on the remainder of the line after name, %%c.
When the first token is Comment, then output the names that have been detected and clear the variables ready for the next data block.

Windows Batch check hostname if contains four or three chars then do

The problem im facing, is that i wanted a batch script which worked like this :
Check for computer hostname
If computer hostname starts with A405 or A408 or A410 or A417 or LAS (i red that i can use %var:0,4% or smthing similar to check the full hostname for the first 4 characters, for the LAS its 3 chars - all pc hostname's are given like this %CLASSROOM%-%PCNUM%)
Then launch corresponding reg file from network disk (each regfile disables PROXY and sets a new default and different homepage for each class, the regfiles are done already)
A405 -> J:/1.reg
A408 -> J:/2.reg
etc
after that launch internet explorer with -k enabled. ( start iexplore -k )
But i couldn't get how to make %computername% a variable, because set host=%computername%, returns %computername% not the actual name of the workstation, and how to check with previously stated - %host:0,4%, and for LAS the %host:0,3% and check for the first 4 characters.
I'm sorry if im not making myself clear enough.
I tried to use this script from SO:
#echo off
rem Change this for testing, remove for production
set computername=xyz
set dropLoc=machine-abc
if "%computername%" == "xyz" (
set dropLoc=machine-xyz
)
echo %dropLoc%
But I couldn't do it.
#echo off
rem Check "%computername%" vs. 4 chars. long names:
set regFile=0
for %%a in (A405 A408 A410 A417) do (
set /A regFile+=1
if "%computername:~0,4%" equ "%%a" goto found
)
rem Check "%computername%" vs. 3 chars. long names:
for %%a in (LAS) do (
set /A regFile+=1
if "%computername:~0,3%" equ "%%a" goto found
)
echo No matching hostname found
goto :EOF
:found
echo Hostame found, reg file: J:/%regFile%.reg

WIN2K8: Running two netsh commands in one line; hitting a syntax error

I've written a Windows script to change NIC interface metrics, and need to condense it to two commands, because of the manner in which it is executed. To render a long story short, I support an application (BladeLogic Server Automation [BSA]) that uses remote agents to call system commands.
I've hypothesized that when BSA runs the script, each command executes in a separate Command Prompt environment, so the environment variables used to store the route strings aren't persistent.
for /f "delims=" %a in ('netsh interface ipv4 dump ^| find "nexthop=1.1.1.1"') do #set VAR1=%a
netsh interface ipv4 set %VAR1:~4% metric=200
for /f "delims=" %a in ('netsh interface ipv4 dump ^| find "nexthop=2.2.2.1"') do #set VAR2=%a
netsh interface ipv4 set %VAR2:~4% metric=500
I've condensed the script as such and am testing it at the Command Prompt.
for /f "delims=" %a in ('netsh interface ipv4 dump ^| find "nexthop=1.1.1.1"') do #set VAR1=%a && netsh interface ipv4 set %VAR1:~4% metric=200
for /f "delims=" %a in ('netsh interface ipv4 dump ^| find "nexthop=2.2.2.1"') do #set VAR2=%a && netsh interface ipv4 set %VAR2:~4% metric=500
Unfortunately, it doesn't seem to recognize the proper syntax for the second command:
The following command was not found: interface ipv4 set %VAR1:~4% metric=200
Is there another way I could append the second command, so it's interpreted as being syntactically correct? I'm open to suggestions!
Matt,
You're correct that BladeLogic fires up separate shells for each statement, however, depending on what you're trying to achieve, it may be possible to "persuade" it to run a complete script remotely on your target.
If you're still facing this issue, please respond with a little more info about what you're trying to achieve and which approaches you've tried, and we can look a the alternatives.
-John.

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