2013年9月21日星期六

linux shell脚本之if

无论什么编程语言都离不开条件判断。SHELL也不例外。
      if list then
          do something here
      elif list then
          do another thing here
      else
         do something else here
      fi   

例子:
#!/bin/sh
SYSTEM=`uname -s`    #获取操作系统类型,我本地是linux
if [ $SYSTEM = "Linux" ] ; then     #如果是linux的话打印linux字符串
echo "Linux"
elif [ $SYSTEM = "FreeBSD" ] ; then   
echo "FreeBSD"
elif [ $SYSTEM = "Solaris" ] ; then
echo "Solaris"
else
echo "What?"
fi     #ifend

基本上和其他脚本语言一样。没有太大区别。不过值得注意的是。[]里面的条件判断。

1 字符串判断
str1 = str2      当两个串有相同内容、长度时为真
str1 != str2      当串str1和str2不等时为真
-n str1        当串的长度大于0时为真(串非空)
-z str1        当串的长度为0时为真(空串)
str1           当串str1为非空时为真

2 数字的判断
int1 -eq int2    两数相等为真
int1 -ne int2    两数不等为真
int1 -gt int2    int1大于int2为真
int1 -ge int2    int1大于等于int2为真
int1 -lt int2    int1小于int2为真
int1 -le int2    int1小于等于int2为真

3 文件的判断
-r file     用户可读为真
-w file     用户可写为真
-x file     用户可执行为真
-f file     文件为正规文件为真
-d file     文件为目录为真
-c file     文件为字符特殊文件为真
-b file     文件为块特殊文件为真
-s file     文件大小非0时为真
-t file     当文件描述符(默认为1)指定的设备为终端时为真

3 复杂逻辑判断
-a         与
-o        或
!        非

Linux中变量$#,$@,$0,$1,$2,$*,$$,$?的含义 - Websense - 51CTO技术博客

先写一个简单的脚本,执行以后再解释各个变量的意义
 
# touch variable
# vi variable
 
脚本内容如下:
 
#!/bin/sh
echo "number:$#"
echo "scname:$0"
echo "first :$1"
echo "second:$2"
echo "argume:$@"
echo "show parm list:$*"
echo "show process id:$$"
echo "show precomm stat: $?"
保存退出
 
赋予脚本执行权限
 
# chmod +x variable
 
执行脚本
 
# ./variable aa bb
number:2
scname:./variable
first:aa
second:bb
argume:aa bb
show parm list:aa bb
show process id:24544
show precomm stat:0
 
通过显示结果可以看到:
 
$# 是传给脚本的参数个数
$0 是脚本本身的名字
$1 是传递给该shell脚本的第一个参数
$2 是传递给该shell脚本的第二个参数
$@ 是传给脚本的所有参数的列表
$* 是以一个单字符串显示所有向脚本传递的参数,与位置变量不同,参数可超过9个
$$ 是脚本运行的当前进程ID号
$? 是显示最后命令的退出状态,0表示没有错误,其他表示有错误

2013年8月27日星期二

DD-WRT 固件脚本添加与修改

一.在 \fmk\rootfs\etc\config 目录下新建文件 nocat_liht.sh :

#!/bin/sh
status=`/bin/ps | /bin/grep splashd | /bin/grep -v grep | /bin/wc -l`
#取得nocat状态,必须为1,不能是0,也不能是2
    if [ $status != 1 ];
    then
    /usr/sbin/splashd
   # date >> /tmp/nocat.log #记录出错时间
   # echo "liht_nocat auto restarted" >> /tmp/nocat.log
    else
    exit
    fi

注意:需要管理员登陆linux才能修改rootfs目录,保存后记得修改文件属性为可执行。

二. 修改 \fmk\rootfs\etc\config 目录下的nocat.startup文件,添加如下内容:

if [ "$(nvram get NC_enable)" = "1" ]; then
  * * * * * root /etc/config/nocat_liht.sh
fi

 意思是,如果使用了nocat,就每分钟运行一次脚本。


nocat.sh 
Code:
#!/bin/sh 
X=1 
Y=status 

while [ $X -eq 1 ] 

do 
   sleep 1000 
   cd /tmp 
   sleep 7 
   webup=`ls status` 
     if [ "$Y" != "$webup" ]; then 
          killall wget 
          sleep 5 
          killall splashd 
          echo "nocat restarted" >> nocat.log 
          sleep 20 
          splashd 
          sleep 5 
       else 
          # echo "status deleted" >> nocat.log 
          rm /tmp/status 
     fi 

done 

192.168.2.241 is the ip of the dd-wrt on the lan side 
this check if the status nocat page is working otherwise restarts nocat. The killall comand could be a reboot command if the proccess cannot be killed (depending on the enviroment may be better...). 

I put the script in the startup. 
Code:
chmod +x /tmp/nocat.sh 
/tmp/nocat.sh &

where anyserver is a webserver containing nocat.sh, and accesible for the router 

This work for me, every 24-48 hours nocat need to be restarted. Sometimes nocat depending on global configuration may be too unstable, and the script does not help very much. 

进入 DD-WRT 网页管理界面,Administration -> Commands,贴入如下脚本Save Startup:
mkdir /tmp/myscripts
    /bin/echo ‘#!/bin/sh
    status=`/bin/ps | /bin/grep splashd | /bin/grep -v grep | /bin/wc -l`
    #echo $status

    if [ $status = 0 ];
    then
    /usr/sbin/splashd >> /tmp/nocat.log 2>&1 &
    else
    exit
    fi’ >> /tmp/myscripts/monitor_splash
    /bin/chmod +x /tmp/myscripts/monitor_splash

    /bin/echo ‘ * * * * * root /tmp/myscripts/monitor_splash’ >> /tmp/crontab

    # restart cron daemon
    stopservice cron && startservice cron 

dd-wrt计划任务cron

由于CRON执行命令,至少需要当前时间,DD-WRT的设备的日期和时间通常应设置运行在【NTP】。有时间和日期设置是如此重要,对NTP的配置是直接在位于下基本/设置基本法第一次设置页。

您可以通过GUI查看到当前的日期和时间:

【状态】->【路由器信息】下的【当前时间】(English:Status/Router/Current Time)
您还可以【管理】->【命令】->【指令解释器】(即shell)界面中输入'date'命令。
确定Cron/Crond 为启用状态.



只需要在“管理”页面中,开启cron。然后输入你要执行的开、关Radio的命令:

40 0-6 * * *  root /usr/sbin/wl radio off
0 8 * * *  root /usr/sbin/wl radio on
基本和Linux下的命令一致,只是要注意的是,要在执行的任务前面加上root,才能真正执行。

第一行的含义是在每天晚上12点40到清晨6点40,每隔一个小时执行一次关闭无线的命令。为什么要执行6次呢?因为本人有时候是网迷

,cron执行关闭后,我还会手工打开无线。所以多关闭几次折磨自己。
第二行的含义是在早上8点整打开路由器的无线发射。

分钟、小时、第几天、第几月、每周的第几天
# +---------------- 分钟 (0 - 59)
# |  +--------------小时 (0 - 23)
# |  |  +-----------日 (1 - 31)
# |  |  |  +-------- 月 (1 - 12)
# |  |  |  |  +------每周的第几天 (0 - 6) (周日=0 或 7)
# |  |  |  |  |
*/5 * * * * root /jffs/auto3322.sh
# 每5分钟执行一次
# chmod 777 auto3322.sh

linux下面有atd和crond两种计划任务,其中,atd服务使用的at命令只能执行一次,而crond服务使用的crontab定义的命令,是循环作

用的,所以crond才符合我们的要求。
 crontab支持两种状态:一、直接编写计划任务;二、使用目录的方式,放在目录里面的都会定时执行。

在#!/bin/sh的情况下,source不成功,不会运行source后面的代码。
在#!/bin/bash的情况下,虽然source不成功,但是还是运行了source后面的echo语句。

2013年8月22日星期四

