Skip to content

Instantly share code, notes, and snippets.

@netwjx
Created April 28, 2015 08:13
Show Gist options
  • Save netwjx/26ac42cdf7a475402f26 to your computer and use it in GitHub Desktop.
Save netwjx/26ac42cdf7a475402f26 to your computer and use it in GitHub Desktop.
路由更新
routes =
'192.168.0.0': '255.255.0.0'
Future = require 'fibers/future'
child_process = require 'child_process'
iconv = require 'iconv-lite'
Future.task ->
stdout = exec 'route print'
IF = IF_NAME = 0
stdout.replace /^ (\d+).+\.([\w\(\)]+ Ethernet .+)$/m, (_, $1, $2)->
IF = $1
IF_NAME = $2
stdout = exec 'ipconfig'
NW = 0
if ~(n = stdout.indexOf 'qunarservers.com') and
~(n = stdout.indexOf '默认网关', n) and
~(n = stdout[n..].match /\d+\.\d+\.\d+\.\d+/)
NW = n[0]
console.log "网关:#{NW} 接口:#{IF} #{IF_NAME}"
stats = for own IP, MASK of routes
execFuture "route add #{IP} MASK #{MASK} #{NW} IF #{IF}", encoding: 'hex'
do s.wait for s in stats
console.log '完成'
.detach()
execFuture = Future.wrap child_process.exec, 'multi'
exec = (cmd)->
[stdout] = execFuture cmd, encoding: 'hex'
.wait()
iconv.decode new Buffer(stdout, 'hex'), 'gbk'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment