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

2016年3月7日 星期一

哪一個Shell環境?

處理/etc/rc與.cronrc檔案時,如果不使用Bourne shell的話,會產生一些問題,因為這兩個檔案一定得用Bourne shell來處理。

From "Wicked Cool Shell Scripts"
http://www.intuitive.com/wicked/index.shtml


2016年3月5日 星期六

用自己喜歡的bash色彩


想要修改成自己"甲意"的bash環境很easy
切換到自己的家目錄 cd~
打開家目錄下的隱藏檔 .bashrc
把以下的文字貼到最後一行


export PS1="\[\033[38;5;11m\]\u\[$(tput sgr0)\]\[\033[38;5;15m\]@\h:\[$(tput sgr0)\]\[\033[38;5;6m\][\w]:\[$(tput sgr0)\]\[\033[38;5;15m\] \[$(tput sgr0)\]"
重啟Terminal後就會套用

有個網站專門讓你自製~很直覺
http://bashrcgenerator.com/

2013年7月10日 星期三

常用的主机监控shell脚本


转载时请务必以超链接形式标明文章 原始出处和作者信息及本版权声明。
链接:http://www.dbasky.net/archives/2009/10/shell.html

       有一段时间没更新Blog了主要是时间有点紧张。然而最近时不时有互联网的朋友问我关于服务器监控方面的问题?问常用的服务器监控除了用开源软件,比如:cacti,nagios监控外是否可以自己写shell脚本呢?根据自己的需求写出的shell脚本更能满足需求,更能细化主机监控的全面性。
      下面是我常用的几个主机监控的脚本,大家可以根据自己的情况在进行修改,希望能给大家一点帮助。
 1、查看主机网卡流量

#!/bin/bash
#network
#Mike.Xu
while : ; do
      time='date +%m"-"%d" "%k":"%M'
      day='date +%m"-"%d'
      rx_before='ifconfig eth0|sed -n "8"p|awk '{print $2}'|cut -c7-'
      tx_before='ifconfig eth0|sed -n "8"p|awk '{print $6}'|cut -c7-'
      sleep 2
      rx_after='ifconfig eth0|sed -n "8"p|awk '{print $2}'|cut -c7-'
      tx_after='ifconfig eth0|sed -n "8"p|awk '{print $6}'|cut -c7-'
      rx_result=$[(rx_after-rx_before)/256]
      tx_result=$[(tx_after-tx_before)/256]
      echo "$time Now_In_Speed: "$rx_result"kbps Now_OUt_Speed: "$tx_result"kbps"
      sleep 2
done

2、系统状况监控

#!/bin/sh
#systemstat.sh
#Mike.Xu
IP=192.168.1.227
top -n 2| grep "Cpu" >>./temp/cpu.txt
free -m | grep "Mem" >> ./temp/mem.txt
df -k | grep "sda1" >> ./temp/drive_sda1.txt
#df -k | grep sda2 >> ./temp/drive_sda2.txt
df -k | grep "/mnt/storage_0" >> ./temp/mnt_storage_0.txt
df -k | grep "/mnt/storage_pic" >> ./temp/mnt_storage_pic.txt
time=`date +%m"."%d" "%k":"%M`
connect=`netstat -na | grep "219.238.148.30:80" | wc -l`
echo "$time  $connect" >> ./temp/connect_count.txt


 
3、监控主机的磁盘空间,当使用空间超过90%就通过发mail来发警告

#!/bin/bash
#monitor available disk space
SPACE='df | sed -n '/ \ / $ / p' | gawk '{print $5}' | sed  's/%//'
if [ $SPACE -ge 90 ]
then
fty89@163.com
fi

4、 监控CPU和内存的使用情况

#!/bin/bash
#script  to capture system statistics
OUTFILE=/home/xu/capstats.csv
DATE='date +%m/%d/%Y'
TIME='date +%k:%m:%s'
TIMEOUT='uptime'
VMOUT='vmstat 1 2'
 USERS='echo $TIMEOUT | gawk '{print $4}' '
LOAD='echo $TIMEOUT | gawk '{print $9}' | sed "s/,//' '
FREE='echo $VMOUT | sed -n '/[0-9]/p' | sed -n '2p' | gawk '{print $4} ' '
IDLE='echo  $VMOUT | sed -n '/[0-9]/p' | sed -n '2p' |gawk '{print $15}' '
echo "$DATE,$TIME,$USERS,$LOAD,$FREE,$IDLE" >> $OUTFILE

5、全方位监控主机

#!/bin/bash
# check_xu.sh
# 0 * * * * /home/check_xu.sh

DAT="`date +%Y%m%d`"
HOUR="`date +%H`"
DIR="/home/oslog/host_${DAT}/${HOUR}"
DELAY=60
COUNT=60
# whether the responsible directory exist
if ! test -d ${DIR}
then
        /bin/mkdir -p ${DIR}
fi
# general check
export TERM=linux
/usr/bin/top -b -d ${DELAY} -n ${COUNT} > ${DIR}/top_${DAT}.log 2>&1 &
# cpu check
/usr/bin/sar -u ${DELAY} ${COUNT} > ${DIR}/cpu_${DAT}.log 2>&1 &
#/usr/bin/mpstat -P 0 ${DELAY} ${COUNT} > ${DIR}/cpu_0_${DAT}.log 2>&1 &
#/usr/bin/mpstat -P 1 ${DELAY} ${COUNT} > ${DIR}/cpu_1_${DAT}.log 2>&1 &
# memory check
/usr/bin/vmstat ${DELAY} ${COUNT} > ${DIR}/vmstat_${DAT}.log 2>&1 &
# I/O check
/usr/bin/iostat ${DELAY} ${COUNT} > ${DIR}/iostat_${DAT}.log 2>&1 &
# network check
/usr/bin/sar -n DEV ${DELAY} ${COUNT} > ${DIR}/net_${DAT}.log 2>&1 &
#/usr/bin/sar -n EDEV ${DELAY} ${COUNT} > ${DIR}/net_edev_${DAT}.log 2>&1 &


   放在crontab里每小时自动执行:
0 * * * * /home/check_xu.sh

   这样会在/home/oslog/host_yyyymmdd/hh目录下生成各小时cpu、内存、网络,IO的统计数据。


   如果某个时间段产生问题了,就可以去看对应的日志信息,看看当时的主机性能如何

2013年5月28日 星期二

find : 尋找特定字串的檔案或目錄

說明 : 
find 式搜尋特定檔案最常被使用的指令, 可以搜尋的條件包括時間, 名稱與權限等.

屬性 : 系統相關 - 檔案與目錄 
語法 : find [路徑] [語法] 
語法 | 功能 
-amin 分鐘 | 搜尋指定時間內曾被存取的檔案或目錄
-anewwe 檔案 | 搜尋比指定檔案儲存時間較新檔案
-atime 小時 | 搜尋指定時間內曾被存取的檔案或目錄, 單位為小時, 以24小時計
-cmin 分鐘 | 搜尋比指定時間新的檔案, 單位為分鐘
-ctime 小時 | 搜尋在指定時間被變更的檔案.
-empty | 搜尋檔案大小為0 或是目錄下沒有檔案的目錄.
-fstype 檔案系統類型 | 指定檔案系統類型.
-gid 群組號碼 | 指定群組號碼
-group 群組名稱 | 指定群組名稱
-inum inode個數 | 指定inode個數
-mmin 分鐘 | 搜尋指定時間內變動的檔案或目錄, 以分鐘計
-mtime 小時 | 搜尋指定時間內變動的檔案或目錄, 以小時計
-name 範本 | 指定範本樣式的檔名
-nouser | 搜尋不屬於本機使用者的檔案或目錄
-nogroup | 搜尋不屬於本機群組的檔案或目錄
-path 範本 | 指定目錄的範本
-perm 權限數值 | 搜尋指定權限的檔案或目錄.
-regex 範本 | 指定範本格式
-size 檔案大小 | 搜尋指定大小的檔案
-type 檔案型態 | 搜尋符合檔案型態的檔案, 包括 b(block special), c(character special), d(directory), p(named pipe), f(regular file), l(symbolic link) 以及s(socket)
-uid 使用者id | 指定使用者id
-used 日數 | 異動後天數
-user 使用者名稱 | 指定使用者名稱


