批处理--工作当中获取本机ip地址然后执行一个循环启动脚本

<pre name="code" class="cpp"><span style="font-size:18px;">goto :main

rem +++++++获取本机ip++++++++++++++++++++++++++++++++++++
:get_local_ip
rem 注意 不要照搬某些人的文章,如果你把函数写成setlocal   dosomething endlocal 这种结构,那么在这段代码中返回值无法赋值,你得到的返回ip为""
@echo off
echo 开始获取本机ip....................

for /f "delims=:" %%i in ('ipconfig ^| findstr /N "本地连接"') do (
	set /a op_line=%%i
	goto :for_break1
)
:for_break1

set ip_addr=""
set /a op_num=%op_line%

rem 启用延迟变量扩展 启用后 for里面一定要用双!不要用双%啊 这是我发现很多讲延迟扩展的文章都没说的,我看了他们的文章根本不行,必须用! 自己血的试验结果,你问我为何?鬼知道这该死的批处理语法。。。
setlocal enabledelayedexpansion
rem skip=%op_line%表示跳过前面%op_line%行,直接从%op_line%+1行开始处理 delims=表示读取文件时是按照那个字符来分割行的,这里默认使用换行符,也可以比如delims=+表示每次碰到+号时就作为一行交给for内部代码处理
for /f "skip=%op_line% delims=" %%i in (1.tmp) do (
rem 数字操作 用 /a的set
	set /a op_num+=1
rem 这里 op_line可以用%或者!围起来都行,因为它本身没有被改变的逻辑 因为%表示不延迟扩展,那么该变量在for之前就已经获得了 for的整个代码块被认为是一条代码,那么用%的变量会在for之前被替换上for之前的值,这样for内部就好像成了常量一般,无法改变for之前定义的变量
	if !op_num! gtr %op_line% (
		set tmp_i=%%i
		echo !tmp_i! | findstr "[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*"

		rem 发现该行为地址行
		if "!errorlevel!" == "0" (
			:loop_search_addr_pos
rem 表示<span style="color:#ff0000;">截</span>取掉第一个字符 !tmp_i:~0,3!表示<span style="background-color: rgb(255, 0, 0);">获</span>取前三个字符
			set tmp_i=!tmp_i:~1!
			echo !tmp_i! | findstr "^[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\.[0-9]*[0-9]"  >null
			if  !errorlevel! equ 0 (
				set ip_addr_local=!tmp_i!
				goto :for_break2
			)  else (
				goto :loop_search_addr_pos
			)
		)
	)
)
setlocal disableextensions
:for_break2</span>
rem 返回值赋值
set "%~1=%ip_addr_local%"
goto :exec_code

:main
call :get_local_ip ip_addr
:exec_code
echo local_ip=%ip_addr%
pause</span>
rem 循环从 (开始值,递增值,最后值) 进行调用
for /l %%i in (10000001,1,10000010) do start HostClientTest.exe %ip_addr%  0 %%i 192.168.12.63 30004</span>

时间: 2024-08-29 05:55:21

批处理--工作当中获取本机ip地址然后执行一个循环启动脚本的相关文章

获取本机IP地址

这里有两种方法: 1 //获取本机IP 2 - (NSString *)localIPAddress 3 { 4 NSString *localIP = nil; 5 struct ifaddrs *addrs; 6 if (getifaddrs(&addrs)==0) { 7 const struct ifaddrs *cursor = addrs; 8 while (cursor != NULL) { 9 if (cursor->ifa_addr->sa_family == AF_

关于是用dotnet获取本机IP地址+计算机名的方法

印象中在maxscript帮助文档里找到过方法,但是当时没记下来.只能通过dotnet实现了. 如果电脑有无线网卡和本地连接,可能会出现乱码,也问了写dotnet的朋友,提供了一些思路,不过最终还是使用了这个笨办法. fn getIP_PCname = ( cc = (dotnetclass "System.Net.Dns") oo = cc.GetHostAddresses(cc.GetHostName()) for ip = 1 to oo.count do ( getip = f

java获取本机IP地址

/** * WIFI没打开:ip为127.0.0.1 * 获取本机IP地址字符串 * @return */ public String getWifiIp() { if (!getWifiEnabled()) { return "127.0.0.1"; } WifiInfo wifiInfo = mWifiManager.getConnectionInfo(); int ipAddress = wifiInfo.getIpAddress(); String ip = intToIp(i

Linux编程获取本机IP地址

使用函数getifaddrs来枚举网卡IP,其中使用到的结构体如下所示: struct ifaddrs { struct ifaddrs *ifa_next; /* Next item in list */ char *ifa_name; /* Name of interface */ unsigned int ifa_flags; /* Flags from SIOCGIFFLAGS */ struct sockaddr *ifa_addr; /* Address of interface *

#获取本机IP地址时排除IPv6类型,只返回IPv4地址的方法

public static string GetLocalIP(){try{string HostName = Dns.GetHostName(); //得到主机名IPHostEntry IpEntry = Dns.GetHostEntry(HostName); for (int i=0; i < IpEntry.AddressList.Length; i++){//从IP地址列表中筛选出IPv4类型的IP地址//AddressFamily.InterNetwork表示此IP为IPv4,//Ad

Python 获取本机IP地址

import socket #获取本机IP地址 self.local_ip = socket.gethostbyname(socket.gethostname()) print (self.local_ip) python 判断本机是否联网 1 timeout = 200 2 host = 'www.baidu.com' 3 port = 80 4 s=socket.socket() 5 s.settimeout(timeout) 6 status = s.connect_ex((host,po

C# 获取本机IP地址以及转换字符串

/// <summary> /// IP地址转化 /// </summary> /// <param name="ipaddr">整型的IP地址</param> /// <returns>字符串的IP地址</returns> private string UintIPToStringIP(uint ipaddr) { string hexStr = ipaddr.ToString("X8");

Qt获取本机IP地址

Qt获取本机IP地址: Qt版本:4.8.6 #include <QtNetwork/QNetworkInterface.h> QString ipAddr; QList<QHostAddress> list = QNetworkInterface::allAddresses(); foreach (QHostAddress address, list) { if (address.protocol() == QAbstractSocket::IPv4Protocol) { ipA

详谈再论JAVA获取本机IP地址

首先,你如果搜索“JAVA获取本机IP地址”,基本上搜到的资料全是无用的.比如这篇:http://www.cnblogs.com/zrui-xyu/p/5039551.html实际上的代码在复杂环境下是不准的 网上一个比较普遍的说法是InetAddress.getLocalHost().getHostAddress()似乎很简单,但忽略了一个问题,即IP地址在现在的网络环境更加复杂了,比如有Lan,WIFI,蓝牙热点,虚拟机网卡...即存在很多的网络接口(network interfaces),