顯示具有 linux 標籤的文章。 顯示所有文章
顯示具有 linux 標籤的文章。 顯示所有文章

2100年12月31日 星期五

vim 小技巧篇

vim 小技巧篇

*: VIM 多行註解

1. vim加上多行註解的步驟:

(1) 將游標移到要開始註解的那一列。
(2) 按下ESC、然後按下Ctrl + V ,切換成區塊選取的模式(就是反白啦)。
(3) 將游標向下移到要加上註解的最後一列,此時你可以發現這幾列都被反白了。
(4) 按下大寫I,游標跑回步驟(2)的那一列了,這時就可以直接輸入要用的註解了,假設輸入'//'。
(5) 按下ESC,大功告成。
(6) 千萬不要忘記步驟(5)。

2. vim 取消多行註解的步驟:

(1) 方法和加上多行註解一樣,先將游標移到開始註解的那一列。
(2) 按下ESC、然後按下Ctrl + V ,切換成區塊選取的模式。
(3) 將游標向下移到要加上註解的最後一列,再按左、右的方向鍵,將要delete掉的註解都反白。
(4) 按下delete,大功告成。



vim study 指令基本篇



========== command mode ==========
insert mode
由 command mode 進入 insert mode 有六個常用的按鍵: i I a A o O
i     Insert text before the cursor 
I     Insert text Before the first non-blank in the line 
a     Append text after the cursor
A     Append text at the end of the line
o     Begin a new line below the cursor and insert text 

O     Begin a new line above the cursor and insert text (並把文字往下推一行) 



2020年12月1日 星期二

2020年1月8日 星期三

ebtable staudy


ebtables

默認有三個Table (filter,nat,broute)

filter 內建三個chain:INPUT/OUTPUT/FORWARD

nat 內建三個chain:PREROUTING/OUTPUT/POSTROUTING

broute內建一個chain : BROUTING



*顯示table

ebtables [-t filter] -L 顯示filter table的內容,默認也是顯示該table

ebtables -t broute -L 顯示broute table的內容

ebtables -t nat -L 顯示nat table的內容



增加一個chain

ebtables -t filter -N newChain -P ACCEPT



但此時不會有封包走到此chain,須將 chain 加到 linux 內建的 hook chain上

ebtables -t filter -A INPUT -j newChain 


ebtables -t filter -L

Bridge chain: INPUT, entries: 1, policy: ACCEPT

-j newChain 



ebtables [-t table ] -[ACDI] chain rule specification [match extensions] [watcher extensions] target    // append, delete, change-counters, insert

ebtables [-t table ] -P chain ACCEPT | DROP | RETURN                                                                      // policy, Set the policy for the chain to the given target

ebtables [-t table ] -F [chain]                                                                                                           // flush, Flush the selected chain

ebtables [-t table ] -Z [chain]                                                                                                           // zero, Set the counters of the selected chain to zero

ebtables [-t table ] -L [-Z] [chain] [ [--Ln] | [--Lx] ] [--Lc] [--Lmac2]                                                  // list, List all rules in the selected chain. If no chain is selected, all chains are listed.

                              // -L -Z , When both the -Z and -L commands are used together in this way, the rule counters are printed on the screen before they are set to zero.

                              // -L --Ln, Shows the counters at the end of each rule displayed by the -L command.

ebtables [-t table ] -N chain [-P ACCEPT | DROP | RETURN]                                                               // new-chain

ebtables [-t table ] -X [chain]                                                                                                           // delete-chain

ebtables [-t table ] -E old-chain-name new-chain-name                                                                    // rename-chain

ebtables [-t table ] --init-table                                                                                                         // Replace the current table data by the initial table data

ebtables [-t table ] [--atomic-file file] --atomic-commit

ebtables [-t table ] [--atomic-file file] --atomic-init

ebtables [-t table ] [--atomic-file file] --atomic-save





ebtables [-t table ] -[ACDI] chain rule specification [match extensions] [watcher extensions] target

ebtables [-t table ] -P chain ACCEPT | DROP | RETURN

ebtables [-t table ] -F [chain]

ebtables [-t table ] -Z [chain]

ebtables [-t table ] -L [-Z] [chain] [ [--Ln] | [--Lx] ] [--Lc] [--Lmac2]

ebtables [-t table ] -N chain [-P ACCEPT | DROP | RETURN]

ebtables [-t table ] -X [chain]

ebtables [-t table ] -E old-chain-name new-chain-name

ebtables [-t table ] --init-table

ebtables [-t table ] [--atomic-file file] --atomic-commit

ebtables [-t table ] [--atomic-file file] --atomic-init

ebtables [-t table ] [--atomic-file file] --atomic-save




2018年8月28日 星期二

安裝 shadowsock server

Synology DSM6.21

#介紹
https://zh.wikipedia.org/wiki/Shadowsocks

#git位置 shadowsock python
https://github.com/dgkang/shadowsocks-python


1.先安裝 python2 or python3皆可

2.安裝 python套件包 pip  (ubuntu:apt-get install python-pip)
wget https://bootstrap.pypa.io/get-pip.py
python get-pip.py
https://bootstrap.pypa.io/

3.pip 安裝 shadowsocks
pip install shadowsocks

4.
vi /etc/shadowsocks.json

{

    "local_address": "127.0.0.1",
    "local_port":1080,
    "port_password":{
    "6655":"nguguest",
    "8080":"nguguest"
    },
    "timeout":600,
    "method":"aes-256-cfb",
    "fast_open":false
}

其中
    "port_password":{
    "6655":"nguguest",
    "8080":"nguguest"
    },
是設定server port 對應的 password
這邊沒有設定serverIP 因為不需要

5.

*ssserver -c /etc/shadowsocks.json遇到error
RuntimeError: can not find library crypto 
自行修改/usr/lib/python2.7/ctypes/util.py
拿個發行版的來取代

*遇到Exception: libcrypto(OpenSSL) not found
參考https://ningselect.com/2018/05/01/synologyshadowsocks
修改 註解掉以下紅色四行
sudo vi /usr/lib/python2.7/site-packages/shadowsocks/crypto/util.py
 56     for name in lib_names:
 57         if os.name == "nt":
 58             paths.extend(find_library_nt(name))
 59 #        else:
 60 #            path = ctypes.util.find_library(name)
 61 #            if path:
 62 #                paths.append(path)

*
2020 update
最近重裝遇到另一個新error
TypeError: find_library() takes exactly 1 argument (3 given)
  File "/usr/lib/python2.7/site-packages/shadowsocks/crypto/openssl.py", line 41, in load_openssl
    raise Exception('libcrypto(OpenSSL) not found')
Exception: libcrypto(OpenSSL) not found

sudo -i 後進入root即可
ssserver -c /etc/shadowsocks.json -d start
(我直接sudo 不能,應該是lib path 的問題,沒有繼續trace)

6.連線試試 後臺啟動
ssserver -c /etc/shadowsocks.json -d start
ssserver -c /etc/shadowsocks.json -d stop
ssserver -c /etc/shadowsocks.json -d restart


2018年2月13日 星期二

ftrace

https://www.ibm.com/developerworks/cn/linux/l-cn-ftrace1/
https://www.ibm.com/developerworks/cn/linux/l-cn-ftrace2/
https://www.ibm.com/developerworks/cn/linux/l-cn-ftrace3/index.html


http://blog.csdn.net/scarecrow_byr/article/details/17741809

https://zhuanlan.zhihu.com/p/22130013


#先關閉tracing
echo 0 > tracing_on
#清除trace
echo  1 > trace
#啟用function_graph
echo function_graph  > current_tracer
echo function1 funtion2 > set_graph_function
#開啟tracing
echo 1 > tracing_on
#不確定要不要開
echo 1 > /proc/sys/kernel/stack_tracer_enabled
echo 1 > /proc/sys/kernel/ftrace_enabled

執行function

#查看執行結果和所花費時間
cat trace

#關閉tracing
echo 0 > tracing_on

2018年1月10日 星期三

linux kernel printk

https://www.kernel.org/doc/Documentation/printk-formats.txt

16進位e自動顯示0x
pintk( " %#x " ,hex);

常用
int %d or %x
unsigned int %u or %x
long %ld or %lx
unsigned long %lu or %lx
long long %lld or %llx
unsigned long long %llu or %llx
size_t %zu or %zx
ssize_t %zd or %zx
s32 %d or %x
u32 %u or %x
s64 %lld or %llx
u64 %llu or %llx


MAC
%pM 00:01:02:03:04:05
%pMF 00-01-02-03-04-05

IPv4
%pI4 1.2.3.4
%pi4 001.002.003.004
%p[Ii]4[hnbl]
The additional ``h``, ``n``, ``b``, and ``l`` specifiers are used to specify
host, network, big or little endian order addresses respectively. Where
no specifier is provided the default network/big endian order is used.

IPv6
%pI6 0001:0002:0003:0004:0005:0006:0007:0008
%pi6 00010002000300040005000600070008
%pI6c 1:2:3:4:5:6:7:8



2016年5月29日 星期日

linux 加入PATH 環境變數

http://linux.vbird.org/linux_basic/0320bash.php#settings

http://www.linuxfromscratch.org/blfs/view/svn/postlfs/profile.html


fast.

修改 ~/.bashrc 或 ~/.profile
  1. 若該變數為擴增變數內容時,則可用 "$變數名稱" 或 ${變數} 累加內容,如下所示:
    『PATH="$PATH":/home/bin』或『PATH=${PATH}:/home/bin』
  2. 若該變數需要在其他子程序執行,則需要以 export 來使變數變成環境變數
    『export PATH』

ex.

vim ~/.bashrc
or
vim ~/.profile

加入以下

# User specific environment and startup programs
PATH=${PATH}:/opt/bin
export PATH


2016年5月26日 星期四

NAS learn



開啟ssh PubkeyAuthentication 服務
ref.https://www.chainsawonatireswing.com/2012/01/15/ssh-into-your-synology-diskstation-with-ssh-keys//?from=@


To start the process, you need to edit the SSH daemon’s config file to allow access via keys.

Edit/etc/ssh/sshd_config using vim & change these lines:

#RSAAuthentication yes 
#PubkeyAuthentication yes 
#AuthorizedKeysFile .ssh/authorized_keys

To this:

RSAAuthentication yes 
PubkeyAuthentication yes 
AuthorizedKeysFile .ssh/authorized_keys


Now get your permissions set correctly on that directory & file:> chmod 700 .ssh

> chmod 600 .ssh/authorized_keys

> chmod 700 .ssh/

再來是client 端 ,基本的概念就是使用private key 去開在遠端server public key

生成privatekey 和publickey
#ssh-keygen 

copy 到遠端的~/.ssh下
#scp id_rsa.pub username@serverip:~/.ssh/
登入遠端server
ssh username@serverip

將public key 加入.ssh/authorized_keys  (我的cat 曾經動過手腳 alias 成 cat -n 害我卡了一整個晚上  哭哭)
cat .ssh/id_rsa.pub >> .ssh/authorized_keys 

這樣子就可以 key 認證登入, 不需輸入密碼.


重開遠端server ssh
Restarted sshd via synoservicectl --restart sshd and by restarting whole NAS.
or
rebooot

2016年5月24日 星期二

linux server 記事 ubuntu 16.04LTS

家裡退休的筆電閒了下了,因此打算架個 linux server 來玩玩
在這邊紀錄一下架設日誌,過程,供日後參考。

安裝版本為 ubuntu 16.04 LTS desktop x64 english



*更新軟體
apt-get update
apt-get upgrade

*備份
rsync + ssh + crontab

1.先處理免密碼登入的 RSA key