執行範例 : 
* 尋找 /bin 之下以 ps 開頭的檔案 
server1:~ # find /bin -name ps* 
/bin/psfstriptable 
/bin/psfaddtable 
/bin/psfgettable 
...

* 尋找權限為 rwsr-xr-x 的檔案 ( rwsr-xr-x 的權限以數字表示為4755 ) 
server1:~ # find / -perm 4755 
/opt/gnome/lib/libgnomesu/gnomesu-pam-backend 
/opt/gnome/sbin/change-passwd 
/opt/kde3/bin/kpac_dhcp_helper 
/usr/lib/pt_chown 
/usr/bin/opiepasswd 
/usr/bin/opiesu 
/usr/bin/sudo 
/usr/bin/mandb 
...

如何在 Linux 底下找到想找的東西? - OpenFoundry

如何在 Linux 底下找到想找的東西? - OpenFoundry
好物,有不收藏的道理嗎?

搜尋最大或最新的檔案

在多個資料夾內搜尋

排除指定目錄

搜尋特定類型的檔案

搜尋重複檔案

搜尋檔案內的文字

搜尋文字區塊

管理使用者及檔案

2013年5月27日 星期一

刪除空白目錄 find應用

刪除空白目錄  find -type d -empty -delete 

-type
Supported. POSIX specifies 'b', 'c', 'd', 'l', 'p', 'f' and 's'. GNU find also supports 'D', representing a Door, where the OS provides these.

2013年2月1日 星期五

常用 vi 密技之一

每次遇到檔案需要大量註解就得google一次很麻煩,所以把爬文發現筆記到這兒
文章: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中,Ctrl + V的區塊模式還有很多有趣的功能可以摸索。
其他進階功能,可以參考Vim的官方網站。
http://www.vim.org

筆記網站:http://result945.pixnet.net/blog/post/28300565-vim大量註解

2011年3月19日 星期六

shell script - snmp

echo "-----------------------------"
echo "Enter First 3 Octets (x.x.x):"
read ipthree
echo "-----------------------------"
echo "Enter 4th Octet FIRST IP:"
read ipfirst
echo "-----------------------------"
echo "Enter 4th Octet ENDING IP:"
read iplast
echo "-----------------------------"
sleep 1
clear

2011年3月18日 星期五

creat many account by awk

利用以下兩個檔案大量新增帳號及建目錄
中研院計算中心ASPAC計劃 , 我的參考文件是94年的文件AWK Tutorial Guide 。
##mkuser.awk
=========================================
##del useraccount
#{ print "userdel  -r " $1 " " } 

##user account add
{ print "useradd  -d /home/u97/u97337/"$1" -c "$3" "$1" -e " $4}
{ print "echo " $1 ":" $2 " | chpasswd "}

##user www
{ print "mkdir /home/u97/u97337/"$1"/www" }
{ print "chown  "$1 ":" $1 " /home/u97/u97337/"$1"/www" }
{ print "touch /home/u97/u97337/"$1"/www/index.html" }
{ print "chown  "$1 ":" $1 " /home/u97/u97337/"$1"/www/index.html" }
#{ print "chmod  755 -R /home/u97/" }
~

#u97337(新增的清單)
==================
u973370xx       T123456789      "VCD097劉X潔"  2011-09-01
u973370xx       A223456789      "VCD097戴X耘"  2011-09-01

2011年3月17日 星期四

UNIX 文字工具: pipe

UNIX 文字工具: pipe

