顯示具有 指令說明 標籤的文章。 顯示所有文章
顯示具有 指令說明 標籤的文章。 顯示所有文章

2018年11月20日 星期二

sed 搜尋某特定字符串的行頭添加字符串

sed 搜尋某特定字符串的行頭添加字符串

沒學好只好多記錄 

sed -i '/字串/  s/^/#/'  /tmp/testfile


2016年12月27日 星期二

隨手筆記 指令

隨手筆記 指令

###change AP serevr output log #### 
:%s/Patch_program\/\(\S\+\)\/\S\+.log/Patch_log\/\1\/console.log/g  



####change AP crontab to each file #### 
grep '/opt/scripts/batch/ap_cron.sh' /etc/crontab | for i in `awk '{print $8}'` ; do grep " $i "  /etc/crontab > AP_$i ; done 


##delete postque yahoo domain 
postqueue -p | tail -n +2 | awk 'BEGIN { RS = "" } /@yahoo\.com/ { print $1 }' | tr -d '*!' | postsuper -d - 

2013年9月26日 星期四

mount remount

mount -o remount,rw /


很簡單的一個remount 成可讀寫的file system ,但前題下,

要做時最好要先進 single user mode 丫丫丫丫丫~~~~~~~

PS: 當 FSTAB 掛載不到會進入一個要求你KEY root 密碼,那其實不是 single user mode 




2012年7月22日 星期日

sysctl.conf 相關設定


# Disables packet forwarding
net.ipv4.ip_forward = 1

# Enables source route verification
net.ipv4.conf.default.rp_filter = 1

# Disables the magic-sysrq key
kernel.sysrq = 0

#Sets the max system file descriptor limit to 500000
fs.file-max = 500000

#Sets the default Socket Listen Queue Length
net.ipv4.tcp_max_syn_backlog=12000

#sets min/default/max TCP read buffer (default 4096 87380 174760)
net.ipv4.tcp_rmem = 30000000 30000000 30000000
net.ipv4.tcp_wmem = 30000000 30000000 30000000
net.ipv4.tcp_mem = 30000000 30000000 30000000

#maximum receive socket buffer size  (Default 131071)
net.core.rmem_max = 10000000

#maximum send socket buffer size Default 131071)
net.core.wmem_max = 10000000

#default receive socket buffer size (Default 65535)
net.core.rmem_default = 10000000

#default send socket buffer size (Default 65535)
net.core.wmem_default = 10000000

#maximum amount of option memory buffers (Default 10240)
net.core.optmem_max = 10000000

#Define the local TCP ephemeral port range
net.ipv4.ip_local_port_range = 8192 65535

#Enable fast recycling of connections in the TIME_WAIT state
net.ipv4.tcp_tw_recycle = 1

#Maximum number of TIME_WAIT sockets held by the system simultaneously.
net.ipv4.tcp_max_tw_buckets = 2000000

#Number of unprocessed input packets before the kernel starts
#dropping them. (Default 10240).
net.core.netdev_max_backlog = 400000

# Adjusts the minimum number of entries to keep in the ARP cache.
# The garbage collector will not run if there  are  fewer than
# this number of entries in the cache.
#(Default 128)
net.ipv4.neigh.default.gc_thresh1 = 1024

#The  soft  maximum  number  of entries to keep in the ARP cache.
# The garbage collector will allow the number of entries to exceed
#this for 5 seconds before collection will be performed.
#(Default 128).
net.ipv4.neigh.default.gc_thresh2 = 1024

#The hard maximum number of entries to keep in the ARP cache.
# The garbage collector will always run if there are more than
#this number of entries in the cache.  (Default 512)
net.ipv4.neigh.default.gc_thresh3 = 2048

#The interval to do garbage collection on the arp table.
# (Defaults to 30).
net.ipv4.neigh.default.gc_interval = 3600

#When to time-out an arp table entry.  (Defaults 60).
net.ipv4.neigh.default.gc_stale_time = 3600

#When to time-out an arp table entry.  Do this for every NIC.
#  (Defaults to 60).
net.ipv4.neigh.eth0.gc_stale_time = 3600
net.ipv4.tcp_syncookies = 1

#
kernel.sem = 1000 256000 60 1024

# Share Memory
kernel.shmmni = 4096
kernel.shmall = 2097152
kernel.shmmax = 2147483647

# Per message Size
kernel.msgmnb = 11000000
kernel.msgmni = 3200
kernel.msgmax = 210000

# This causes the kernel to avoid using lower memory areas if address space is available in the high memory area.
vm.lower_zone_protection = 100

# Useful for debugging multi-threaded applications
kernel.core_uses_pid = 1

# Ignore ping the broadcast address of a network
net.ipv4.icmp_echo_ignore_broadcasts = 1

# Set this to ignore ICMP errors caused by hosts in the network reacting badly to frames sent to what they perceive to be the broadcast address.
net.ipv4.icmp_ignore_bogus_error_responses = 1

# Security Option
net.ipv4.conf.default.accept_source_route = 0
net.ipv4.conf.all.accept_source_route = 0
net.ipv4.conf.all.accept_redirects = 0

# Disables ICMP Redirect
net.ipv4.conf.all.send_redirects = 0

# Protect against wrapping sequence numbers
net.ipv4.tcp_timestamps = 0

# This setting determines the number of SYN+ACK packets sent before the kernel gives up on the connection.
net.ipv4.tcp_synack_retries = 2

# Time to hold socket in state FIN-WAIT-2, if it was closed by our side. Default value is 60sec
net.ipv4.tcp_fin_timeout = 5

# How many times to retry before deciding that something is wrong and it is necessary to report this suspicion to network layer. Minimal RFC value is 3, it is default
net.ipv4.tcp_retries1 = 3

# How many keepalive probes TCP will send, until it decides that the connection is broken. Default value: 9.
net.ipv4.tcp_keepalive_probes = 5

# How frequent probes are retransmitted, when a probe isn't acknowledged. Default: 75 seconds.
net.ipv4.tcp_keepalive_intvl = 15

# How often TCP sends out keepalive messages when keepalive is enabled. Default: 2hours
net.ipv4.tcp_keepalive_time = 60

# TCP/IP normally allows windows up to 65535 bytes big. For really fast networks, this may not be enough.
net.ipv4.tcp_window_scaling = 0

# Use Selective ACK which can be used to signify that specific packets are missing - therefore helping fast recovery.
net.ipv4.tcp_sack = 0

# Hidden
net.ipv4.conf.lo.arp_ignore=1
net.ipv4.conf.lo.arp_announce=2
net.ipv4.conf.all.arp_ignore=1
net.ipv4.conf.all.arp_announce=2

2012年7月18日 星期三

vi 與 vim 的指令整理


vi 是 unix 家族下最功能強大的文字編輯器,讓用戶只要使用一個鍵盤就可以完成所有的編輯。而 vim 則是 vi 的加強版,甚至在 Windows 上也找得到 vim 的芳蹤。但 vi/vim 眾多的指令卻經常令初學者卻步,以下就是我所整理出來那些令人卻步的指令:


指令 說明
i 在游標位置進入編輯模式
I 在游標行的第一個非空白字元進入編輯模式
a 在游標位置後進入編輯模式
A 在游標行的最後一個字元進入編輯模式
o 向下新增一行,並進入編輯模式
O 向上新增一行,並進入編輯模式
cc 刪除游標行,並進入編輯模式
[ESC] 取消指令或退出編輯模式
游標移動

指令 說明
gg 移到第一行
G 移到最後一行
行數 → G 移動到第 n 行
0 移動到該行最前面
$ 移動到該行最後面
字數 → [Space] 向右移動 n 個字元
行數 → [Enter] 向下移動 n 行 <h3>標記與複製</h3>
指令 說明
v 開始字串標記
V 開始行標記
v → [Ctrl]-V 開始區塊標記
d 刪除標記的內容
y 複製標記的內容
yy 複製游標行
yG 複製游標行到最後一行
y1G 複製游標行到第一行
y$ 複製游標處到最後一個字元
y0 複製游標處到第一個字元
p 在下一行貼上複製或刪除的內容
P 在上一行貼上複製或刪除的內容
[Ctrl]-R → 0 在下一行貼上複製或刪除的內容,適用於編輯模式及指令行 <h3>搜尋與取代</h3>
指令 說明
/搜尋字串 向下搜尋字串
* 將游標移到字串上,直接按 “*” 也可以做向下搜尋
?搜尋字串 向上搜尋字串
:set ic 搜尋時不分大小寫
:set noic 搜尋時要分大小寫
n 繼續下一個搜尋結果
N 繼續上一個搜尋結果
:起始行,終止行s/搜尋字串/取代字串/gic 從第 n 行到第 n 行取代字串 (後面的 g: 整行全部, i: 不分大小寫,


c: 詢問)

:1,$s/搜尋字串/取代字串/gic 全部取代字串 (後面的 g: 整行全部, i: 不分大小寫,
c: 詢問) <h3>刪除</h3>
指令 說明
dd 刪除游標行
dG 刪除游標行到最後一行
d1G 刪除游標行到第一行
d$ 刪除游標處到最後一個字元
d0 刪除游標處到第一個字元 <h3>檔案功能</h3>
指令 說明
:w 存檔 (加 ! 表示強制存檔)
:w 檔案名稱 另存新檔
:wq 存檔並退出 vi
:e 檔案名稱 編輯其它檔案
:e! 還原至檔案編修前的狀態
:r 檔案名稱 讀入檔案內容,並加到游標行的後面
:n 切換到下一個開啟的檔案
:N 切換到上一個開啟的檔案
:set nu 顯示行號
:set nonu 取消行號顯示
:files 列出所有開啟的檔案 <h3>視窗分割</h3>
指令 說明
:new 新增水平視窗
:new 檔案名稱 新增水平視窗,並在新增的視窗載入檔案
:vnew 新增垂直視窗
:vnew 檔案名稱 新增垂直視窗,並在新增的視窗載入檔案
[Ctrl]-W → [方向鍵] 切換視窗
:only 僅保留目前的視窗 <h3>其它</h3>
指令 說明
J 將游標行與下一行合併
u 還原指令
[Ctrl]-R 重做指令
. 重覆上一個指令
! 命令 執行 linux 指令,並顯示執行結果
:q 退出 vi (加 ! 表示強制退出) <h3>vim 的設定檔</h3>
通常我會編輯 /etc/vimrc,在檔案最後加入:

" 顯示列號
    set number
    " 語法高亮度顯示
    syntax on
    " 標記搜尋到的字串
    set hlsearch
    " 自動縮排
    set autoindent
    " 顯示說明
    set ruler
    " 顯示編輯狀態
    set showmode
    " 設定註解的顏色
    highlight Comment ctermfg=cyan
    " 設定搜尋到的字串顏色
    highlight Search term=reverse ctermbg=4 ctermfg=7
    " 設定 tab 鍵的字元數
    set tabstop=4


好用的 netcat 指令


netcat 就像 cat 指令一樣,只是將簡單的 cat 功能完全「網路化」,好用與靈活的程度可說是很驚人,而在 Top 100 Network Security Tools 排行榜中還排行第四名呢!

netcat 除了可以當 TCP Client 工具以外,還可以當 TCP Server 的工具,以下我用幾個簡單的例子說明:

示範 netcat 如何當 TCP Client 工具

底下這段指令代表你要將 /etc 目錄整個打包並壓縮起來,並將資料傳送到 10.0.0.99 主機的 Port 3456,並指定若超過 30 秒沒有連上目的地或 30 秒沒有回應的話自動 Timeout。

# tar -zcf - /etc | netcat -w 30 10.0.0.99 3456
示範 netcat 如何當 TCP Server 工具

如果拿以上的例子,你可能會想說那你還要自己寫一個 Server 接受這些封包啊!答案是:「只要用 netcat 就可以拿來當 Server 用了,一行程式也不用寫」,底下這段指令是假設從 10.0.0.99 主機執行的。

# netcat -l -p 3456 > /backup/my_etc_backup.tar.gz
-l 參數代表進入 Listen mode

-p 代表 Listen 的 Port 為 3456

之後的 > my_etc_backup.tar.gz 代表你要將從 TCP 傳入的所有資料都導入到 /backup/my_etc_backup.tar.gz 檔案中。

用以上兩個非常簡單的例子就可以知道,原來在網路之間傳輸資料可以如此的簡單。若知道 rsync 工具的人可能會笑說:「用 netcat 太麻煩了吧,用 rsync 同步或備份檔案才更好用吧」。是的,如果要「同步檔案」的話,當然用 rsync 比較好用,不過 netcat 還可以做的更多!

因為 netcat 可以模擬任何所有 TCP Protocol 的動作,就如同我們常用 telnet 測試 HTTP、POP3、或 SMTP 協定的時候一樣,只是使用 netcat 可以讓我們更自動化的做些測試,舉幾個例子如下:

1. 測試 HTTP 運作是否正常

首先,必須先建立要下的 HTTP 指令檔,假設叫 GET_tw.yahoo.com.txt,內容如下:

GET / HTTP/1.0
Host: tw.yahoo.com
注意:在指令的最後面必須要有兩個「斷行」符號,所以你可以在文字檔後面加上兩行空白行。

然後就可以用以下指令取得 Yahoo! 奇摩 的首頁 HTML:

# cat GET_tw.yahoo.com.txt | nc tw.yahoo.com 80
再透過一些指令或程式就可以將測試網頁是否還活著的偵測程式完成且自動化了。

2. 測試 SMTP 運作是否正常

測試 SMTP 的方式也跟 HTTP 一樣,就是先把指令都給先寫好,假設檔名是 SMTP_test.txt,內容如下:

HELO localhost
MAIL FROM:<will@test.com>
RCPT TO:<will@test.com>
DATA
To: Will <will@test.com>
Subject: This is a test
This is first line
.
QUIT
然後就可以用以下指令將 SMTP Command 送到 smtp.test.com 了:

#  cat SMTP_test.txt | nc smtp.test.com 25
執行的結果如下:

# cat SMTP_test.txt | nc smtp.test.com 25
220 smtp.test.com ESMTP Service ready Fri, 11 Jul 2008 19:13:17 +0800 (CST)
250 smtp.test.com
250 Sender <doggy@test.com> OK
250 Recipient <doggy@test.com> OK
354 Enter mail, end <CRLF>.<CRLF>
250 Message accepted for delivery
221 smtp.test.com

引數列項目過長?什麼鬼


引數列項目過長

查了一下好像是系統限制,處理的檔案數列

A.當檔案數太多, 砍不掉的時後該怎麼辦(錯誤訊息: rm: too many arguments 或 rm: Argument list too long)

先轉成 file owner 的身份去下指令(不然就轉成 root 吧!)

 ls -l | awk '{print $9}' | xargs rm  # 再來這樣子就可以砍掉

 ls -l *.jpg | awk '{print $9}' | xargs rm # 如果只要砍那目錄下的所有 jpg 檔簡單的 shell script 就搞定囉 :)

不過使用前請小心確認一下, 最後 xargs rm 先拿掉, 改成 less, 先確定一下是自己要砍的, 再下此指令吧 :)

B.-bash: /bin/mv: 引數列項目過長 or /bin/find 引數列項目過長
 
     解法: find $path -name '*.*' |xargs grep -i XXX

好像是使用xargs 就可以處理

正規表示式 Regular Expression

完全只是會忘記@@~所以寫在這~給自己看

資料來源


字元說明簡單範例
\避開特殊字元/A\*/ 可用於比對 "A*",其中 * 是一個特殊字元,為避開其特殊意義,所以必須加上 "\"
^比對輸入列的啟始位置/^A/ 可比對 "Abcd" 中的 "A",但不可比對 "aAb"
$比對輸入列的結束位置/A$/ 可比對 "bcdA" 中的 "A",但不可比對 "aAb"
*比對前一個字元零次或更多次/bo*/ 可比對 "Good booook" 中的 "booo",亦可比對 "Good bk" 中的 "b"
+比對前一個字元一次或更多次,等效於 {1,}/a+/ 可比對 "candy" 中的 "a",但不可比對 "caaandy"
?比對前一個字元零次或一次/e?le?/ 可比對 "angel" 中的 "el",但不可比對 "angle"
.比對任何一個字元(但換行符號不算)/.n/ 可比對 "nay, an apple is on the tree" 中的 "an" 和 "on",但不可比對 "nay"
(x)比對 x 並將符合的部分存入一個變數/(a*) and (b*)/ 可比對 "aaa and bb" 中的 "aaa" 和 "bb",並將這兩個比對得到的字串設定至變數 RegExp.$1 和 RegExp.$2。
xy比對 x 或 y/a*b*/g 可比對 "aaa and bb" 中的 "aaa" 和 "bb"
{n}比對前一個字元 n 次,n 為一個正整數/a{3}/ 可比對 "lllaaalaa" 其中的 "aaa" 但不可比對 "aa"
{n,}比對前一個字元至少 n 次,n 為一個正整數/a{3,}/ 可比對 "aa aaa aaaa" 其中的 "aaa" 及 "aaaa" 但不可比對 "aa"
{n,m}比對前一個字元至少 n 次,至多 m 次,m、n 均為正整數/a{3,4}/ 可比對 "aa aaa aaaa aaaaa" 其中的 "aaa" 及 "aaaa" 但不可比對 "aa" 及 "aaaaa"
[xyz]比對中括弧內的任一個字元/[ecm]/ 可比對 "welcome" 中的 "e" 或 "c" 或 "m"
[^xyz]比對不在中括弧內出現的任一個字元/[^ecm]/ 可比對 "welcome" 中的 "w" "l" "o" 可見出其與 [xyz] 功能相反 同時請同學也注意 /^/ 與 [^] 之間功能的不同
[\b]比對退位字元(Backspace character)可以比對一個 backspace ,也請注意 [\b] 與 \b 之間的差別
\b比對英文字的邊界,例如空格例如 /\bn\w/ 可以比對 "noonday" 中的 'no' ;
/\wy\b/ 可比對 "possibly yesterday." 中的 'ly'
\B比對非「英文字的邊界」例如, /\w\Bn/ 可以比對 "noonday" 中的 'on' ,
另外 /y\B\w/ 可以比對 "possibly yesterday." 中的 'ye'
\cX比對控制字元(Control character),其中 X 是一個控制字元/\cM/ 可以比對 一個字串中的 control-M
\d比對任一個數字,等效於 [0-9]/[\d]/ 可比對 由 "0" 至 "9" 的任一數字 但其餘如字母等就不可比對
\D比對任一個非數字,等效於 [^0-9]/[\D]/ 可比對 "w" "a"... 但不可比對如 "7" "1" 等數字
\f比對 form-feed若是在文字中有發生 "換頁" 的行為 則可以比對成功
\n比對換行符號若是在文字中有發生 "換行" 的行為 則可以比對成功
\r比對 carriage return
\s比對任一個空白字元(White space character),等效於 [ \f\n\r\t\v]/\s\w*/ 可比對 "A b" 中的 "b"
\S比對任一個非空白字元,等效於 [^ \f\n\r\t\v]/\S/\w* 可比對 "A b" 中的 "A"
\t比對定位字元(Tab)
\v比對垂直定位字元(Vertical tab)
\w比對數字字母字元(Alphanumerical characters)或底線字母("_"),等效於 [A-Za-z0-9_]/\w/ 可比對 ".A _!9" 中的 "A" "_" "9"
\W比對非「數字字母字元或底線字母」,等效於 [^A-Za-z0-9_]/\W/ 可比對 ".A _!9" 中的 "." " " "!" 可見出其與 \w 功能恰好相反
\ooctal比對八進位,其中octal是八進位數目/\oocetal123/ 可比對 與 八進位的ASCII中 "123" 所相對應的字元值
\xhex比對十六進位,其中hex是十六進位數目/\xhex38/ 可比對 與 16進位的ASCII中 "38" 所相對應的字元