2.編輯vim backup_script.ssh 內容只有一行
rsync -av /* username@serverip::NetBackup/ngulinux/ --rsh='ssh -i userPrivateKey' --exclude={/dev/*,/home/username/.cache/*,/proc/*,/sys/*,/tmp/*,/run/*,/mnt/*,/media/*,/lost+found}

不知道為何我上面的指令可以直接 單run script 執行沒問題,但是加到crontab時 ㄍ--exclude 指令沒起到作用,因此只好把此指令拔掉將除外的檔案塞到額外的檔案,作法大概是
rsync -av /* username@serverip::NetBackup/ngulinux/ --exclude-from='/root/rsync_bk/excludeList'  --rsh='ssh -i userPrivateKey' -

然後建立一個檔案叫 excludeList 在路徑上
檔案的寫法大概是
/dev/
/proc/
/sys/
/tmp/
/run/
/mnt/
/media/
/lost+foun/
.cache/
google-chrome/
/var/tmp/
/var/log/

大概說明一下
/dev/    代表略過此目錄(連目錄都不產生)
/dev/*  複製此目錄但是略過此目錄下所有檔案
dev/     所有檔案或目錄的名稱的任意位置包含 dev 的都略過

3.將backup_script.ssh 加進crontab
vim /etc/crontab

* * * * * root /bin/sh /cronfiles/rsync.sh
(分 時 日月 周) (執行者) (執行shell) (script)

10 5    * * *   root    /root/rsync_bk/rsync.sh >> /var/log/rsync.log 2>&1

收割
#/etc/init.d/cron restart


*遠端桌面
1.anydesk  http://anydesk.com/remote-desktop
2.teamviewer  https://www.teamviewer.com/
3.VNC xrdp
ref.http://programingman.blogspot.tw/search/label/raspberry

*文字介面開機

vim /etc/default/grub

修改成以下參數

#GRUB_HIDDEN_TIMEOUT=0
#GRUB_HIDDEN_TIMEOUT_QUIET=true
GRUB_TIMEOUT=3
GRUB_CMDLINE_LINUX_DEFAULT="text"
GRUB_CMDLINE_LINUX="text"
GRUB_TERMINAL=console

開啟多人模式
sudo systemctl enable multi-user.target --force 
sudo systemctl set-default multi-user.target
更新
update-grub

若要開啟x-window 不知道為何我的 startx 無效
需要
service lightdm restart
or 
 /etc/init.d/lightdm restart
才可以再開啟x-window

到此告一段落,我做出的效果差不多就是開機為tty1 文字模式
遠端可以隨時xrdp 回來x-window (使用Windows),當server 有問題時也可以使用x-window 來控制

在這邊不得不推崇一下 Teamviewer,發現使用teamviewer 設定好後不用登入到 x-window 畫面
在TTY1 純文字下就可以連進來下 command 不知道怎麼做的,有點厲害
若在TTY1 下使用 service lightdm restart ,畫面也會自動轉為x-window , 是個蠻棒的環境

所以在我的環境下
1.可以使用xrdp 直接遠端進我的 Sever 桌面 (不需要 service lightdm restart)
2.可以使用teamviewer 進來下 service lightdm restart 後直接轉成我的桌面
3.SSH 近來我的server

完成,媽呀 因為我的是筆電,每次重開機時只要系統偵測到是蓋著的就自動休眠

# 按下電源 預設行為為關機
HandlePowerKey=poweroff
 # 按下暫停
 HandleSuspendKey=suspend 
# 按下休眠 
HandleHibernateKey=hibernate
 # 闔上螢幕
# HandleLidSwitch=suspend
HandleLidSwitch=ignore

vim /etc/systemd/logind.conf

*必備軟體
sudo apt-get install vim
sudo apt-get install tmux

chewing
upnpc
openssh-server

*wake-on-Lan 網路喚醒

#ethtool interfaceName
查看 這兩個參數
Supports Wake-on: pg
Wake-on: d

d為disable 的意思  g為magic packet 可以喚醒的意思

使用指令將 Wake-on 改為 g

#ethtool -s eth0 wol g

之後就可以關機試試看效果了
另一台電腦使用
wakelan or wakeonlan 之類的指令試試看可不可以喚醒

wakeonlan YOUR_TARGET_MAC -b 192.168.0.255

-b 為指定brocast packet  因為 中間的router or switch 若因為arp timeout 有可能default 的行為不是flooding 而是drop 使用brocast packet 是比較保險的做法

其中ethtool -s eth0 wol g 只有一次性,因此需要用你所想的道的方式設定他always on
ex.加入排程 crontab

way2. 使用nmcli 指令設定
# nmcli con show
NAME    UUID                                  TYPE            DEVICE
enp9s0  e33fd8e4-bf42-4652-8a85-55e4ebc49d24  802-3-ethernet  enp9s0

查看wol 是否有開啟
# nmcli c show "enp9s0" | grep 802-3-ethernet.wake-on-lan
802-3-ethernet.wake-on-lan:             80 (broadcast, magic)
802-3-ethernet.wake-on-lan-password:    --

修改成接收broadcast and magic paacket 
nmcli c modify "enp9s0" 802-3-ethernet.wake-on-lan magic,broadcast

(後記:在我的環境兩種方式都設定過了還是不work ,發包的magic packet 沒有錯誤,另一台電腦可以順立wol,因此猜測應該是硬體支援的問題,
先暫時放棄此功能,原本想法是有要連進來再利用gateway 將server 喚醒避免不必要的耗電,畢竟當server 是不太會關機的)


*sudo 免密碼

因為常常在 sudo 我的密碼又很大一串,所以索性用個免密碼sudo 這樣就不用先轉成 root 在做事

修改方式大概為

(舊版方式)
在username最後的一個ALL 前加入 NOPASSWD
username ALL=(ALL) NOPASSWD: ALL
username ALL=(ALL:ALL) NOPASSWD: ALL

上面兩種用法就看你的系統是用哪種語法跟著原本的方式寫就是了

注意若此檔修改錯誤會造成無法使用 sudo 指令,因此語法要注意一下
最好的方式是使用visudo 指令修改,會幫你檢查與法有無錯誤

更新 新版方式更安全些寫在/etc/sudoers.d

vi /etc/sudoers.d/nopasswd4sudo
输入 
yourusername ALL=(ALL) NOPASSWD : ALL
ESC :wq!




*SAMBA 參考 (點我)

但是瑞凡  我失敗了,不知道為何 在我的Raspberry mate 15.04 是成功的 在16.04失敗,也不知從何Debug 起 ,先擱著好了。

# Un-comment the following (and tweak the other settings below to suit)
# to enable the default home directory shares. This will share each
# user's home directory as \\server\username
[homes]
comment = Home Directories
browseable = yes

結果是這段的 [homes] 沒有打開,ㄍㄋㄋ  搞了我一個晚上















2016年1月28日 星期四

ubuntu samba user home directors

Ref.http://linux.vbird.org/linux_server/0370samba.php

sudo apt-get install samba
sudo apt-get install python-glade2 system-config-samba

sudo cp /etc/samba/smb.conf /etc/samba/smb.conf.bak
vim /etc/samba/smb.conf
 

更改以下項目
#======================= Share Definitions =======================

# Un-comment the following (and tweak the other settings below to suit)
# to enable the default home directory shares. This will share each
# user's home directory as \\server\username
[homes]
   comment = Home Directories
   browseable = yes

# By default, the home directories are exported read-only. Change the
# next parameter to 'no' if you want to be able to write to them.
   read only = no

# File creation mask is set to 0700 for security reasons. If you want to
# create files with group=rw permissions, set next parameter to 0775.
   create mask = 0775

# Directory creation mask is set to 0700 for security reasons. If you want to
# create dirs. with group=rw permissions, set next parameter to 0775.
directory mask = 0775

# By default, \\server\username shares can be connected to by anyone
# with access to the samba server.
# Un-comment the following parameter to make sure that only "username"
# can connect to \\server\username
# This might need tweaking when using external authentication schemes
   valid users = %S


創造samba 帳戶和密碼
#smbpasswd -a "username"

enable user
#smbpasswd -e username


重啟
#service smbd restart

使用windows 登入

連接網路磁碟機
\\server\username



=> 避免掛載新磁碟機到各user底下後無法access

sudo vi /etc/samba/smb.conf 在 [global] tag中加入

follow symlinks = yes 

unix extensions = no 

wide links = yes

2016年1月25日 星期一

raspberry ubuntu mate 15.10

##先前準備
SDFormatter
Win32DiskImager

##安裝下載 (請安裝英文版)
https://ubuntu-mate.org/raspberry-pi/


Re-size file system
sudo fdisk /dev/mmcblk0

Delete the second partition (d, 2), then re-create it using the defaults (n, p, 2, enter, enter), then write and exit (w). Reboot the system, then:sudo resize2fs /dev/mmcblk0p2

查看硬碟狀況
df -h

##設定WIFI 且開機自動連線
修改 vim /etc/network/interfacesauto wlan0
allow-hotplup wlan0
iface wlan0 inet static
        address 192.168.0.102
        netmask 255.255.255.0
        gateway 192.168.0.1
        wpa-ssid "SSID"
        wpa-psk "PASSWD"
dns-nameservers 168.95.192.1 8.8.8.8

or
iface wlan0 inet dhcp

ref.http://gsyan888.blogspot.tw/2013/04/raspberry-pi-wireless-network.html

##更新軟體
apt-get update
apt-get upgrade


##關閉圖形介面
graphical enable/disable

console開啟圖形介面
startx

##root遠端登入
1. 修改 /etc/ssh/sshd_config
# vim /etc/ssh/sshd_config
把 PermitRootLogin without-password 前面加上 # 註解
#PermitRootLogin without-password
加入下面這一行
PermitRootLogin yes
2. 重新啟動電腦或重新啟動 ssh Server
# sync;sync;sync;sync;reboot

##安裝必要軟體
*編輯器
apt-get install vim

*多功軟體
apt-get install tmux

*windows to linux 遠端桌面
apt-get install xrdp



##定期動備份至NAS (way1)
在此使用方式為掛載網路硬碟NAS,再使用resync 同步

*掛載 ref.
mkdir /mnt/mountDirector
mount -t cifs //{ip address}/{share} -o username={username},password={password} /mnt/mountDirector/
/mnt/mountDirector/ 為 mount point,取消用 umount /mnt/mountDirector/
編碼可加上iocharset=utf8這種寫法

*RSYNC 備份軟體 (src改為 / (root director)在此的Dest改為/mnt/mountDirector/)
WiKi
rsync -av /src/foo /dest 會在 /dest 下產生一個 foo 的資料夾,表示複製這個資料夾。
rsync -av /src/foo/ /dest 則不會在 /dest 下產生一個 foo 的資料夾,表示複製這個資料夾下

*定期備份

#vim cronfiles/rsync.sh
rsync -av / /mnt/synology_215j/raspberry_ubuntu/

plz try:
rsync -av /* /path/to/backup/folder --exclude={/dev/*,/proc/*,/sys/*,/tmp/*,/run/*,/mnt/*,/media/*,/lost+found}

#vim /etc/crontabm


##定期動備份至NAS (way2)
後來覺得way1 不好用,rsync 常常會跳出error  權限不足之累的,改用ssh 就好一點只要先設好key 的話是頗方便的,請參考
http://programingman.blogspot.tw/2016/05/linux-server.html



最後加進cronfile

  *   *   *   *   *  root /bin/sh /cronfiles/rsync.sh
(分 時 日月 周) (執行者)   (執行shell)   (script)


*收割
#/etc/init.d/cron restart

2016年1月15日 星期五

openwrt OpenWRT UCI API的使用

ref. http://www.tamabc.com/article/1543.html

OpenWRT UCI API的使用


OpenWRT UCI API的使用
UCI 是OpenWRT爲實現配置集中化而引入的一個軟件包, 通過修改UCI,可以實現對OpenWRT的絕對部分配置的修改.LUCI(OpenWRT 的WEB配置界面)也是通過讀UCI配置文件的操作來實現用戶對路由的配置的。通過掌握UCI的API的使用,可以方便地將您的軟件的配置接口集成到LUCI中.

LUCI配置文件簡介


LUCI的配置文件一般存儲在 /etc/config目錄下。比如網絡配置文件則是 /etc/config/network 無線的配置文件是 /etc/config/wireless. 跟多配置文件的含義參考官方 WIKI

基本概念


UCI上下文: struct uci_context *
包(Package): 一個包對應一個UCI格式的文件.類型是 struct uci_package *
節(Section): 一個配置文件的節點. 類型是 struct uci_list *
值(Value):一個節下面可能包含多個值 一個值具有一個名字.
UCI配置文件的基本操作.

首先您需要引入頭文件


#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <uci.h>


static struct uci_context * ctx = NULL; //定義一個UCI上下文的靜態變量.
/*********************************************
*   載入配置文件,並遍曆Section.
*/
bool load_config()
{
    struct uci_package * pkg = NULL;
    struct uci_element *e;


    ctx = uci_alloc_context(); // 申請一個UCI上下文.
    if (UCI_OK != uci_load(ctx, UCI_CONFIG_FILE, &pkg))
        goto cleanup; //如果打開UCI文件失敗,則跳到末尾 清理 UCI 上下文.


    /*遍曆UCI的每一個節*/
    uci_foreach_element(&pkg->sections, e)
    {
        struct uci_section *s = uci_to_section(e);
        // 將一個 element 轉換爲 section類型, 如果節點有名字,則 s->anonymous 爲 false.
        // 此時通過 s->e->name 來獲取.
        // 此時 您可以通過 uci_lookup_option()來獲取 當前節下的一個值.
        if (NULL != (value = uci_lookup_option_string(ctx, s, "ipaddr")))
        {
            ip = strdup(value) //如果您想持有該變量值,一定要拷貝一份。當 pkg銷毀後value的內存會被釋放。
        }
        // 如果您不確定是 string類型 可以先使用 uci_lookup_option() 函數得到Option 然後再判斷.
        // Option 的類型有 UCI_TYPE_STRING 和 UCI_TYPE_LIST 兩種.


    }
    uci_unload(ctx, pkg); // 釋放 pkg 
cleanup:
    uci_free_context(ctx);
    ctx = NULL;
}

