Is this possible? (Batch file)

Soldato
Joined
16 May 2008
Posts
2,579
Location
Bristol
Hi,

I need to make a batch file which takes an IP, then subtracts 1 from the 4th octet and assigns the new value to a variable.

eg:
Enter IP 10.10.10.10
output: 10.10.10.9

I'm struggling to think of a solution without using a third party application.. Any ideas? :confused:
 
I have made some maintenance scripts which currently have to be run on each device individually. With this feature I can run the script on 1 host and have it replicate its actions on all the hosts. :)
 
Hmm I'm not sure how I could get a hostname list without writing it manually.. However this seems to work:

@ECHO OFF
set /p numTill=Enter number of tills:=
IPCONFIG |FIND "IP" > %TEMP%.\TEMP.DAT
FOR /F "tokens=2 delims=:" %%a in (%TEMP%.\TEMP.DAT) do set IP=%%a
del %TEMP%.\TEMP.DAT
set IP=%IP:~1%
echo IP=%IP%

CALL :SPLITIP %IP%

:SPLITIP
:: Split IP into Separate Octects

if defined IP (
for /F "usebackq delims=. tokens=1-4" %%i in (`echo %IP%`) do (
set PART1=%%i
set PART2=%%j
set PART3=%%k
set PART4=%%l
)
) else ( echo ERROR: IP Not Defined )

IF %numTill% == 2 GOTO 2TILL

:2TILL
SET IP2=%PART4%
SET /A IP2=(IP2-1)
SET till2=%PART1%.%PART2%.%PART3%.%IP2%
echo %till2%
 
Well, I usually find it prudent to have a list of machines on a network. Active Directory takes care of this in the domains I manage. But I would certainly make a manual list if otherwise.

Though you have found your solution, so all is well. :)
 
Unfortunately these hosts aren't connected to active directory or anything the IPs could be pulled from easily. There are also 2500 individual sites, which I don't fancy doing manually! :p
 
It's quite a bespoke set-up, I imagine other departments might have access to some kind of host name database.. but for now, this will do.
 
You're not some kind of corporate spy are you? ;)

Once I have the IPs in a variable I can then use lines like:
PSEXEC \\%IP2% -U x -P x shutdown -r -t 10
PSEXEC \\%IP3% -U x -P x shutdown -r -t 10
PSEXEC \\%IP4% -U x -P x shutdown -r -t 10

This one will ultimately close down the software on all hosts, backup the database on a specific host, copy it to another, restore the database, delete certain files, insert certain shortcuts and start up the software again. A 10-15 minute job by hand and it's done usually at least 6 times a day, sometimes many more.
 
Are you able to install Powershell on a particular host? As rather than your batch script, I reckon this one I just wrote is better! :p

multi-ip-maintenance.ps1
Code:
param([string]$subnet = "192.168.0", [int]$start = 1, [int]$end = 254)


$start..$end | % {$subnet + "." + $_} | % {psexec \\$_ -u U -p P powercfg /?}
When you run the script you can specify the subnet and the start and end values for the host numbers. It will do the magic and run the code.

You would need to change what your psexec does, I just used powercfg as it gave me quick feedback to see if it worked! :p

Obviously you need to install PowerShell and set it up to run scripts locally [Set-ExecutionPolicy RemoteSigned]. Save the above in a file, i.e. multi-ip-maintenance.ps1

Then in a PS console:

Code:
C:\scripts\multi-ip-maintenance.ps1 192.168.1 10 254

If you do not supply any arguments it will default to all values between 192.168.1.1 and 192.168.1.254. Easily changed of course.

The code generates a list of numbers between the upper and lower bounds, inclusive, builds an IP address from the subnet variable and generated number, then passes it on to psexec. It does this for every number it generates. Snazzy eh?
 
That does indeed look like a nicer solution! I've never used powershell before, looks pretty good. I have however spent about 8 hours staring at this today and really don't have the energy to rewrite it now :p

The only thing left to do is check all the ips in @ips.txt for a certain process, then when the process does not exist on all the ips.. move on to the next step :)
 
Back
Top Bottom