编写一个开机自启动路由服务,拿走不谢!

第一步、

进入/etc/init.d目录,创建一个名字叫route文件(也可以copy该目录下任意一个文件作为我们的模板,修改一下即可),先编辑如下内容:
 

 #!/bin/sh
 # route This is a route  ----单纯的一个纯注释   
 # chkconfig:   - 99 10   
 # description: route

chkconfig: 设置的三个值是必须的。这个跟我们的启动服务顺序有关。这行告诉chkconfig缺省启动的运行级以及启动和停止的优先级。如果某服务缺省不在任何运行级启动,那么使用-代替运行级。数字越低优先级越高。本例中,开机时由于路由设置与其他服务的依赖关系不大,放在后面启动(设置为99)。相应的,开机最后启动的服务应在关机的时候最先关闭(避免依赖关系导致的服务无法关机)(设置为10)。

description这一行也是必不可少的。虽然这行是注释行,但其他文件会截取这行内容进行判断等处理。

第二步、编辑route文件

#!/bin/sh
# route This is a route
#
# chkconfig:   - 99 10
# description: route
. /etc/rc.d/init.d/functions   //方便我们调用action函数,实现绿色的OK,红色的false
#开启服务
start(){

	route add -net 1.1.1.0/24 gw 192.168.32.167 #添加路由表
	action "Route set OK!" true                 #显示红色OK

}
#关闭服务
stop(){   
		route del -net 1.1.1.0/24        #删除路由
		action "route already delete!" true
}
#stop:关闭路由服务  start:开启路由服务 restart:重启路由服务 status:列出路由表
case $1 in
	stop)stop;;
	start)
	if route -n |grep 1.1.1.0 &> /dev/null ;then
		action "route already exit!" false
		exit 1;
	else
		start
	fi
	;;
	restart)stop;start;;
	status)route -n;;
	*)echo "error";;
esac

第三步、增加服务

1、在增加服务前,我们先查看一下/etc/rc?.d

ls /etc/rc?.d/*route*

啥也没有就对了!因为我们还没有增加服务呢!

2、然后我们添加服务

chkconfig --add route

ls /etc/rc?.d/*route*   //再查看一下/etc/rc?.d/有没包含route的文件

/etc/rc0.d/K10route
/etc/rc1.d/K10route
/etc/rc2.d/K99route
/etc/rc3.d/K99route
/etc/rc4.d/K99route
/etc/rc5.d/K99route
/etc/rc6.d/K10route

哈哈有文件了!

3、

chkconfig route on ---在chkconfig工具服务列表中增加此服务,此时服务会被在/etc/rc.d/rcN.d中赋予K/S入口了;

chkconfig route on
ls /etc/rc?.d/*route* -1
/etc/rc0.d/K10route
/etc/rc1.d/K10route
/etc/rc2.d/S99route
/etc/rc3.d/S99route
/etc/rc4.d/S99route
/etc/rc5.d/S99route
/etc/rc6.d/K10route

对比一下上一步,文件名的K、S发生了变化。离成功就差最后一步了!

最后一步、重启

reboot

重启完成后,查看一下路由表吧   ^_^

[root@localhost init.d]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.32.0    0.0.0.0         255.255.255.0   U     1      0        0 eth1
1.1.1.0         192.168.32.167  255.255.255.0   UG    0      0        0 eth1
172.18.0.0      0.0.0.0         255.255.0.0     U     0      0        0 ethmage
169.254.0.0     0.0.0.0         255.255.0.0     U     1002   0        0 ethmage
0.0.0.0         172.18.0.1      0.0.0.0         UG    0      0        0 ethmage

1.1.1.0/24路由已经添加上去啦。

注:您也可以尝试着用service命令开启或者关闭服务

[root@localhost init.d]# service route start
route already exit!                                        [FAILED]
[root@localhost init.d]# service route stop
route already delete!                                      [  OK  ]

 

有没有受到启发^_^试着做做其他的开机自启服务吧。

 

如有疑问,联系QQ:1930818140  大家可以一起多多交流

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
http://blog.csdn.net/xiaoxiao108/article/details/31398127 现在用有线连网看视频速度要比经过路由器快,用无线做热点,但无线设置热点后,关机或重启又要再重新开启热点,这样比较麻烦,增加上开机自启热点,这样比较方便了,连网速度快看视频不卡其它设备也可以通过盒子上网。 开发工具 android ADT 实现方法 1.开启热点 2.开机启动 具体代码 1.开启热点 [java] view plaincopy在CODE上查看代码片派生到我的代码片 WifiManager wifiManager; wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE); Method method = wifiManager.getClass().getMethod("getWifiApState"); int state = (Integer) method.invoke(wifiManager); if(state==13||state==12)// 已经开 或者 正在开 { }else { Toast.makeText(MainActivity.this,"正在开启AndroidAP...", 1).show(); wifiManager.setWifiEnabled(false); WifiConfiguration apConfig = new WifiConfiguration(); apConfig.SSID = "AndroidAP"; apConfig.allowedKeyManagement.set(4); apConfig.preSharedKey = "5e8918f37260"; method = wifiManager.getClass().getMethod("setWifiApEnabled", WifiConfiguration.class, Boolean.TYPE); boolean open = (Boolean) method.invoke(wifiManager, apConfig, true); } AndroidManifest.xml 中增加权限 2.开机启动 用了用 在我的电视盒子上试了试 发现 偶尔会出现 开机不自动运行的情况 换个思路把 用 替代 电视盒子自带的 桌面 开启AP热点后再 自动运行盒子自带的桌面 [java] view plaincopy在CODE上查看代码片派生到我的代码片 ComponentName componet = new ComponentName("com.duokan.duokantv","com.duokan.duokantv.MainActivity"); Intent i = new Intent(); i.setComponent(componet); startActivity(i); 使用方法 1.电视盒子连有线网 2.配置好盒子正常上网 3.安装自己写好的程序 如果你发现有什么不合理的,需要改进的地方,请留言。或者可以通过 328452421@qq.com 联系我,非常感谢。 http://blog.csdn.net/xiaoxiao108/article/details/31398127
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值