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在測試

2010年2月9日 星期二

HDLM install on RHEL5.2

為了測試MultipathCluster,我準備了以下環境
主機1



Hostname
node1.tedpc.com

IP
10.101.24.182


192.168.0.1

Share Lun
10GB*2(Multipath)
主機2



Hostname
node2.tedpc.com

IP
10.101.24.183


192.168.0.2

Share Lun
10GB*2(Multipath)
Storage

USP100

HDLM6.0 install procedure
1.License Key 改名hdlm_license後放在/var/tmp
#mv license.plk /var/tmp/hdlm_license

2.產生安裝之Key
#mkdir /etc/opt/DynamicLinkManager
#echo "
license-key" >/etc/opt/DynamicLinkManager/dlm.lic_key

3.掛載光碟或ISO
#mount /dev/cdrom /media/cdrom
or
#mount -t iso9660 -t loop HDLM.iso /media/cdrom

4.確認HDLM版本
#/media/cdrom/installhdlm -v

5.安裝HDLM
#/media/cdrom/installhdlm
KAPL09210-I installhdlm will now start.
KAPL09093-I HDLM x.x.x-xx will be installed. Is this OK ? [y/n]: y
Preparing packages for installation...
KAPL09076-I The permanent license was installed.
HDLM-x.x.x.x.xxx-xx
KAPL09043-I The installation of HDLM-x.x.x.x.xxx-xx completed successfully.
KAPL09211-I installhdlm completed successfully.


6.安裝完成,檢查是否安裝成功
#rpm –qi HDLM
Name : HDLM Relocations: (not relocatable)
Version : 6.0.0.1.801 Vendor: Hitachi, Ltd.
Release : 1 Build Date: 西元20080509(週五) 231449
Install Date: 西元20100208(週一) 164636Build Host: inspire.hitachi.co.jp
Group : System Environment/Driver Source RPM: HDLM-6.0.0.1.801-1.src.rpm
Size : 13653222 License: All Rights Reserved. Copyright (C) 2003, 2008, Hitachi, Ltd.
Signature : (none)
Packager : Hitachi, Ltd.
Summary : I/O Path Management Software
Description :
HDLM manages paths between a host and storage subsystem.
HDLM evenly distributes the load across paths and switches to another path if there is a failure in a path being used, thus improving system reliability.

7.載入HDLM alert driver and the filter driver.
#/opt/DynamicLinkManager/bin/dlminsadrv
#modprobe sddlmfdrv

8.執行HDLM設定程式設定LDEV
#/sbin/dlmcfgmgr -r

9.啟動HDLM
#/etc/init.d/DLMManager start

10.增加HDLM環境變數
When the BourneAgain shell or Korn shell is used:
#PATH=$PATH:/opt/DynamicLinkManager/bin ; export PATH
When the C shell is used:
#set path= ( $path /opt/DynamicLinkManager/bin )

11.執行dlnkmgr確認HDLM詳細狀況與設定
# /opt/DynamicLinkManager/bin/dlnkmgr view -sys
HDLM Version : 6.0.0-01
Service Pack Version :
Load Balance : on(rr)
Support Cluster :
Elog Level : 3
Elog File Size (KB) : 9900
Number Of Elog Files : 2
Trace Level : 0
Trace File Size (KB) : 1000
Number Of Trace Files : 4
Path Health Checking : on(30)
Auto Failback : on(1)
Reservation Status :
Intermittent Error Monitor : off
HDLM Manager Ver WakeupTime
Alive 6.0.0-01 2010/02/08 17:10:28
HDLM Alert Driver Ver WakeupTime ElogMem Size
Alive 6.0.0-01 2010/02/08 17:09:17 1000
HDLM Driver Ver WakeupTime
Alive 6.0.0-01 2010/02/08 17:09:40
License Type Expiration
Permanent -
KAPL01001-I The HDLM command completed normally. Operation name = view, completion time = 2010/02/09 15:00:43

firefox調整與加入好用插件

最近在調整firefox與加入插件,作個紀錄以免忘記:

1.about:config
javascript.options.jit.content true (加速)
javascript.options.jit.chrome true (加速)

2.加入好用插件
新同文堂 (繁簡翻譯)
adblock (廣告阻擋)
Xmark (書籤同步)
FireFTP (整合FTP功能)
Download Statusbar (下載管理)
gTranslate (線上翻譯)