简体   繁体   English

如何使用批处理文件将IP地址转换为整数

[英]How to convert IP address to integer using batch file

I have 2 problem statements 我有2个问题陈述

1: 1:

I need to read read an IP address from a text file and increment the value by 1 and store it in a variable. 我需要从文本文件中读取IP地址,并将值增加1并将其存储在变量中。 This all I have to do using a batch script. 这是我使用批处理脚本所做的所有事情。

This is how my text file looks like - 这是我的文本文件的样子-

MyIpAddressList.txt
192.168.1.105

I am able to read it as string and store & print it 我可以将其读取为字符串并存储并打印

for /f "delims=" %%x in (IPADDRESSLIST.txt) do set /p IPAddress=%%x+1
ECHO %IPAddress%

But when I am trying to read it as number as mentioned set /a IPAddress=%%x+1" it does't work. 但是,当我尝试将其读取为set /a IPAddress=%%x+1"它将无法正常工作。

2: 2:

I have to run a loop for 100 times and pass this IP address as an input to another batch script and increament this ip address by 1. Example: 我必须运行100次循环,然后将此IP地址作为输入传递到另一个批处理脚本,并且使该IP地址减1。示例:

Main_Batch_File.bat Main_Batch_File.bat

READ MyIpAddressList.txt and store IP address in IPAddress
IPAddress = 192.168.1.105
LOOP 100 times
CALL Another_Batch_Script(IPAddress)
IPAddress++
LOOP END

I am not sure if it is possible using a batch file and apologise if this does not make sense. 我不确定是否可以使用批处理文件,如果这样做没有道歉,请向我道歉。

setlocal enabledelayedexpansion
for /f "tokens=1,2,3,4 delims=." %%a in (%ipaddress%) do (
  set/a suffix=1%%d-1000
  for /l %%i in (1,1,100) do (
    set/a suffix+=1
    call "yourscript.bat" %%a.%%b.%%c.!suffix! 
  )

first we split ipaddress in four tokens. 首先,我们将ipaddress分为四个令牌。 then handle the fourth. 然后处理第四个。 the set/a suffix=1%%d-1000 avoids numbers below 100 (ie 080) being treated as octal set/a suffix=1%%d-1000避免将100以下的数字(即080)视为八进制

EDIT: as TripeHound suggested, if you need to start from current address, move the increment behind the call statement. 编辑:如TripeHound所建议,如果您需要从当前地址开始,请将增量移到call语句后面。

elzoologico's solution expanded to avoid the "overflow" problem, TripeHound mentioned: TripeHound提到elzoologico的解决方案已扩展以避免“溢出”问题,

@echo off
set ipaddress=192.168.001.015
setlocal enabledelayedexpansion
for /f "tokens=1-4 delims=." %%a in ("%ipaddress%") do (
  set /a suffix=1%%d-1000
  set /a final=suffix+100
  if !final! gtr 255 set final=255
  for /l %%i in (!suffix!,1,!final!) do (
    ECHO call "yourscript.bat" %%a.%%b.%%c.%%i 
  )
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM