跳至主要内容

内网IP自动获取

内网IP自动获取

redraiment, 2009-09-23





  在寝室上内网真的是很闹心呀~稍微迟点开机,IP 地址就被别人抢去了。昨晚九点放学后,一直等到十点半才连上,实在忍无可忍了!
  IP 被抢后解决方法无非就是找一个没被占用的地址,但手工去测试 256 个地址太折磨人了,早些时候我们班的两个同学就分别用 Java C# 来实现过类似的小工具。但解决这样的小问题有点杀鸡用牛刀的感觉,不符合我的性格^_^,我当然还是用批处理来解决。
  批处理中核心的两条语句就是:
设置静态IP地址
netsh int ip set address "本地连接" static IP地址 子网掩码 网关 1
设置域名解析器
netsh int ip set dns "本地连接" static 域名解析器地址 primary

  程序使用时需要提供一个网关地址,为了方便大家使用,在 XP Vista 系统能自动检测网关地址,如果正确的话直接敲一个回车就可以了。下面是批处理的代码,大家只要将下面的代码复制到记事本里,另存为 sip.bat,双击即可运行。
:: 自动查找可用 IP
:: 适用于浙江工商大学
:: by redraiment
@echo off
setlocal enabledelayedexpansion

if NOT "%1"=="" goto scan

:: 检测可识别的系统版本
:: XP
ver | find "[版本 5"
if NOT ERRORLEVEL 1 (
  set SV=XP
  set CG='"ipconfig|find /I "gate""'
  set TS=13
)

:: VISTA
ver | find "[版本 6"
if NOT ERRORLEVEL 1 (
  set SV=VISTA
  set CG='"ipconfig|find /V "::"|find "网关""'
  set TS=15
)

if NOT "%SV%"=="" (for /F "tokens=%TS%" %%i in (%CG%) do set g=%%i)
echo 您的网关可能是:!g!
set /P gateway=如果不正确请重新输入,否则输入回车:
if "!gateway!"=="" set gateway=!g!
echo %0 !gateway!
pause
exit

:scan
for /l %%d in (2,1,253) do (
  echo 正在验证 %~n1.%%d 是否被占用 ...
  ping -w 10 -n 1 %~n1.%%d | find /I "TTL"
  if ERRORLEVEL 1 (
    echo 设置本地IP ...
    netsh int ip set address "本地连接" static %~n1.%%d 255.255.255.0 %1 1

    echo 测试连接网关 ...
    ping -w 10 -n 1 %1 | find /I "TTL"
    if NOT ERRORLEVEL 1 (
      echo 恭喜,%~n1.%%d 可用!
      echo 设置域名解析器 ...
      netsh int ip set dns "本地连接" static 210.33.88.1 primary
      exit
    )
  )
)
  下面是在 XP 环境下运行的效果图,它将本地 IP 设置成 10.230.71.6
  



评论

此博客中的热门博文

AutoHotKey 新手入门教程

AutoHotKey 真是一个好玩的工具!短短几行代码就是先了“窗口置顶”、“窗口透明”等功能,之前我还特意为此装了好几个小工具,现在都可以卸掉了。闲来无事,就把 Quick Start 翻译了一下,我没有逐字逐句地翻译,有时候我嫌原文罗嗦就用自己的话概括地描述了一下。 原文地址:http://www.autohotkey.com/docs/Tutorial.htm 教程目录 创建脚本 启动程序 模拟鼠标键盘 操纵窗口 输入 变量与剪切板 循环 操纵文件 其他特性 创建脚本 每个脚本都是一个纯文本文件,由一些能被 AutoHotKey.exe 执行的命令组成。一个脚本可能还包含 热键 和 热字符串 。如果没有热键和热字符串,脚本在启动的时候就会从头依次执行到尾。 创建一个新的脚本: 下载 并安装 AutoHotkey。 右击鼠标,选择 新建 -> 文本文档 。 输入文件名并确保以 .ahk 结尾。例如:Test.ahk。 右击文件,选择 编辑脚本 。 输入以下内容:#space::Run www.google.com 上一行的第一个字符 "#" 代表键盘上的 Windows 键;所以 #space 表示在按住 Windows 键后再按空格键。"::" 后面的命令会在热键激活后执行,在本例中则会打开谷歌主页。继续按下面步骤操作,来执行这个脚本: 保存并关闭该文件。 双击该文件来启动它。在系统托盘里会出现一个新图标。 按下 Windows 和空格键,网页会在默认的浏览器里打开。 右击系统托盘里的绿色图标可以退出或编辑当前脚本。 注意: 可以同时启动多个脚本,并且在系统托盘里都会有一个相应的图标。 每个脚本都能定义多个 热键 和 热字符串 。 想让某个脚本开机即启动,可以将它的 快捷方式放到开始菜单的启动目录里 。 启动程序 命令 Run 可以运行程序、打开文档、网页链接或快捷键。请参看以下示例: Run Notepad Run C:\My Documents\Address List.doc Run C:\My Documents\My Shortcut.lnk Run www.yahoo.com Run mailto:someone@somedoma...

DAO层测试

<dependency> <groupId>com.wix</groupId> <artifactId>wix-embedded-mysql</artifactId> <version>2.1.4</version> <scope>test</scope> </dependency> 利用 wix-embedded-mysql 把MySQL嵌入到进程中,作为内存型的MySQL来做单元测试。 脚本: resources/migrations/mysql/<database>/<timestamp>_<action>.sql 但多个项目需要共享数据库脚本,可能可以用 git submodule 共享。

JavaScript中的字符串乘法

JavaScript中的字符串乘法 redraiment, Date 原文 原文地址: http://www.davidflanagan.com/2009/08/string-multipli.html 原作者:David Flanagan In Ruby, the "*" operator used with a string on the left and a number on the right does string repetition. "Ruby"*2 evaluates to "RubyRuby", for example. This is only occasionally useful (when creating lines of hyphens for ASCII tables, for example) but it seems kind of neat. And it sure beats having to write a loop and concatenate n copies of a string one at a time--that just seems really inefficient. I just realized that there is a clever way to implement string multiplication in JavaScript: String.prototype.times = function(n) {     return Array.prototype.join.call({length:n+1}, this); }; "js".times(5) // => "jsjsjsjsjs" This method takes advantage of the behavior of the  Array.join()  method for arrays that have undefined elements. But it doesn't even bother creating an array with n+1 undefined ele...