中国DOS联盟

-- 联合DOS 推动DOS 发展DOS --

联盟域名:www.cn-dos.net 论坛域名:www.cn-dos.net/forum
DOS,代表着自由开放与发展,我们努力起来,学习FreeDOS和Linux的自由开放与GNU精神,共同创造和发展美好的自由与GNU GPL世界吧!

中国DOS联盟论坛
现在时间是 2026-06-21 10:49
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » 讨论 完善新的外网IP获取 查看 1,526 回复 9
楼 主 讨论 完善新的外网IP获取 发表于 2008-01-07 11:00 ·  中国 湖南 株洲 电信
初级用户
积分 39
发帖 16
注册 2006-12-16 08:36
19年会员
UID 73701
性别 男
状态 离线
原来那个外网IP获取是通过swz8.com网站提取的
现在这网站已经关了 小弟从ip138找到了它的IP获取页面
但文字处理上 俺不大会 请帮忙完善

@echo off
title 获取本机内网和外网IP
if exist ip2city.asp del ip2city.asp
if exist ip.txt del ip.txt
if exist 1.txt del 1.txt
if exist 2.txt del 2.txt
wget http://www.ip138.com/ip2city.asp
ren ip2city.asp 1.txt
此时1.txt内容为


<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=gb2312">
<title> 您的IP地址 </title>
</head>
<body style="margin:0px"><center>您的IP地址是: 来自:湖南省株洲市 电信</center></body></html>


以下再用SED提取

sed -r "s/(+\.+\.+\.+).*/\n\1/;s/^.*\n//" 1.txt >2.txt
for /f %%i in (2.txt) do set i=%%i
echo %i% >ip.txt
start ip.txt

IP.txt内容为

1.1.1.1空格接回车

我是新手 请高手帮忙精简完善 去掉IP.TXT的空格接回车
2 发表于 2008-01-07 11:03 ·  中国 湖南 株洲 电信
初级用户
积分 39
发帖 16
注册 2006-12-16 08:36
19年会员
UID 73701
性别 男
状态 离线
很多去空格和回车的代码 我试了 没效果
3 发表于 2008-01-07 11:18 ·  中国 浙江 杭州 电信
高级用户
★★
积分 537
发帖 219
注册 2007-08-04 09:43
18年会员
UID 94620
性别 男
来自 杭州--半山
状态 离线
sed "s/[ |\t]\+$//" ip.txt
4 发表于 2008-01-07 11:21 ·  中国 湖南 株洲 电信
初级用户
积分 39
发帖 16
注册 2006-12-16 08:36
19年会员
UID 73701
性别 男
状态 离线
Originally posted by junchen2 at 2008-1-7 11:18 AM:
sed "s/\+$//" ip.txt

sed "s/\+$//" ip.txt >1.txt
空格变成了黑块 回车还是回车
5 发表于 2008-01-07 11:33 ·  中国 湖南 株洲 电信
初级用户
积分 39
发帖 16
注册 2006-12-16 08:36
19年会员
UID 73701
性别 男
状态 离线
在网上找到一个VBS的
但结果也不是纯数字

set http=createobject("Microsoft.XMLHTTP")
ipp="http://www.ip138.com/ip2city.asp"
http.open "get",ipp,false
http.send
ss=bytes2BSTR(Http.responsebody)
ss=Replace(ss,vbcrlf,"")
Set re = New RegExp
re.Pattern = "(.+valign=""top"">)(.+\d+\.\d+\.\d+\.\d+)(.+)"
re.Global = True
re.IgnoreCase = True
re.MultiLine = True
rv = re.Replace(ss,"$2")
msgbox rv
Function bytes2BSTR(vIn)
strReturn = ""
For i = 1 To LenB(vIn)
ThisCharCode = AscB(MidB(vIn,i,1))
If ThisCharCode < &H80 Then
strReturn = strReturn & Chr(ThisCharCode)
Else
NextCharCode = AscB(MidB(vIn,i+1,1))
strReturn = strReturn & Chr(CLng(ThisCharCode) * &H100 + CInt(NextCharCode))
i = i + 1
End If
Next
bytes2BSTR = strReturn
End Function
6 发表于 2008-01-07 12:44 ·  中国 湖北 武汉 电信
版主
★★★★★
积分 11,386
发帖 4,938
注册 2006-07-23 17:10
19年会员
UID 59080
状态 离线
for /f "delims= tokens=2" %a in ('wget -qO- http://www.ip138.com/ip2city.asp^|findstr /rc:"\*\]"') do @set/p=my ip is <nul>1.txt
7 发表于 2008-01-07 13:44 ·  中国 广西 玉林 博白县 电信
金牌会员
★★★★
积分 3,687
发帖 1,467
注册 2005-08-08 12:00
20年会员
UID 44210
状态 离线
用VBS获取:
@echo off
echo 正在连接,请稍等...
>Getip.vbs echo Set oDOM=WScript.GetObject("http://www.ip138.com/ip2city.asp"):Do Until oDOM.readyState="complete":WScript.sleep 200:Loop:WScript.echo oDOM.documentElement.outerText:Set oDOM=nothing
for /f "tokens=2 delims=" %%i in ('"cscript //nologo Getip.vbs"') do echo 你的外网 IP 是: %%i
del Getip.vbs
pause

VBS部分就是(一定要有WScript.sleep 200延时部分,否则会占用99%系统资源及长时间得不出数据):
Set oDOM=WScript.GetObject("http://www.ip138.com/ip2city.asp")
Do Until oDOM.readyState="complete"
WScript.sleep 200
Loop
WScript.echo oDOM.documentElement.outerText
Set oDOM=nothing


[ Last edited by zh159 on 2008-1-7 at 01:49 PM ]
8 发表于 2008-08-29 23:30 ·  中国 新疆 乌鲁木齐 电信
新手上路
积分 2
发帖 1
注册 2006-12-18 14:22
19年会员
UID 73870
性别 男
状态 离线
for /f "skip=6 delims= tokens=2" %%a in ('wget -qO- http://www.ip138.com/ip2city.asp') do set IP=%%a&echo %IP:~0,-2%>IP.txt

回车不好去。。。
9 发表于 2008-12-08 19:06 ·  中国 广东 茂名 电信
初级用户
积分 42
发帖 23
注册 2006-02-13 04:23
20年会员
UID 50270
状态 离线
for /f "delims= tokens=2" %%a in ('wget -qO- http://www.ip138.com/ip2city.asp^|findstr /rc:"\*\]"') do @set/p=%%a<nul>1.txt



纯数字

版主那个已经给出答案了

[ Last edited by pighead123 on 2008-12-8 at 19:16 ]
我是allyesno 帐号忘记密码了 妈的
10 发表于 2008-12-09 15:19 ·  中国 湖北 武汉 电信
中级用户
★★
积分 247
发帖 83
注册 2005-09-12 12:06
20年会员
UID 42446
性别 男
状态 离线
我的是这样的
curl http://members.3322.org/dyndns/getip
以前也是在ip138获取,不过分析ip出来麻烦了点
论坛跳转: