由于懒放弃写自行管理网络的脚本在archlinux官网找到一个软件netcfg2借助这个软件完成了网络控制的部分。

netcfg是需要自己去修改配置文件。我新增的功能是直接搜索到wifi后按数字键选择然后在输入密码就可以了。

也就不用在去编辑任何文本文件。

懒人万岁 

┌─{codewalker}<=Mon Apr 30-12:48 AM=>

|=>/tmp

└─$cat ~/my_script/start_web_server.sh 

#!/bin/bash

#writen by codewalker

#email xyy.xx.victor@gmail.com

#qq 58626619

#Modify date: 2012-4-30 

#update 1.3 comlete network control with netcfg2

#update 1.2 add startx function,ifi control and NOT comlete

#update 1.1 add goagent proxy control

#version 1.0 web server,php,varnish,mysql control

USAGE(){

cat <<EOF

        1:start php,nginx,varnish;

        2:stop php,nginx,varnish;

        3:restart php,nginx,varnish;

        4:online&startx;

        5:contact wifi;

EOF

}

function go(){

    startx

}

function online (){

    echo ‘get ip’;

    sudo dhcpcd eth0

    echo ‘starting goagent…’;

    sudo /etc/rc.d/goagent start

    echo ‘starting dnsmasq…’;

    sudo /etc/rc.d/dnsmasq start

}

function start_php_nginx_varnish (){

    echo ‘starting php-fpm…’;

    sudo /etc/rc.d/php-fpm start

    echo ‘starting nginx…’;

    sudo /etc/rc.d/nginx start

    echo ‘starting varnish’;

    sudo varnishd -f /etc/varnish/default.vcl -s malloc,1G -T 127.0.0.1:2000 -a 0.0.0.0:8080

    sudo /etc/rc.d/varnish start

}

function stop_php_nginx_varnish (){

    echo ‘stop webserver’;

    sudo /etc/rc.d/php-fpm stop

    sudo /etc/rc.d/nginx stop 

    sudo /etc/rc.d/varnish stop

    sudo pkill varnish

}

function restart_php_nginx_varnish (){

    stop_php_nginx_varnish

    start_php_nginx_varnish

}

 function mysql_control(){

    echo “you chose $@ mysql”

    case “$1” in

     #control mysql case statement

    

    START|start) echo “now start mysql”

        sudo /etc/rc.d/mysqld start;;

    

    STOP|stop) echo “now stop mysql”

        sudo /etc/rc.d/mysqld stop;;

    

    RESTART|restart)

        sudo /etc/rc.d/mysqld restart;;

    

    q|Q) done=”true” #ends while loop 

        ;;

     *)   echo “Invalid option, choose again…”;;

   esac

}

count_input=0

 while [ “$switch” == ‘’ ]; do

     read -p “what are you doing?” switch

     ((count_input++))

    # echo $count_input

     if [ $count_input -ge 3 ];then

        USAGE

     fi

     if [ $count_input -ge 5 ];then

         echo ‘are u nuts!!!’

         mytime=5

         less_time=10

         while [ $mytime -ge -4 ];do

             sleep 1

             ((less_time–))

             echo $less_time

             ((mytime–))

         done

         echo ‘quitting….’

         exit 0

     fi

 done

             

if [ “$switch” == “1” ];then

     start_php_nginx_varnish

     read -p ‘Do you start mysql(type:1=yes or 2=no)’ is_start_mysql

     if [ “$is_start_mysql” == “1” ];then

         mysql_control ‘start’

     fi

fi

if [ “$switch” == “2” ];then

    stop_php_nginx_varnish

    read -p ‘Do you stop mysql(type:1=yes or 2=no)’ is_start_mysql

    if [ “$is_start_mysql” == “1” ];then

         mysql_control ‘stop’

    fi

fi

if [ “$switch” == “3” ];then

    restart_php_nginx_varnish

    read -p ‘Do you restart mysql(type:1=yes or 2=no)’ is_start_mysql

    if [ “$is_start_mysql” == “1” ];then

        mysql_control ‘restart’

    else

        echo ‘i chose no’;

        exit 1

    fi

fi

if [ “$switch” == “4” ];then

    online;

    read -p “go?(1=yes or 2=no)” is_go

    if [ “$is_go” == “1” ];then

        go;

    else

        echo ‘You chose NOT startx’;

        exit 1

    fi

fi

if [ “$switch” == “5” ];then

    _net_config_file=’/tmp/wireless-cfg’;

    #sudo ifconfig wlan0 up;

    echo ‘scan wifi…’;

    if [ ! -f /tmp/enable_wifi ];then

        sudo iwlist wlan0 scan|grep ESSID:|cut -d’”‘ -f2 > /tmp/enable_wifi;

    fi

    tc=$(cat /tmp/enable_wifi|wc -l);

    echo -n “You have $tc  enable wifi to use”;

    echo ‘ ‘;

    awk ‘{num=num+1; printf”%d:%sn”, num, $0}’ /tmp/enable_wifi

    #rm /tmp/enable_wifi

    read -p “Chose which one?    “ index_of_wifi_ESSID

    cw=$(sed -n “$index_of_wifi_ESSID p” /tmp/enable_wifi);

    echo  “Now you contact to $cw”;

    if [ -f $_net_config_file ];then

        echo ‘’ > $_net_config_file;

        echo ‘the $_net_config_file is cunzai,re it’;

    else

        touch $_net_config_file;

        echo ‘this is no $_net_config_file, touch it’;

    fi

    echo “ESSID=’$cw’” >> $_net_config_file;

    read -p “Please type your wireless_password==>    “ wireless_password

    echo “KEY=’$wireless_password’” >> $_net_config_file;

    echo “INTERFACE=’wlan0’” >> $_net_config_file;

    echo “IP=’dhcp’” >> $_net_config_file;

    echo “SECURITY=’wpa’”>> $_net_config_file;

    sudo mv /tmp/wireless-cfg /etc/network.d/wireless.cfg;

    sudo chmod 644 /etc/network.d/wireless.cfg;

    sudo chown root.root /etc/network.d/wireless.cfg;

    sudo netcfg2 wireless.cfg

fi