如何分割IP地址到单独的字节,在cmd中执行算术 [英] How to split an ip address into seperate octets and perform arithmetic in cmd

查看:158
本文介绍了如何分割IP地址到单独的字节,在cmd中执行算术的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我所试图做的是通过CMD配置一些VLAN范围。什么到目前为止,我已经完成了是检索域控制器的IP地址,并在值前面删除的空间。我已经使用以下命令来实现这一点:

What I am trying to do is configure some vlan scopes via cmd. What I have accomplished so far is to retrieve the IP Address of the Domain Controller and remove the space in front of the value. I have accomplished this using the following commands:

通过本地CMD REM提取IP地址
    FOR / F令牌= 2 delims =%i的('IPCONFIG ^ |找到/ IIPv4地址)并设置IP_ADDRESS =%I

rem extract ip address via local cmd for /f "tokens=2 delims=:" %i in ('ipconfig ^| find /i "IPv4 Address"') do set ip_address=%i

结果举例:设置IP_ADDRESS = 10.0.0.25

example result: set ip_address= 10.0.0.25

从物权IP地址的变种删除空
    设置IP_ADDRESS =%IP地址:=%

rem remove empty space from ip address var set ip_address=%ip_address: =%

回声%IP_ADDRESS%,现在导致10.0.0.25没有前面的空间。

echo %ip_address% now results in 10.0.0.25 without the space in front.

我想下一步要做什么被分割在不同的八位字节变量IP_ADDRESS变量,使算法能对选择的八位字节来执行。

What I would like to do next is split the ip_address variable in separate octet variables so that arithmetic can be performed on the octet of choice.

例如:。10.0.0.25然后可以被操纵,以反映10 + 100] .0 [ - 24]

For example: 10.0.0.25 could then be manipulated to reflect 10.[+100].0.[-24]

期望的结果随后将是10.100.0.1

the desired result would then be 10.100.0.1

我想preFER来完成这个严格使用Windows命令行,但如果有更好的方法退出我很开放的建议。

I would prefer to accomplish this strictly using windows command line but if a better method exits I am open to suggestions.

在此先感谢
乔尔

Thanks in advance Joel

推荐答案

试试这个到八位组分成变量。

Try this to split up the octets into variables.

@echo off
set "ip=10.0.0.25"
SET "offsets=0.100.0.-24"

@echo off
for /f "tokens=1-4 delims=. " %%a in ("%ip%") do (
set octetA=%%a
set octetB=%%b
set octetC=%%c
set octetD=%%d
)
FOR /f "tokens=1-4 delims=." %%a in ("%offsets%") do (
SET /a octetA+=%%a
SET /a octetB+=%%b
SET /a octetC+=%%c
SET /a octetD+=%%d
)
echo "%octetA%","%octetB%","%octetC%","%octetD%"
pause

这篇关于如何分割IP地址到单独的字节,在cmd中执行算术的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