2010年9月13日 星期一

Shell script 練習(while do...done,for do...done)

<1>mywhile.sh
#由0開始+1到值變為10後停止
#!/bin/bash
myvar=0
while [ $myvar -ne 10 ]
do
        echo $myvar
        myvar=$(( $myvar + 1 ))
done
exit 0

執行方式:
$./mywhile.sh

修正版:
a.
#修正為手動輸入參數,判斷是否為數字0-9
#~/bin/bash
myvar="$1"
for (( i=0; i<=9; i=i+1 ))
do
        if [ "$i" == "$myvar" ]; then
                while [ $myvar -ne 10 ]
                do
                        echo $myvar
                        myvar=$(( $myvar + 1 ))
                done
        fi
done
echo "Please input num 0-9"
exit 0

執行方式:
1.$./mywhile.sh 12515    出現錯誤訊息
2.$./mywhile.sh abc    出現錯誤訊息
3.$./mywhile.sh 5     正確執行,在畫面上秀出5,6,7,8,9

b.
#!/bin/bash
[ "$#" != "1" ] && echo "parameter too much" && exit 1
myvar="$1"

s=$(echo $1 |tr -d 0-9)
[ -n "$s" ] && echo "$myvar not number" && exit 1

if [ $myvar -gt "9" -o $myvar -lt "0" ]; then
        echo "Please input 0-9"
        exit 0
else
        while [ $myvar -ne 10 ]
        do
                echo $myvar
                myvar=$(( $myvar + 1 ))
        done
fi

exit 0

<2>myfor1.sh
#秀出所給的所有參數
#!/bin/bash
for thing in "$@"                   #thing 表示變數
do
        echo you typed $thing
done
exit 0

執行方式:
1../myfor1.sh aaa bbb ccc  畫面依序秀出you typed aaa,you typed bbb,you typed ccc

進階練習:
a. 
#將後面給的參數IP,交由ping來執行,然後在螢幕上秀出有無成功
#!/bin/bash

for ip in "$@"
do
        ping -c 2  $ip &> /dev/null
        if [ "$?" != "0" ]; then
                echo ping $ip fail
        else
                echo ping $ip ok
        fi
done
exit 0

執行方式:
1.$./myfor1.sh 192.168.120.1 168.95.1.1 192.168.1.1
正確執行,依序秀出ping 192.168.120.1 ok,ping 168.95.1.1 ok,ping 192.168.1.1 fail
b.
#在畫面上秀出類似99乘法表
#!/bin/bash
#writer:Marcus Tsai 20100913
read -p "請輸入第一個數字:" fn
s=$(echo $fn |tr -d 0-9)
[ -n "$s" ] && echo "Error: $fn 不是數字" && exit 1
read -p "請輸入第二個數字:" sn
s=$(echo $sn |tr -d 0-9)
[ -n "$s" ] && echo "Error: $sn 不是數字" && exit 1

for (( i=1 ; i<=$fn ; i=i+1 ))
do
        for (( j=1 ; j<=$sn ; j=j+1 ))
        do
                total=$(($i*$j))
                echo "$i X $j = $total"
        done
done
exit 0
b修正版:
#在畫面上將結果分開秀出,而不是都在同一列,並增加錯誤判斷
#!/bin/bash
#Author : marcus Tsai 20100913
read -p "請輸入第一個數字1-9:" fn
s=$(echo $fn |tr -d 0-9)
[ -n "$s" ] && echo "Error: $fn 不是數字" && exit 1

[ $fn -gt "10" -o $fn -lt "1" ] && echo "Error: 請輸入1-9的數字" && exit 1
read -p "請輸入第二個數字1-9:" sn
s=$(echo $sn |tr -d 0-9)
[ -n "$s" ] && echo "Error: $sn 不是數字" && exit 1
[ $sn -gt "10" -o $sn -lt "1" ] && echo "Error: 請輸入1-9的數字" && exit 1
for (( i=1 ; i<=$sn ; i=i+1 ))
do
        for (( j=1 ; j<=$fn ; j=j+1 ))
        do
                total=$(($i*$j))
                printf "%s\t " "echo $j X $i = $total"
        done
    echo -e "\n"
done
exit 0

Shell script 練習(if then...else...)

<1>testroot.sh
#判斷執行程式的帳號是否為root
#!/bin/bash

w=$(whoami)
[ "$w" != "root" ] && echo "$w have no permission,need root " && exit 1
[ "$w" == "root" ] && echo "you are root"
exit 0


