顯示具有 Bash Script 標籤的文章。 顯示所有文章
顯示具有 Bash Script 標籤的文章。 顯示所有文章

2011年12月26日 星期一

製作程式 (網頁看IP、user)

建立檔案
$nano mkhome.sh


echo "<html><head></head><body>" > /var/www/index.html

echo "<pre>" >> /var/www/index.html

/home/student/ipconfig.sh >> /var/www/index.html

/home/student/ug.sh >> /var/www/index.html

echo "</pre>" >> /var/www/index.html

echo "</body></html>" >> /var/www/index.html


修改權限
$chmod +x mkhome.sh




執行程式
$./mkhome.sh

開啟瀏覽器,並輸入首頁網址

http://192.168.1.10

就可以看見執行程式的結果輸出到網頁



備註:在student家目錄下必須要有 ipconfig.sh 、 ug.sh 此執行檔才會生效

檢視使用者名單

建立檔案
$nano ug.sh
   
            u=$( cat /etc/passwd | cut -d ':' -f1 )
            g=$( cat /etc/group | cut -d ':' -f1 )

            echo '[ user ]'
            echo $u

            echo '[ group ]'
            echo $g

增加執行權限
$chmod +x ug.sh

執行檔案
$./ug.sh

撰寫程式 產生10名使用者

建立使用者名單 users.txt

 $touch users.txt

 $nano users.txt

把10名使用者及密碼寫上(可自行設定)

a001:student
a002:student
a003:student
a004:student
a005:student
a006:student
a007:student
a008:student
a009:student
a010:student


編寫自動產生使用者的程式,名字為 useradd.sh

$touch useradd.sh

$chmod +x useradd.sh

$nano useradd.sh

編寫程式如下:

[ ! -f users.txt ] && echo "file not found" && exit 1
 
 
s=$(cat users.txt)
 
 
for  us  in $s
do
    uname=${us%%:*}
 
 
    upasswd=${us##*:}
 
 
    useradd -m -s /bin/bash $uname
    echo "$uname:$upasswd" | chpasswd

    mkdir  /home/$uname/www
    chown "$uname:$uname" /home/$uname/www

    echo "$uname home" >  /home/$uname/www/index.html
    chown "$uname:$uname"  /home/$uname/www/index.html
 
done


執行程式
$sudo ./useradd.sh

2011年12月12日 星期一

新增、刪除多重目錄

$nano dir.sh

if [ -d ./test1 ] ; then                            --條件式,如果 test1 此目錄存在

      rm -r ./test1                                  --則刪除 test1

else                                                    --否則

      mkdir -p test1/test2/test3              --建立多重目錄

fi                                                        --條件式 if 的結尾

$chmod +x dir.sh
$./dir.sh

檢視網路設定

$nano ipconfig.sh                          --編輯檔案

ifconfig -a | grep -A1 eth0              --只檢視 eth0 的前段
ifconfig -a | grep -A1 eth1              --只檢視 eth1 的前段
netstat -rn                                      --檢視預設閘道
cat /etc/resolv.conf                        --檢視DNS

$chmod +x ipconfig.sh                  --修改權限為可執行檔
$./ipconfig.sh                                --執行檔案