TOMATO/DD-WRT/DUALWAN成功设置中文SSID的方法(包括中继SSID

先telnet到路由器:telnet 192.168.1.1

以下命令TOMATO/DD-WRT/DUALWAN都一样

设置命令如下
nvram set wl_ssid="我的热点"
nvram commit 

说明1:其中的中文最好在命令行里输入,不要复制,两边的引号为半角,不要写错了。
说明2:命令行中的中文(即SSID)输入后应正常显示的,如果为乱码,则可能会导致设置不成功。
说明3:第2条命令执行完,需要等几秒钟,然后会提示:commit...done!表示设置成功。
说明4:设置完成后,DD/TT/DW的页面都会显示为乱码。
说明5:WinXP的无线列表里会显示正常的中文,但连接后的右下角会显示乱码。vista/win7显示正常。
说明5:4,5中的中文显示问题,均不影响正常使用。(呵呵,这条貌似多余?)
说明6:设置完成后,须重起路由器,以便客户端识别新的SSID。

中继后的设置命令如下:
nvram set wl0_ssid="TP-LINK" (其它路由器的SSID)
nvram set wl0.1_ssid="我的热点" (自己路由的SSID)
nvram commit
reboot

dd-wrt中文SSID无线连接名称网络设置方法

两种修改方法任你选
 
  1.浏览器法
 
  设备要求:设备要求比较低,即使没有为无线路由器刷DD-WRT或Tomato等第三方固件也可使用该方法将SSID修改为中文。
 
  固件是DD-WRT的无线路由器,在修改无线网络SSID信息为中文名并保存时提示“无线网络名(SSID)包含非法ASCⅡ码”。这是因为无线路由器管理界面由JSP编写而成,而很多JSP语句对中文字符的支持不好,使用浏览器法的目的就是让无线路由管理界面支持中文编码。
 
  第一步:由于很多无线路由器管理界面都是通过JSP或JAVA程序制作的,所以可通过禁止加载JAVA组件的方式来解除设备对中文SSID无线网络设置的限制。由于默认情况下IE浏览器没有相关功能,只能使用Firefox浏览器来解决。首先通过Firefox浏览器访问无线路由器管理界面,默认情况下会出现乱码。
 
  第二步:在Firefox浏览器的“字符编码”中将语言设定为“中文”后可解决乱码问题。同时在Firefox浏览器中取消对“启用javascript”选项的选择。
 
  第三步:重新刷新管理页面,再次输入中文SSID无线网络设置信息并保存,将不再出现“非法ASCⅡ码”的提示。
  第四步:保存完毕后,通过操作系统的无线扫描功能,可看到SSID修改为中文字符的无线网络。设置完成后,要重新选中Firefox浏览器的“启用javascript”选项,以免日后浏览其他页面时受到影响。

  2.Telnet法
 
  设备要求:此方法要求无线路由器开启了Telnet服务即可,不过并不是所有无线设备都默认开启该服务,在进行操作前需要确定该服务开启。
  只有开启了Telnet服务的无线路由器才能够使用此方法进行设置。
  第一步:一般情况下,刷了DD-WRT或Tomato等第三方固件的无线路由器都会默认开启Telnet功能,而部分D-Link、Linksys无线路由器也有相关功能。只需要通过命令提示符窗口输入“Telnet 无线路由器管理地址”进行检测即可,如果出现要求输入用户名和密码的提示,就说明该功能已经开启。
 
  提示:在Telnet状态下输入的用户名永远是root,而密码则和图形化管理界面中的账户密码一致。
 
  第二步:通过Telnet进入无线路由器命令行管理界面后,可通过nvram set wl_SSID=“中文SSID名称”命令来设置一个中文SSID,例如笔者输入的是nvram set wl_SSID=“阮征”,那么会设置SSID广播网络信息为“阮征”。
 
  第三步:进行修改后,可以实时通过图形管理界面查看SSID信息的更改,不过会出现乱码,不用理会即可。
 
  第四步:接下来在命令行管理界面使用nvram commit命令保存做过的设置,最后通过reboot命令进行重启。

2013年8月20日星期二

修改DD-WRT固件,添加删除功能

Optional steps for users with "Mega" (like me)

The steps below are only applicable if you used the Mega version as your base.
While we have used the "mini" version up until now, some of you may be in possession of a "Mega" capable router (like me).

Personally I found that while Mega has some really nice packages integrated into it out of the box, some are completely useless for my setup, and just take up precious potential JFFS space.

The packages include:

Warning: we will delete everything to do with these packages other then the web pages. this is because, for various reasons, DD-WRT pre-compiles the ASP pages directly into the apache binary, making it impossible to remove them with the mod-kit alone. (yes this was deliberate :\ ).
Therefore it is up to you to remember not to use the web-GUI options, for the packages above.
If your uncertain that you can do this, you may opt to use the "standard" version as your base instead of Mega, or simply skip this procedure.

To delete the packages (despite leaving the web-GUI configurations in place) issue the following commands:
rm ./working_dir*/rootfs/usr/sbin/sputnik  rm ./working_dir*/rootfs/usr/sbin/splashd  rm ./working_dir*/rootfs/usr/sbin/pound  rm ./working_dir*/rootfs/usr/sbin/kaid  rm ./working_dir*/rootfs/usr/sbin/chilli     for i in $( find ./ -name *milkfish* ); do ll $i ;rm -rfv $i ; done  for i in $( find ./ -name *asterisk* ); do ll $i ;rm -rfv $i ; done  for i in $( find ./ -name *openser* ); do ll $i ;rm -rfv $i ; done

原文地址:

http://hotfortech.wikispaces.com/DD-WRT+mod-kit+home+setup