<2>param01.sh
#判斷option是否為"-a"
#!/bin/bash


if [ "$#" != "1" ] || [ "$1" != "-a" ]; then
        echo "error option"
        exit 1
fi

echo "-a ok"
exit 0

<3>mytar.sh
#判斷接續的檔案副檔名是否為.tar
#!/bin/bash

if [ "${1##*.}" == "tar" ]; then        
#${1}=$1,{}是為了執行##*.表示由左向右抓出最後一個.右邊的所有文字,單一個#表示由左向右第一個.右邊的所有文字
        echo "This appears to be a tarball."
else
        echo "At firs glance,this does not appear to be a tarball"
fi
exit 0

<4>touchsh
#判斷檔案是否附檔名為.sh如是給予執行權限
#!/bin/bash

[ ! -f "$1" ] && echo "file no exist!" && exit 1
if [ "$#" != "1" ] || [ "${1##*.}" != "sh" ]; then
        echo "error option"
        exit 1
fi
chmod +x $1
exit 0

2010年9月10日 星期五

bash下的字串處理

1. 向左刪除
字串處理方向是由
$ myvar=foodforthought.jpg
$ echo ${myvar##*fo}    #由左自右找到最後一個符合fo的將之後的顯示出來,*表示fo左邊可以有任意字元  
rthought.jpg

$ echo  ${myvar#*fo}    #由左自右找到第一個符合fo的將之後的顯示出來
odforthought.jpg

# 顯示 DNS IP 位址
$ mydns=$(cat /etc/resolv.conf | grep nameserver)
$ echo ${mydns##* }
192.168.56.2

顯示最後一個參數
$ nano lastargv.sh
myargv="$@"
echo ${myargv##* }

$ bash lastargv.sh a b c
c

2. 向右刪除
字串處理方向是由
$ myfoo=“chickensoup.tar.gz”
$ echo  ${myfoo%%.*}
chickensoup


$ echo  ${myfoo%.*}
chickensoup.tar

3. 截取字串
$ ex=cowabungaxyz
$ echo  ${ex:0:3}                # 0 代表起始位址, 3 代表抓三個字
Cow

$ echo  ${ex:3:7}
abungax

## 顯示 Dsfault Gateway
$ mygw=$(netstat -r | grep default)
$ echo $mygw
default              192.168.200.2 0.0.0.0 UG 0 0 0 eth0
$ echo ${mygw:16:15}
192.168.200.2

2010年9月6日 星期一

用VMware+Ubuntu-server做網路NAT與ROUTE lab



1>NAT
(1)$sudo vim /etc/network/interfaces ;加入
auto eth0
iface eth0 inet static
address 192.168.213.5
netmask 255.255.255.0
gateway 192.168.213.2

auto eth1
iface eth1 inet static
address 192.168.120.254
netmask 255.255.255.0

(2)$sudo vim /etc/sysctl.conf
#net.ipv4.ip_forward=1
將上面那行的註解拿掉變:
net.ipv4.ip_forward=1

(3)$sudo vim /etc/rc.local
route add -net 192.168.87.0 netmask 255.255.255.0 gw 192.168.120.5 dev eth1
route add -net 192.168.160.0 netmask 255.255.255.0 gw 192.168.120.7 dev eth1
iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE

(4)$sudo rm /etc/udev/rules.d/70-persistent-net.rules
避免網路設備有異常,可將上列檔案刪除

(5)$ sudo reboot #重開機,NAT設定完畢

(6)顯示文字如果有問題可以做下面修正:
$sudo locale-gen 'en_US.UTF-8'
$vim .bashrc ;在最後面加入底下幾行,表示用SSH方式還是使用zh_TW

2>Router
(1)sudo vim /etc/network/interfaces ;加入
auto eth0
iface eth0 inet static
address 192.168.120.5
netmask 255.255.255.0
gateway 192.168.120.254

auto eth1
iface eth1 inet static
address 192.168.87.254
netmask 255.255.255.0

(2))$sudo vim /etc/sysctl.conf
#net.ipv4.ip_forward=1
將上面那行的註解拿掉變:
net.ipv4.ip_forward=1

(3)$sudo rm /etc/udev/rules.d/70-persistent-net.rules
避免網路設備有異常,可將上列檔案刪除

(4)$ sudo reboot #重開機,Router設定完畢

R2同樣設定方式只是IP不同

3>NS
(1)sudo vim /etc/network/interfaces ;加入
auto eth0
iface eth0 inet static
address 192.168.87.10
netmask 255.255.255.0
gateway 192.168.87.254

(2)$sudo rm /etc/udev/rules.d/70-persistent-net.rules
避免網路設備有異常,可將上列檔案刪除

NS2與NS1一樣設定方式,修改ip即可

(3)$ sudo reboot #重開機,Router設定完畢

2010年9月4日 星期六

KVM install on Ubuntu 10.04

Max以前在Ubuntu上一直都是使用VirtualBox來建制虛擬環境。曾經也想試試Xen,
不過在Ubuntu上安裝一直失敗。前一陣子得知新的虛擬技術已經相當成熟且很多Linux發行版本
都有支援,更重要的是KVM是Kernel Based的虛擬化技術,當然值得來研究一下

參考資料:
1.KVM 官網
2.Ubuntu install 文件

安裝步驟:
1.檢查CPU是否支援硬體虛擬技術,沒有支援也可以安裝,但效能會相當低落,不如使用virtualbox
$egrep -c '(vmx|svm)' /proc/cpuinfo

If 0 it means that your CPU doesn't support hardware virtualization.

If 1 (or more) it does - but you still need to make sure that virtualization is enabled in the BIOS.

2.由於max使用的是64bit kernel,所以以下安裝操作以64bit為主,32bit請參考官方文件
<1>更新套件庫:
$sudo apt-get update
<2>安裝KVM相關套件:
$sudo apt-get install kvm libvirt-bin ubuntu-vm-builder bridge-utils virt-viewer
<3>將使用者帳號加入相關群組(kvm and libvirtd):
$groups
marcus adm kvm libvirtd
<4>在使用KVM前可能需要重開機或relogin一次
$sudo reboot or logout
<5>檢查是否有安裝成功
$virsh -c qemu:///system list
 Id Name                 State
----------------------------------

$
<6>安裝圖形管理工具
$
sudo apt-get install virt-manager
<7>安裝好後在系統-->偏好設定-->主選單 中,找到『虛擬機器管理員』,
選擇屬性,在指令前方加入gksudo以root權限啟動管理介面

2010年8月27日 星期五

Linux 動態路由設定方式

講師 : 陳祥輝
課程 : TCP/IP 網路通訊協定
2010/8/27 在文化上TCP/IP 陳祥輝老師教授如何設定動態路由,由於步驟繁複,特別記錄下來
以免忘記。
A – 192.168.100.0/27 – [B] – 192.168.100.32/27 – [C] – 192.168.100.64/27 – [D] – 192.168.100.96/27 -- E
A : eth0 100.1 / 27
B : eth0 100.30 / 27
eth1 100.33 / 27
C : eth0 100.34 / 27
eth1 100.65 / 27
D : eth0 100.66 / 27
eth1 100.126 / 27
E : eth0 100.97 / 27
B, C, D路由器安裝動態路由協定的套件
yum install quagga
為了避免網路出問題, 先將所有的網路卡IP清掉
ifconfig eth0 0.0.0.0
ifconfig eth1 0.0.0.0
將所有的設定檔ready
cd /etc/quagga
準備以下檔案
zebra.conf (已經存在)
vtysh.conf (已經存在)
ripd.conf ( cp ripd.conf.sample ripd.conf )
ospfd.conf ( cp ospfd.conf.sample ospfd.conf )
分別將[B], [C], [D]設定主機名稱 RB, RC, RD
vi vtysh.conf 編輯
hostname RB
或直接
echo “hostname RB” > vtysh.conf
啟動daemon
/etc/init.d/zebra start
/etc/init.d/ripd start
進入quagga的整合模式
vtysh
線上查詢使用 ?
進入模組模式
config terminal ( config t )
進入網路介面卡 eth0 設定
RB(config) interface eth0
RB(config-if) ip address 192.168.100.30/27 ( 設定IP位址 )
RB(config-if) no shutdown ( 啟動此介面卡 )
RB(config-if) exit ( 回上一層 )
進入網路介面卡 eth1 設定
RB(config) interface eth1
RB(config-if) ip address 192.168.100.33/27
RB(config-if) no shutdown
RB(config-if) exit
RB(config)
RB,RC,RD都要設好
exit : 回到上一層
Ctrl-Z : 回到第一層
請注意以下所在位置 RB#
查詢目前執行的設定
RB# show running-config
查詢啟動時的設定
RB# show startup-config
查詢目前的路由表
RB# show ip route
啟動 rip
RB# config t
RB(config)# router rip
RB(config-router)# version 2
RB(config- router)# network 192.168.100.0/27
RB(config- router)# network 192.168.100.32/27
若是輸入錯誤要刪除掉
RB(config- router)# no network 192.168.100.32/27
別忘了要啟動路由器的 ip forwarding 功能
RB(config)# ip forwarding
RB(config)# exit
RB# show ip route (看看你的路由表是否OK)
--- RIP 動態路由協定完成 ----
--- OSPF 動態路由協定開始 ---
重新啟動 daemon
/etc/init.d/zebra stop (zebra服務關閉)
/etc/init.d/ripd stop ( ripd服務關閉)
/etc/init.d/zebra start (啟動 zebra)
/etc/init.d/ospfd start (啟動 ospfd)
設定IP方式與前面完全相同

啟動 ospf
RB# config t
RB(config)# router ospf
RB(config-router)# network 192.168.100.0/27 area 0
RB(config-router)# network 192.168.100.32/27 area 1
注意你規劃的網路拓樸, 該網路是屬於哪一個 area, 並非全為 0 ^O^
別忘了要啟動路由器的 ip forwarding 功能
RB(config)# ip forwarding
RB(config)# exit
RB# show ip route (看看你的路由表是否OK)

2010年8月20日 星期五

修改VMware Guest OS MAC

本篇來自軟體工匠的文章
VMware Player 有一套演算法來自動決定 Guest OS 虛擬網路卡的 MAC address。這個機制運作得很好不會有問題,但有時候我們就是想自己指定這些 MAC address。要這樣作,只要修改 .vmx 檔就可以了。

首先,把以下這幾行刪除 (或以 # 「註」掉):
#ethernet0.addressType = "generated"
#ethernet0.generatedAddress = "00:0c:29:12:34:56"
#ethernet0.generatedAddressOffset = "0"
然後,增加以下兩行:
ethernet0.addressType = "static"
ethernet0.address = "00:50:56:03:06:14"
其中 ethernet0.address 一定要在這個範圍之內:00:50:56:00:00:00-00:50:56:3F:FF:FF。

當然,改完之後要重新啟動 GuestOS 才可以生效。

參考資料

2010年8月10日 星期二

ubuntu加入套件金鑰

在做套件庫(sudo apt-get update)更新時常會出現以下錯誤:
W: GPG error: http://ppa.launchpad.net hardy Release: 由於無法取得它們的公鑰,以下簽章無法進行驗證: NO_PUBKEY 6AF0E1940624A220

由於公鑰是用來驗證正確的套件來源,雖然還是可以使用,但是為了保護自己的電腦
最好還是使用官方驗證過的套件來源

方法:
sudo apt-key adv --recv-keys --keyserver keyserver.ubuntu.com [ID]
ID:即為錯誤碼中的 6AF0E1940624A220

2010年5月2日 星期日

Ubuntu 10.04 ubuntu one無法使用

話說MAX最近重灌電腦,把系統升級成ubuntu 10.04 64 bit
很棒的新版本~可是卻發現Ubuntu one不能用了
找了很久終於找到解決方法~特別紀錄一下以免忘記

1. $ps aux |grep ubuntuone #結果應該會出現:


marcus 1552 0.1 0.7 124084 30108 ? Sl 14:01 0:28 /usr/bin/python /usr/lib/ubuntuone-client/ubuntuone-syncdaemon
marcus 4027 0.1 0.7 251440 31064 ? S 15:09 0:22 /usr/bin/python /usr/lib/ubuntuone-client/ubuntuone-login

將這兩個行程都停掉後,再執行一次ubuntuone-preferences
登入ubtutu one應該就會出現add computer webpage嚕

2010年3月28日 星期日

LAMP安裝設定-UBUNTU

在ubuntu上安裝LAMP非常簡單
1.Synaptic套件管理裡面選擇LAMP sever安裝即可

2.apt-get

安裝後可在/var/www/建立test.php
內容打入:

查看php版本,並檢視有無正常支援
如果一直出現叫你下載
請到/etc/apache2/mods-enabled/ 檢查
php5.conf , php5.load 這兩個檔案有沒存在
若沒有,請下指令
$sudo a2enmod php5
加入
然後重起Apache在測試