bonding各個模式的介紹
http://www.ithome.com.tw/itadm/article.php?c=58865
2012/03/22
多網卡bind同一個ip的方法
http://blog.secaserver.com/2011/11/centos-bonding-multiple-network-interface-card-nics/
http://www.how2centos.com/centos-6-channel-bonding/
proxmox 使用
http://pve.proxmox.com/wiki/Bond_configuration_(Video)
只有mode 6 guest os會通
http://yu-minspace.blogspot.com/2008/02/linux-bonding-lnux-kernel-2.html
所以只有mode 1及 mode 6可以使用 特別注意
否則guest os網路不會通
http://blog.secaserver.com/2011/11/centos-bonding-multiple-network-interface-card-nics/
http://www.how2centos.com/centos-6-channel-bonding/
proxmox 使用
http://pve.proxmox.com/wiki/Bond_configuration_(Video)
只有mode 6 guest os會通
http://yu-minspace.blogspot.com/2008/02/linux-bonding-lnux-kernel-2.html
所以只有mode 1及 mode 6可以使用 特別注意
否則guest os網路不會通
2012/03/21
今天在設定route-map
因為久沒下
所以有點忘了
記錄一下
1. 一定要先下
sdm prefer routing
這是隱藏指令 很重要
2. 再來設定 access-list
ip access-list extended 101
deny ip 192.168.0.0 0.0.255.255 host 10.0.0.3 (4786696 matches)
permit ip 192.168.0.0 0.0.255.255 any
3.設定route-map
route-map test permit 10
match ip address 101
set ip next-hop 10.0.0.1
4. 再把指令下到port 上或vlan上
ip policy route-map test
http://harlemhsu.pixnet.net/blog/post/20501380-%E8%A7%A3%E6%94%BE-cisco-3560-%E7%9A%84-ip-policy-route-map-(%E7%AD%96%E7%95%A5%E6%80%A7%E8%B7%AF%E7%94%B1)-
因為久沒下
所以有點忘了
記錄一下
1. 一定要先下
sdm prefer routing
這是隱藏指令 很重要
2. 再來設定 access-list
ip access-list extended 101
deny ip 192.168.0.0 0.0.255.255 host 10.0.0.3 (4786696 matches)
permit ip 192.168.0.0 0.0.255.255 any
3.設定route-map
route-map test permit 10
match ip address 101
set ip next-hop 10.0.0.1
4. 再把指令下到port 上或vlan上
ip policy route-map test
http://harlemhsu.pixnet.net/blog/post/20501380-%E8%A7%A3%E6%94%BE-cisco-3560-%E7%9A%84-ip-policy-route-map-(%E7%AD%96%E7%95%A5%E6%80%A7%E8%B7%AF%E7%94%B1)-
2012/03/18
2012/03/11
python 印出程式執行時間
import time
# 記錄開始時間 Record start time
tStart = time.time()
# 記錄結束時間 Record stop time
tStop = time.time()
print(tStop - tStart)
http://whhnote.blogspot.com/2010/10/python.html
2012/03/10
想研究一下python 畫圖
先安裝
http://my.opera.com/taiwanmonkey/blog/2012/02/19/python-2
範例
# -*- coding: cp950 -*-
import matplotlib.pyplot as plt
#以直線畫出一個三角形
plt.plot([1,20,20,1],[1,1,20,1],color="green",linewidth=5)
#畫點 r表示紅色 o表示圓形
#詳細在http://matplotlib.sourceforge.net/api/pyplot_api.html#matplotlib.pyplot.plot
for i in range(10):
plt.plot([2],[i*2],'ro')
#plt.plot([i],[i*2],'r1')
#plt.plot([i],[i*3],'r2')
#plt.plot([i],[i*4],'r3')
#plt.plot([i],[i*5],'r4')
#plt.plot([i])
plt.axis([-10,30,-10,30])
plt.xlabel('x numbers')
plt.ylabel('y numbers')
plt.show()
http://blog.rexzhao.com/category/matplotlib
先安裝
http://my.opera.com/taiwanmonkey/blog/2012/02/19/python-2
範例
# -*- coding: cp950 -*-
import matplotlib.pyplot as plt
#以直線畫出一個三角形
plt.plot([1,20,20,1],[1,1,20,1],color="green",linewidth=5)
#畫點 r表示紅色 o表示圓形
#詳細在http://matplotlib.sourceforge.net/api/pyplot_api.html#matplotlib.pyplot.plot
for i in range(10):
plt.plot([2],[i*2],'ro')
#plt.plot([i],[i*2],'r1')
#plt.plot([i],[i*3],'r2')
#plt.plot([i],[i*4],'r3')
#plt.plot([i],[i*5],'r4')
#plt.plot([i])
plt.axis([-10,30,-10,30])
plt.xlabel('x numbers')
plt.ylabel('y numbers')
plt.show()
http://blog.rexzhao.com/category/matplotlib
2012/03/08
2012/03/03
今天在找要如何用python登入apache的認証網頁
找到httplib2
http://code.google.com/p/httplib2/
範例如下
import base64
import httplib2
h = httplib2.Http()
auth = base64.encodestring( 'id' + ':' + 'password' )
resp, content = h.request(
'http://10.0.0.56/all/',
'GET',
headers = { 'Authorization' : 'Basic ' + auth }
)
print resp #傳回html的標頭
print content #傳回html的內容
2012/03/02
2012/03/01
今天一台vm出現disk空間不夠的問題
參考以下的資料
搞定
改完後記得再用gparted 調整分割大小
修改虛擬機鏡像大小(qcow2/raw resize)
不多說,直接看
創建一個鏡像文件,大小1G,格式是qcow2
muxueqz@muxueqz /tmp $ qemu-img create -f qcow2 t.qcow2 1G
Formatting 't.qcow2', fmt=qcow2 size=1073741824 encryption=off cluster_size=0
查看鏡像文件實際佔用空間
muxueqz@muxueqz /tmp $ ls -alh t.qcow2
-rw-r--r-- 1 muxueqz muxueqz 193K 8月31 13:18 t.qcow2
查看qcow2信息
muxueqz@muxueqz /tmp $ qemu-img info t.qcow2
image: t.qcow2
file format: qcow2
virtual size: 1.0G (1073741824 bytes)
disk size: 136K
cluster_size: 65536
加1G(resize +)
muxueqz@muxueqz /tmp $ qemu-img resize t.qcow2 +1G
Image resized.
再查看qcow2信息
muxueqz@muxueqz /tmp $ qemu-img info t.qcow2
image: t.qcow2
file format: qcow2
virtual size: 2.0G (2147483648 bytes)
disk size: 140K
cluster_size: 65536
減1G(resize -)
muxueqz@muxueqz /tmp $ qemu-img resize t.qcow2 -- 2G
qemu-img: This image format does not support resize
!!!qcow2只能加不能減!
再查看qcow2信息
muxueqz@muxueqz /tmp $ qemu-img info t.qcow2
image: t.qcow2
file format: qcow2
virtual size: 2.0G (2147483648 bytes)
disk size: 140K
cluster_size: 65536
果然沒變
再試試改變raw格式的大小(resize raw)
同樣先創建1G大小的raw
muxueqz@muxueqz /tmp $ qemu-img create -f raw t.raw 1G
Formatting 't.raw', fmt=raw size=1073741824
muxueqz@muxueqz /tmp $ qemu-img info t.raw
image: t.raw
file format : raw
virtual size: 1.0G (1073741824 bytes)
disk size: 1.0M
可以看出raw要比qcow2多佔一些空間。
加1G
muxueqz@muxueqz /tmp $ qemu-img resize t.raw -- +1G
Image resized.
muxueqz@muxueqz /tmp $ qemu-img info t.raw
image: t.raw
file format: raw
virtual size: 2.0G (2147483648 bytes )
disk size: 2.0M
減1G(resize -)
muxueqz@muxueqz /tmp $ qemu-img resize t.raw -- -1G
Image resized.
muxueqz@muxueqz /tmp $ qemu-img info t.raw
image: t.raw
file format: raw
virtual size: 1.0G (1073741824 bytes )
disk size: 1.0M
http://www.linuxzh.org/Linux/qemu_resize.html
參考以下的資料
搞定
改完後記得再用gparted 調整分割大小
修改虛擬機鏡像大小(qcow2/raw resize)
不多說,直接看
創建一個鏡像文件,大小1G,格式是qcow2
muxueqz@muxueqz /tmp $ qemu-img create -f qcow2 t.qcow2 1G
Formatting 't.qcow2', fmt=qcow2 size=1073741824 encryption=off cluster_size=0
查看鏡像文件實際佔用空間
muxueqz@muxueqz /tmp $ ls -alh t.qcow2
-rw-r--r-- 1 muxueqz muxueqz 193K 8月31 13:18 t.qcow2
查看qcow2信息
muxueqz@muxueqz /tmp $ qemu-img info t.qcow2
image: t.qcow2
file format: qcow2
virtual size: 1.0G (1073741824 bytes)
disk size: 136K
cluster_size: 65536
加1G(resize +)
muxueqz@muxueqz /tmp $ qemu-img resize t.qcow2 +1G
Image resized.
再查看qcow2信息
muxueqz@muxueqz /tmp $ qemu-img info t.qcow2
image: t.qcow2
file format: qcow2
virtual size: 2.0G (2147483648 bytes)
disk size: 140K
cluster_size: 65536
減1G(resize -)
muxueqz@muxueqz /tmp $ qemu-img resize t.qcow2 -- 2G
qemu-img: This image format does not support resize
!!!qcow2只能加不能減!
再查看qcow2信息
muxueqz@muxueqz /tmp $ qemu-img info t.qcow2
image: t.qcow2
file format: qcow2
virtual size: 2.0G (2147483648 bytes)
disk size: 140K
cluster_size: 65536
果然沒變
再試試改變raw格式的大小(resize raw)
同樣先創建1G大小的raw
muxueqz@muxueqz /tmp $ qemu-img create -f raw t.raw 1G
Formatting 't.raw', fmt=raw size=1073741824
muxueqz@muxueqz /tmp $ qemu-img info t.raw
image: t.raw
file format : raw
virtual size: 1.0G (1073741824 bytes)
disk size: 1.0M
可以看出raw要比qcow2多佔一些空間。
加1G
muxueqz@muxueqz /tmp $ qemu-img resize t.raw -- +1G
Image resized.
muxueqz@muxueqz /tmp $ qemu-img info t.raw
image: t.raw
file format: raw
virtual size: 2.0G (2147483648 bytes )
disk size: 2.0M
減1G(resize -)
muxueqz@muxueqz /tmp $ qemu-img resize t.raw -- -1G
Image resized.
muxueqz@muxueqz /tmp $ qemu-img info t.raw
image: t.raw
file format: raw
virtual size: 1.0G (1073741824 bytes )
disk size: 1.0M
http://www.linuxzh.org/Linux/qemu_resize.html
2012/02/26
今天在用mail.py使用hinet發信時
一直出現以下訊息
Traceback (most recent call last):
File "send_chk_mail.py", line 5, in ?
smtp = smtplib.SMTP("168.95.4.10")
File "/usr/lib/python2.4/smtplib.py", line 258, in __init__
addr = socket.gethostbyname(socket.gethostname())
socket.gaierror: (-2, 'Name or service not known')
查了半天
最後的解決方法是
把 hostname 加到 /etc/hosts
例
127.0.1.1 abc.com abc
2012/02/10
2012/02/08
2012/02/07
2012/02/06
為了要測試机櫃內加排風扇有沒有作用
有一個新的需求 要知道cpu的溫度
查了一下資料
可以使用lm-sensors來得到資料
步驟如下
apt-get install lm-sensors
(yum install lm-sensors)
#sensors-detect
一直按Enter直到看到
#----cut here----
#...................................
#....................
i2c-i801
# Chip drivers
# no driver for Winbond W83L785R/G yet
lm85
#----cut here----
在二個cut here 中間找到的就是必需insert的module
以上為例
# modprobe i2c-i801
# modprobe lm85
接下來輸入
# sensors
就會出現cpu溫度之類的監控訊息
adm1027-i2c-3-2e
Adapter: SMBus I801 adapter at e000
V1.5: +1.31 V (min = +0.00 V, max = +3.32 V)
VCore: +1.49 V (min = +0.00 V, max = +2.99 V)
V3.3: +3.30 V (min = +0.00 V, max = +4.38 V)
V5: +5.08 V (min = +0.00 V, max = +6.64 V)
V12: +11.97 V (min = +0.00 V, max = +15.94 V) ALARM
CPU_Fan: 3941 RPM (min = 0 RPM)
fan2: 0 RPM (min = 0 RPM)
fan3: 0 RPM (min = 0 RPM)
fan4: 1882 RPM (min = 0 RPM)
CPU Temp: +48.8°C (low = -127.0°C, high = +127.0°C)
Board Temp: +46.0°C (low = -127.0°C, high = +127.0°C)
Remote Temp: +45.5°C (low = -127.0°C, high = +127.0°C)
cpu0_vid: +1.525 V
但在hp的机器中出現的是
#----cut here----
# You must also install and load the IPMI modules
ipmi-si
# Chip drivers
# Warning: the required module ipmisensors is not currently installed
# on your system. For status of 2.6 kernel ports check
# http://www.lm-sensors.org/wiki/Devices. If driver is built
# into the kernel, or unavailable, comment out the following line.
ipmisensors
#----cut here----
有一個新的需求 要知道cpu的溫度
查了一下資料
可以使用lm-sensors來得到資料
步驟如下
apt-get install lm-sensors
(yum install lm-sensors)
#sensors-detect
一直按Enter直到看到
#----cut here----
#...................................
#....................
i2c-i801
# Chip drivers
# no driver for Winbond W83L785R/G yet
lm85
#----cut here----
在二個cut here 中間找到的就是必需insert的module
以上為例
# modprobe i2c-i801
# modprobe lm85
接下來輸入
# sensors
就會出現cpu溫度之類的監控訊息
adm1027-i2c-3-2e
Adapter: SMBus I801 adapter at e000
V1.5: +1.31 V (min = +0.00 V, max = +3.32 V)
VCore: +1.49 V (min = +0.00 V, max = +2.99 V)
V3.3: +3.30 V (min = +0.00 V, max = +4.38 V)
V5: +5.08 V (min = +0.00 V, max = +6.64 V)
V12: +11.97 V (min = +0.00 V, max = +15.94 V) ALARM
CPU_Fan: 3941 RPM (min = 0 RPM)
fan2: 0 RPM (min = 0 RPM)
fan3: 0 RPM (min = 0 RPM)
fan4: 1882 RPM (min = 0 RPM)
CPU Temp: +48.8°C (low = -127.0°C, high = +127.0°C)
Board Temp: +46.0°C (low = -127.0°C, high = +127.0°C)
Remote Temp: +45.5°C (low = -127.0°C, high = +127.0°C)
cpu0_vid: +1.525 V
但在hp的机器中出現的是
#----cut here----
# You must also install and load the IPMI modules
ipmi-si
# Chip drivers
# Warning: the required module ipmisensors is not currently installed
# on your system. For status of 2.6 kernel ports check
# http://www.lm-sensors.org/wiki/Devices. If driver is built
# into the kernel, or unavailable, comment out the following line.
ipmisensors
#----cut here----
但無法使用
modprobe ipmisensors
會有error
但使用
modprobe coretemp
接下來輸入
#sensor
出現
coretemp-isa-0000
Adapter: ISA adapter
Core 0: +30.0°C (high = +85.0°C, crit = +95.0°C)
coretemp-isa-0001
Adapter: ISA adapter
Core 1: +37.0°C (high = +85.0°C, crit = +95.0°C)
coretemp-isa-0002
Adapter: ISA adapter
Core 2: +33.0°C (high = +85.0°C, crit = +95.0°C)
coretemp-isa-0003
Adapter: ISA adapter
Core 3: +38.0°C (high = +85.0°C, crit = +95.0°C)
就可以看到了
2012/02/04
http://lccnetvip.pixnet.net/blog/post/33921210-%E8%81%AF%E6%88%90%E9%9B%BB%E8%85%A6%E8%AC%9B%E5%B8%AB%E5%B0%88%E6%AC%84%EF%BC%9A%E8%AB%87-linux-%E7%B3%BB%E7%B5%B1%E7%92%B0%E5%A2%83%E5%BF%AB%E9%80%9F%E5%BB%BA
談 Linux 系統環境快速建立大量的使用者帳號
分享:
文、小州老師
§章節:
前言
使用者帳號清單的格式
設定使用者帳號本身的密碼
開始寫 shell script
其他使用技巧作法
總結
§前言:
對於有用過 linux 的人來說,使用者帳號的建立應該是很常見的操作,比方如下的操作方式:
# adduser jonhy
# passwd jonhy
當要建立的使用者帳號很少時候可以簡單用命令列快速敲入幾個指令即可。不過若是要建立的帳號數量很多時候,手動命令輸入的方式會顯得很不方便也容易出錯。所以下面來簡單介紹一下如何寫個 shell script 達成該需求。
§使用者帳號清單的格式:
要大量建立使用者帳號,前提當然是需要有一份帳號清單資訊,所以暫定格式規劃如下:
username:password
其中 username 表示帳號名稱,password 表示該帳號應對的密碼。該兩個欄位使用 : 為分隔即可。比方一份清單內容如下:
peter:hello123
allen:cvs1211oz
實際上可能會有人在 windows 上的 excel 建立帳號與密碼資訊,那可以匯出成為 csv 等格式。不過 csv 格式是以逗點為分隔字元,所以下面要使用的話只要適當改一下程式碼就可以直接使用。
§設定使用者帳號本身的密碼:
傳統使用者新增用 adduser 命令後,還需要用 passwd 設定密碼。不過使用 passwd 設定密碼通常需要輸入兩次確認,若是要用於 shell script 非交談的環境,可能不是很方便,所以得找個方式應對。
方式1:
passwd 命令在大多的 linux 發行版本內,有支援 --stdin 的參數可以使用。
[root@linux ~]# passwd --help
Usage: passwd [OPTION...] <accountName>
-k, --keep-tokens keep non-expired authentication tokens
-d, --delete delete the password for the named account (root only)
-l, --lock lock the named account (root only)
-u, --unlock unlock the named account (root only)
-f, --force force operation
-x, --maximum=DAYS maximum password lifetime (root only)
-n, --minimum=DAYS minimum password lifetime (root only)
-w, --warning=DAYS number of days warning users receives before
password expiration (root only)
-i, --inactive=DAYS number of days after password expiration when an
account becomes disabled (root only)
-S, --status report password status on the named account (root only)
--stdin read new tokens from stdin (root only)
實際使用方式如下:
# passwd --stdin peter
這時候只要輸入密碼按下 enter 後即可完成密碼的異動。不過考慮於 shell script 非交談環境使用,轉向用管線方式導入:
# echo "hello123" | passwd --stdin peter
方式2:
linux 發行版本內可以找到 chpasswd 命令,該命令可以透過 stdin 讀取一對使用者帳號與密碼,最後完成密碼的異動。使用方式如下:
# echo "peter:hello123" | chpasswd
要注意的是,chpasswd 預設使用 des 的密碼加密格式,所以若要使用 md5 或者是其他密碼加密格式,要看一下自己系統該命令的參數支援。
[root@linux ~]# chpasswd --help
Usage: chpasswd [options]
Options:
-c, --crypt-method the crypt method (one of DES MD5 SHA256 SHA512)
-e, --encrypted supplied passwords are encrypted
-h, --help display this help message and exit
-m, --md5 use MD5 encryption instead DES when the supplied passwords are not encrypted
§開始寫 shell script:
這邊開始動工寫 shell script,程式碼很單純沒有考慮太多很細部問題,先要求可以工作為主:
#!/bin/bash
DATA_FILE="account.txt"
while read line
do
username=$(echo "$line" | cut -d : -f 1)
passwd=$(echo "$line" | cut -d : -f 2)
adduser $username
echo "$passwd" | passwd --stdin $username
done < $DATA_FILE
程式碼透過迴圈來完成讀入檔案每一行內容,然後透過 cut 命令以逗點為分隔字元取出第 1,2 個使用者帳號與密碼欄位,最後呼叫 adduser 與 passwd 命令完成使用者新增的動作。若是要改成呼叫 chpasswd 命令設定密碼,程式碼如下一份:
#!/bin/bash
DATA_FILE="account.txt"
while read line
do
username=$(echo "$line" | cut -d : -f 1)
passwd=$(echo "$line" | cut -d : -f 2)
adduser $username
echo "$username:$passwd" | chpasswd # 這邊同 echo "$line" | chpasswd
done < $DATA_FILE
§其他使用技巧作法:
除了剛剛方式之外,這邊順便補充幾個額外的東西可以看情況使用。大多發行版本內使用者新增都有 adduser 與 useradd,adduser 命令各家使用方式會有點差別,不過 useradd 命令應該都差不多使用方式。在 useradd 命令有個 -p 參數可用:
[root@linux ~]# useradd
Usage: useradd [options] LOGIN
Options:
-b, --base-dir BASE_DIR base directory for the new user account home directory
......
-p, --password PASSWORD use encrypted password for the new user account
依據說明來看可以在新增時候直接把密碼套用進去,不過密碼需要是 DES/MD5 等等這類已經加密過可用的格式,那剛好可以透過 openssl 套件包提供的 openssl 命令達成該需求。
[root@linux ~]# openssl passwd --help
Usage: passwd [options] [passwords]
where options are
-crypt standard Unix password algorithm (default)
-1 MD5-based password algorithm
-apr1 MD5-based password algorithm, Apache variant
-salt string use provided salt
-in file read passwords from file
-stdin read passwords from stdin
-noverify never verify when reading password from terminal
-quiet no warnings
-table format output as table
-reverse switch table columns為 hello123 的話,可以這樣使用:
# openssl passwd -crypt "hello123" (這是 DES 加密演算法格式)
# openssl passwd -1 "hello123" (這是 MD5 加密演算法格式)
# echo "hello123" | openssl passwd -1 -stdin (這是 MD5 加密演算法格式,密碼由標準輸入讀入)
不過很可惜這個版本的 openssl 命令還找不到如何產生 SHA256 等更高級的演算法加密格式.... 最終程式碼改改如下:
#!/bin/bash
DATA_FILE="account.txt"
while read line
do
username=$(echo "$line" | cut -d : -f 1)
passwd=$(echo "$line" | cut -d : -f 2)
passwd2=$(openssl passwd -1 "$passwd")
useradd -p "$passwd2" $username
done < $DATA_FILE
§總結:
這篇簡單談到 linux 上面如何大量新增使用者帳號的方式,過程中談到幾個小指令的應用,也可以簡單了解到熟悉 shell script 對於系統管理的優點,提供給有需要的朋友們參考。
2012/02/02
訂閱:
文章 (Atom)