UNIX 除了可以將資料流導入或導出純文字檔案之外, 還可以將它導向另一個工具軟體,或是濾器。 這個機制稱為導管 (pipe)。導管符號是 | (通常在鍵盤的 Shift+\ 位置)。 | 的功能就是將它左邊的 stdout 資料變成右邊的 stdin。 所以,只要一個工具軟體的輸出是 stdout, 一個工具軟體的輸入是 stdin,就可以用導管將它們銜接在一起, 變成一個新的工作流程 (pipeline)。
例如
cat -n list.txt | less
就是將加上列號的 list.txt 檔案,放進 less 閱讀工具中。再如
tail -12 list.txt | cat -n
就是將 list.txt 的後 12列,加上列號,印在螢幕上。
只要合乎規定,UNIX 並不限定一個流程中可以使用幾次導管。例如您可以說
tail -22 list.txt | cat -n | less

如果您想要看到一個文字檔案從第 n 列到第 m 列的內容, 可以說
head -m FILE | tail -K
其中 K = m - n + 1。
舉個特例。如果想看到  list.txt 的第 5 列,就說
head -5 forty.txt | tail -1

參考http://libai.math.ncu.edu.tw/bcc16/6/textutil/index.html

2010年3月10日 星期三

Shell 環境

FreeBSD 內附的 Shell 有 sh、csh、tcsh,sh 名為 Bourne Shell,它是 UNIX 系統中最悠久的 shell,所有的 UNIX 系統都會內附這個 shell,我們在寫 shell scripts 時為了可移植性,大多使用 sh。在 FreeBSD 中 csh 和 tcsh 是一樣的東西,csh 名稱的由來是因為它的 scripts 語法和 C 語言很像。至於要使用哪一種 shell 全憑個人偏好,您也可以自行從 ports 中安裝 Linux 用的 bash 或 ksh。

例如,我們要將預設的文書編輯器改成 ee,
在 tcsh 中可以使用:
# setenv EDITOR ee
如果您使用的是 Bourne Shell:
# export EDITOR="ee"
 
在 /etc 下的 csh.cshrc 
是通用的設定,但如果使用者自己的設定和通用設定一樣時,會以使用者的設定為主。 
如果您希望日後每個新增的使用者的設定都是這樣,則必須修改 /usr/share/skel/dot.cshrc 檔案
 
/etc/csh.cshrc 和 /etc/csh.login的差別在於如果是使用者登入,二個檔案都會被執行,如果是 scripts 則只會去讀取 csh.cshrc。 
http://twbsd.org/cht/book/index.php?ch=03 
 
相關資料:第十一章、認識與學習 BASH  

2009年11月8日 星期日

Shell Script 相關文件

[文件] Shell Script 相關文件(含cut, awk, sed, grep等)
Shell 設計入門 臥龍小三 ols3@www.tnc.edu.tw 台南縣教育網路中心
http://linux.tnc.edu.tw/techdoc/shell/book1.html

awk 的使用說明文件
1. AWK Tutorial Guide 中央研究院計算中心 ASPAC計劃 (中文)
http://phi.sinica.edu.tw/aspac/reports/94/94011/
2. The GNU Awk User's Guide (英文)
http://www.gnu.org/software/gawk/manual/gawk.html

awk 書本
1. 書名:sed & awk 程式設計 第二版 作者:Dale Dougherty & Arnold Robbins 編譯:陳爽 / 蔣大偉 ISBN:957-8247-67-2
http://www.oreilly.com.tw/product_unix.php?id=a084_toc

2009年2月18日 星期三

sed

Want to use sed(1) to edit a file in place? Well, to replace every 'e' with an 'o', in a file name 'foo', you can do:

sed -i.bak s/e/o/g foo

And you'll get a backup of the original in a file name 'foo.bak', but if you want no backup:

sed -i '' s/e/o/g foo

2008年4月8日 星期二

sort 應用

sort filename
將檔案中的資料排序

$ sort -nk2 filename

參數說明:
-n:對「數字」排序,如果沒有這個參數,則是對「文字」排序
-k2:以第二行的資料為排序基準
-r:將大小順序反過來排

延伸閱讀:
http://linux.vbird.org/linux_basic/0320bash.php#pipe_2

熱門文章