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%
 
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.
 
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