设为首页
收藏本站
用户名
Email
自动登录
找回密码
密码
登录
注册
快捷导航
平台
Portal
论坛
BBS
文库
项目
群组
Group
我的博客
Space
搜索
搜索
热搜:
shell
linux
mysql
本版
用户
LinuxSir.cn,穿越时空的Linuxsir!
»
论坛
›
Linux 综合讨论区 —— LinuxSir.cn
›
shell进阶应用、shell编程
›
一个连网的实用脚本
返回列表
查看:
4983
|
回复:
4
一个连网的实用脚本
[复制链接]
kiron
kiron
当前离线
积分
1261
IP卡
狗仔卡
发表于 2003-12-27 00:10:28
|
显示全部楼层
|
阅读模式
脚本的使用范围:对于校园网的mac绑定的拨号用户更改mac并拨号上网时使用
特点:脚本做得就像一个实用工具一样,非常方便,我做了两个版本,一个是用于X,一个用于字符界面,在X下像应用程序,需要要Xdialog支持,若没有安装Xdialog,把下面第一个脚本中的Xdialog的X去掉,可以在字符下使用dialog代替。在字符界面下能给出详细的连接情况,连接结果。
脚本测试环境:在redhat9.0和redflag4.0下测试通过
下面这个脚本如果在桌面上建立一个启动器,双击后就弹出一个对话框,选择要上网的帐号,就可以连上网了,用起来感觉就像应用程序一样
#!/bin/bash
#chmac--change mac address
#this script for change my netcard mac address for enter Internet.
MAC1=00:60:7c:01:1f:ab
#MAC1和2对应拨号ppp1和ppp0,而MAC3只是改为
MAC2=00:40:4c:51:d2:26
#一个不对应帐号的MAC地址
MAC3=00:40:5d:01:1f:ac
#这个函数用来调用脚本出错时显示脚本的使用方法
usage()
{
echo "Usage:chmac [1|2|3]
option 1:You can use accounter ACCOUNT1,
2:You can use accounter ACCOUNT2.
3:Only change your netcard mac address."
exit 1
}
tmpfile="/tmp/tmp.$$"
#建立空的临时文件,在X下的虚拟终端中敲入脚本名不带
#参数时,启动对话框,要求选择一个要拨的帐号,并把选择结果保存在建立的临#时文件里
if [ $# == 0 ];then
Xdialog --title "Choose accounter" --menu "Which accounter you want to use?" 0 0 0 1 ACCOUNT1 2 ACCOUNT 3 "Just change mac" 2>$tmpfile
ANS=`cat $tmpfile`
rm -f $tmpfile
#把选择结果显示赋给变量ANS,并删除临时文件
fi
if [ $# == 1 ];then
#如果参数只有一个,检查是否为1-3内的值,1表用帐号1拨号ANS=$1
#2表用帐号2拨号,3表只是更改网卡地址,不拨号上网
ANS=$1
elif [ $# == 0 ];then
:
else
usage
#如果给出多于一个参数,调用出错信息,错误代码为1退出
fi
case $ANS in
#这个选项可以用来把帐号让给其他机器上网,而不会有MAC冲突
1|2|3) :
;;
*) usage
;;
esac
echo "Please wait..."
adsl-stop>/dev/null 2&>1
#若正在上网,先断网,再进行下述动作
/sbin/ifconfig eth0 down
case $ANS in
1) /sbin/ifconfig eth0 hw ether $MAC1
#更改为MAC1的地址,再拨号上网
/sbin/ifconfig eth0 up
adsl-start /etc/sysconfig/network-scripts/ifcfg-ppp1
;;
2) /sbin/ifconfig eth0 hw ether $MAC2
#更改为MAC2的地址,再拨号上网
/sbin/ifconfig eth0 up
adsl-start
;;
3) /sbin/ifconfig eth0 hw ether $MAC3
/sbin/ifconfig eth0 up
;;
*) usage
#出错,退出脚本,给出错误代码1
;;
esac
/root/bin/networktest
#调用另外一个我写的网络情况测试脚本,我把它放在了
#/root/bin/目录下,脚本名为networktest,然后退出代码0表脚本成功执行
exit 0
复制代码
下面是没有任何图形的脚本,原理是一样的,有小小改动,那就不注释了
#!/bin/bash
#chmac--change mac address
#this script for change my netcard mac address for enter Internet.
MAC1=00:60:7c:01:1f:ab
MAC2=00:40:4c:51:d2:26
MAC3=00:40:5d:01:1f:ac
usage()
{
echo "Usage:chmac [1|2|3]
option 1:You can use accounter ACCOUNT1,
2:You can use accounter ACCOUNT2.
3:Only change your netcard mac address."
exit 1
}
if [ $# == 0 ];then
echo -n "Which accounter you want to use[1)ACCOUNT1,2)ACCOUNT,3)Just change mac]:"
while :
do
read ANS
case $ANS in
1|2|3) break
;;
*) echo -n "Please Enter accounter number[1)ACCOUNT1,2)ACCOUNT2,3)only change mac]:"
;;
esac
done
fi
if [ $# == 1 ];then
ANS=$1
elif [ $# == 0 ];then
:
else
usage
fi
echo "Please wait..."
adsl-stop>/dev/null 2&>1
/sbin/ifconfig eth0 down
case $ANS in
1) /sbin/ifconfig eth0 hw ether $MAC1
/sbin/ifconfig eth0 up
adsl-start /etc/sysconfig/network-scripts/ifcfg-ppp1
;;
2) /sbin/ifconfig eth0 hw ether $MAC2
/sbin/ifconfig eth0 up
adsl-start
;;
3) /sbin/ifconfig eth0 hw ether $MAC3
/sbin/ifconfig eth0 up
;;
*) usage
;;
esac
/root/bin/networktest
exit 0
复制代码
上述两个脚本中调用了我写过的一个ADSL网络工作情况报告的脚本,曾经贴出过,现在一并贴出:
#!/bin/bash
#networktest
#this script for test my network status
adsl-status >/dev/null 2>&1 #ADSL成功连接状态返回0,断网状态则是1
case $? in
0) echo "Your network run well!!"
;;
1) ping -c 6 192.168.X.X >/dev/null 2>&1 #这里192.168.X.X是你的本地IP或你本
#地网络的其他主机IP也可以
if [ $? = 0 ] #本地网成功ping则返回0,错误返回1
then
echo "Your adsl is cut!!"
else
echo "Your local network is cut!!"
fi
;;
*) echo "Your network configure error!!"
;;
esac
复制代码
请兄弟们多多指教,:thank
回复
使用道具
举报
提升卡
置顶卡
沉默卡
喧嚣卡
变色卡
显身卡
KornLee
KornLee
当前离线
积分
6960
IP卡
狗仔卡
发表于 2003-12-27 00:48:53
|
显示全部楼层
精彩~~,等我安了ADSL一定把这些脚本都试试~~
加
精
回复
支持
反对
使用道具
举报
显身卡
cycker
cycker
当前离线
积分
169
IP卡
狗仔卡
发表于 2005-7-30 14:14:29
|
显示全部楼层
#!/bin/sh
#ping两个dns,如果两个都断了,就重新启拨号
#把它加到crontab中,每两分钟执行一次,可保网络畅通 :)
ping 202.96.128.68 -c1
r1=$?
ping 202.96.134.133 -c1
r2=$?
echo -n `date +"%Y-%m-%d %H:%M"` " check connection: " >> /root/adsl.log
if [ $r1 -eq 1 ] && [ $r2 -eq 1 ] ;then
echo "...[DEAD]" >> /root/adsl.log
echo -n " stop adsl connection..." >> /root/adsl.log
poff
echo "done">> /root/adsl.log
echo -n " start adsl connection..." >> /root/adsl.log
pon dsl-provider &
sleep 10
grep=`ifconfig | grep "
-t-P" | sed -e "s/[-a-zA-Z ]//g" -e "s/:/\//g" | cut -d/ -f2,3`
if [ "x${grep}" != "x" ];then
echo "success,address is $grep" >> /root/adsl.log
else
echo "faild">> /root/adsl.log
fi
iptables -t nat -A POSTROUTING -o ppp0 -j MASQUERADE
echo 1 > /proc/sys/net/ipv4/ip_forward
else
echo "...[OK]" >> /root/adsl.log
fi
回复
支持
反对
使用道具
举报
显身卡
Zer4tul
Zer4tul
当前离线
积分
1070
IP卡
狗仔卡
发表于 2005-8-2 21:17:10
|
显示全部楼层
这个不错,赞一下,呵呵。好帖
回复
支持
反对
使用道具
举报
显身卡
alucard_ad
alucard_ad
当前离线
积分
108
IP卡
狗仔卡
发表于 2007-6-13 23:05:01
|
显示全部楼层
佩服你的技术 我一直都在学习写脚本但是就是学不懂。。。
回复
支持
反对
使用道具
举报
显身卡
返回列表
高级模式
B
Color
Image
Link
Quote
Code
Smilies
您需要登录后才可以回帖
登录
|
注册
本版积分规则
发表回复
回帖后跳转到最后一页
浏览过的版块
BSD 讨论专题
网络技术\网络安全讨论
Archlinux讨论区
Debian Linux
硬件设备 Linux 驱动
Redhat/Fedora/CentOS Linux
Gentoo Linux
LFS(LinuxfromScratch)
Copyright © 2002-2023
LinuxSir.cn
(http://www.linuxsir.cn/) 版权所有 All Rights Reserved.
Powered by
RedflagLinux!
技术支持:
中科红旗
|
京ICP备19024520号
快速回复
返回顶部
返回列表