通用式說明及範例比對不成立之字串
/a/含字母 "a" 的字串,例如 "ab", "bac", "cba""xyz"
/a./含字母 "a" 以及其後任一個字元的字串,例如 "ab", "bac"(若要比對.,請使用 \.)"a", "ba"
/^xy/以 "xy" 開始的字串,例如 "xyz", "xyab"(若要比對 ^,請使用 \^)"axy", "bxy"
/xy$/以 "xy" 結尾的字串,例如 "axy", "abxy"以 "xy" 結尾的字串,例如 "axy", "abxy" (若要比對 $,請使用 \$)"xya", "xyb"
/[13579]/包含 "1" 或 "3" 或 "5" 或 "7" 或 "9" 的字串,例如:"a3b", "1xy""y2k"
/[0-9]/含數字之字串不含數字之字串
/[a-z0-9]/含數字或小寫字母之字串不含數字及小寫字母之字串
/[a-zA-Z0-9]/含數字或字母之字串不含數字及字母之字串
/b[aeiou]t/"bat", "bet", "bit", "bot", "but""bxt", "bzt"
/[^0-9]/不含數字之字串(若要比對 ^,請使用 \^)含數字之字串
/[^aeiouAEIOU]/不含母音之字串(若要比對 ^,請使用 \^)含母音之字串
/[^\^]/不含 "^" 之字串,例如 "xyz", "abc""xy^", "a^bc"


通用表示法的特定字元說明等效的通用表示法
\d數字[0-9]
\D非數字[^0-9]
\w數字、字母、底線[a-zA-Z0-9_]
\W非 \w[^a-zA-Z0-9_]
\s空白字元[ \r\t\n\f]
\S非空白字元[^ \r\t\n\f]
通用表示法說明
/a?/零或一個 a(若要比對? 字元,請使用 \?)
/a+/一或多個 a(若要比對+ 字元,請使用 \+)
/a*/零或多個 a(若要比對* 字元,請使用 \*)
/a{4}/四個 a
/a{5,10}/五至十個 a
/a{5,}/至少五個 a
/a{,3}/至多三個 a
/a.{5}b/a 和 b中間夾五個(非換行)字元

Linux 在vim 中 如何區塊編輯

Linux 在vim 中 如何區塊編輯~


1. ctrl + v  後 選取 要編輯的區塊

2. 建入 大寫I

3. 輸入要編輯的文字

4 按ESC 離開

5 最後記得存檔...XD