遍曆一個UCI_TYPE_LIST 類型.


加入現在有一個如下的配置文件:
    config  "server" "webserver"
        list    "index" "index.html"
        list    "index" "index.php"
        list    "index" "default.html"

代碼片:
// s 爲 section.
struct uci_option * o = uci_lookup_option(ctx, s, "index");
if ((NULL != o) && (UCI_TYPE_LIST == o->type)) //o存在 且 類型是 UCI_TYPE_LIST則可以繼續.
{
    struct uci_element *e;
    uci_foreach_element(&o->v.list, e)
    {
        //這裏會循環遍曆 list
        // e->name 的值依次是 index.html, index.php, default.html
    }
}

寫配置


UCI提供了一個簡潔的辦法來操作配置信息,例如有一個配置文件
#文件名: testconfig
config  'servver'
    option  'value' '123' # 我們想修改 'value' 的值爲 '456'

代碼如下:
struct uci_context * ctx = uci_alloc_context(); //申請上下文
struct uci_ptr ptr ={
    .package = "config",
    .section = "servver",
    .option = "value",
    .value = "256",
};
uci_set(_ctx,&ptr); //寫入配置
uci_commit(_ctx, &ptr.p, false); //提交保存更改
uci_unload(_ctx,ptr.p); //卸載包


uci_free_context(ctx); //釋放上下文

依照上面的例子,我們可以舉一反三, uci_ptr 用來指定信息.而是用uci_set則是寫入信息.同類的函數有如下幾個: 針對list的操作:
    uci_add_list()  // 添加一個list 值
    uci_del_list()  // 刪除一個list 值
    uci_delete()    // 刪除一個option值

2015年9月22日 星期二

alias 的使用 .bashrc

http://jslinux.pixnet.net/blog/post/15534168-%E5%A5%BD%E7%94%A8%E7%9A%84-alias-%E6%8C%87%E4%BB%A4


寫在home底下的  .bashrc

# User specific aliases and functions
alias grep='grep --color=auto -n '
alias cat='cat -n '

 alias tmux='TERM=xterm-256color tmux -2'
~