服务器维护,服务器代维,安全设置,漏洞扫描,入侵检测服务

dirtysea 发表于 2013-4-18 14:25:20

10个工具让你的 shell 脚本更强大

<P>很多人误以为shell脚本只能在命令行下使用。其实shell也可以调用一些GUI组件,例如菜单,警告框,进度条等等。你可以控制最终的输出,光标位置还有各种输出效果。下面我将介绍一些工具,帮助你创建强大的,互动的,用户友好的 Unix/Linux shell脚本。我在FreeBSD和Linux下测试过这些工具,不过其他UNIX系列的操作系统应该都支持的。 <BR minmax_bound="true"><BR minmax_bound="true"><SPAN style="FONT-WEIGHT: bold" minmax_bound="true">1. notify-send 命令</SPAN> <BR minmax_bound="true">这个命令可以让你通过通知进程发送一个桌面通知给用户。这可以用来向用户发送提示,或者显示一些信息而不用打断用户工作。你需要安装如下软件包: <BR minmax_bound="true"></P><PRE class="brush:shell; toolbar: true; auto-links: false;" minmax_bound="true">$ sudo apt-get install libnotify-bin</PRE>
<P>下面这个例子展示了如何从命令行向桌面发送一个简单的消息: <BR minmax_bound="true"></P><PRE class="brush:shell; toolbar: true; auto-links: false;" minmax_bound="true">notify-send "rsnapshot done :)" </PRE>
<P>输出: <BR minmax_bound="true"><IMG style="WIDTH: auto; HEIGHT: auto; minmaxWidth: auto; minmaxHeight: auto; maxWidth: 600px" alt="" src="http://static.oschina.net/uploads/img/201202/27224457_u8Vq.png" minmax_bound="true"> <BR minmax_bound="true"><BR minmax_bound="true">下面是一个复杂一点的例子: <BR minmax_bound="true"></P><PRE class="brush:shell; toolbar: true; auto-links: false;" minmax_bound="true">....
alert=18000
live=$(lynx --dump http://money.rediff.com/ | grep 'BSE LIVE' | awk '{ print $5}' | sed 's/,//g;s/\.*//g')
[ $notify_counter -eq 0 ] &amp;&amp; [ $live -ge $alert ] &amp;&amp; { notify-send -t 5000 -u low -i   "BSE Sensex touched 18k";notify_counter=1; }
...</PRE>
<P>输出: <BR minmax_bound="true"><IMG style="WIDTH: auto; HEIGHT: auto; minmaxWidth: auto; minmaxHeight: auto; maxWidth: 600px" alt="" src="http://static.oschina.net/uploads/img/201202/27224459_5fIA.png" minmax_bound="true"> <BR minmax_bound="true"><BR minmax_bound="true">这里的参数解释如下: <BR minmax_bound="true"></P>
<UL minmax_bound="true">
<LI minmax_bound="true">&nbsp;-t 5000:指定超时的时间,毫秒
<LI minmax_bound="true">&nbsp;-u low:设置是否紧急
<LI minmax_bound="true">&nbsp;-i gtk-dialog-info:通知图标,你可以指定图标 -i /path/to/your-icon.png </LI></UL>
<P><BR minmax_bound="true"><SPAN style="FONT-WEIGHT: bold" minmax_bound="true">2. tput 命令</SPAN> <BR minmax_bound="true">这个命令是用来设置终端特性的: <BR minmax_bound="true"></P>
<UL minmax_bound="true">
<LI minmax_bound="true">&nbsp; 移动光标
<LI minmax_bound="true">&nbsp; 获得终端信息
<LI minmax_bound="true">&nbsp; 设置前景和背景色
<LI minmax_bound="true">&nbsp; 设置粗体模式
<LI minmax_bound="true">&nbsp; 设置反模式等等 </LI></UL>
<P>举例: <BR minmax_bound="true"></P><PRE class="brush:shell; toolbar: true; auto-links: false;" minmax_bound="true">#!/bin/bash

# clear the screen
tput clear

# Move cursor to screen location X,Y (top left is 0,0)
tput cup 3 15

# Set a foreground colour using ANSI escape
tput setaf 3
echo "XYX Corp LTD."
tput sgr0

tput cup 5 17
# Set reverse video mode
tput rev
echo "M A I N - M E N U"
tput sgr0

tput cup 7 15
echo "1. User Management"

tput cup 8 15
echo "2. Service Management"

tput cup 9 15
echo "3. Process Management"

tput cup 10 15
echo "4. Backup"

# Set bold mode
tput bold
tput cup 12 15
read -p "Enter your choice " choice

tput clear
tput sgr0
tput rc</PRE>
<P>输出: <BR minmax_bound="true"><IMG style="WIDTH: auto; HEIGHT: auto; minmaxWidth: auto; minmaxHeight: auto; maxWidth: 600px" alt="" src="http://static.oschina.net/uploads/img/201202/27224500_rueS.png" minmax_bound="true"> <BR minmax_bound="true"><BR minmax_bound="true"><SPAN style="FONT-WEIGHT: bold" minmax_bound="true">3. setleds 命令</SPAN> <BR minmax_bound="true">这个命令可以让你控制键盘灯,例如打开数字键盘灯: <BR minmax_bound="true"></P><PRE class="brush:shell; toolbar: true; auto-links: false;" minmax_bound="true">setleds -D +num</PRE>
<P>关闭数字键盘灯: <BR minmax_bound="true"></P><PRE class="brush:shell; toolbar: true; auto-links: false;" minmax_bound="true">setleds -D -num</PRE>
<UL minmax_bound="true">
<LI minmax_bound="true">&nbsp; -caps: 清除大写灯
<LI minmax_bound="true">&nbsp; +caps:打开大写灯
<LI minmax_bound="true">&nbsp; -scroll:清除滚动锁
<LI minmax_bound="true">&nbsp; +scroll:打开滚动锁 </LI></UL>
<P><BR minmax_bound="true"><SPAN style="FONT-WEIGHT: bold" minmax_bound="true">4. zenity 命令</SPAN> <BR minmax_bound="true">这个命令可以显示GTK+的**框,然后返回用户的输入。你可以用这个命令在脚本中显示信息,并要求用户输入信息。下面这段代码就是域名的whois查询: <BR minmax_bound="true"></P><PRE class="brush:shell; toolbar: true; auto-links: false;" minmax_bound="true">#!/bin/bash
# Get domain name
_zenity="/usr/bin/zenity"
_out="/tmp/whois.output.$$"
domain=$(${_zenity} --title"Enter domain" \
                --entry --text "Enter the domain you would like to see whois info" )

if [ $? -eq 0 ]
then
# Display a progress dialog while searching whois database
whois $domain| tee &gt;(${_zenity} --width=200 --height=100 \
                      --title="whois" --progress \
                        --pulsate --text="Searching domain info..." \
                                    --auto-kill --auto-close \
                                    --percentage=10) &gt;${_out}

# Display back output
${_zenity} --width=800 --height=600\
         --title "Whois info for $domain" \
         --text-info --filename="${_out}"
else
${_zenity} --error \
         --text="No input provided"
fi</PRE>
<P>输出: <BR minmax_bound="true"><BR minmax_bound="true"><IMG title="zenity: Linux / UNIX display Dialogs Boxes From The Shell Scripts" style="WIDTH: auto; HEIGHT: auto; minmaxWidth: auto; minmaxHeight: auto; maxWidth: 600px" alt="Fig.04: zenity in Action" src="http://static.oschina.net/uploads/img/201202/27224501_SjfC.png" minmax_bound="true"> <BR minmax_bound="true"><BR minmax_bound="true"><SPAN style="FONT-WEIGHT: bold" minmax_bound="true">5. kdialog 命令</SPAN> <BR minmax_bound="true">这个命令和zenity很想,只不过它是为KDE/QT应用准备的。使用方法如下: <BR minmax_bound="true"></P><PRE class="brush:shell; toolbar: true; auto-links: false;" minmax_bound="true">kdialog --dontagain myscript:nofilemsg --msgbox "File: '~/.backup/config' not found."</PRE>
<P>输出 <BR minmax_bound="true"><IMG style="WIDTH: auto; HEIGHT: auto; minmaxWidth: auto; minmaxHeight: auto; maxWidth: 600px" alt="" src="http://static.oschina.net/uploads/img/201202/27224503_CVFj.png" minmax_bound="true"> <BR minmax_bound="true"><BR minmax_bound="true">你可以查看 <A href="http://techbase.kde.org/Development/Tutorials/Shell_Scripting_with_KDE_Dialogs" target=_blank rel=nofollow minmax_bound="true"><FONT color=#0000ff>shell scription with KDE Dialogs</FONT></A> 来获取更多信息 <BR minmax_bound="true"><BR minmax_bound="true"><SPAN style="FONT-WEIGHT: bold" minmax_bound="true">6. Dialog</SPAN> <BR minmax_bound="true">这个命令可以在shell脚本中显示文本组件。它使用了curses和ncurses类库。示例代码: <BR minmax_bound="true"></P><PRE class="brush:shell; toolbar: true; auto-links: false;" minmax_bound="true">&gt;#!/bin/bash
dialog --title "Delete file" \
--backtitle "Linux Shell Script Tutorial Example" \
--yesno "Are you sure you want to permanently delete \"/tmp/foo.txt\"?" 7 60

# Get exit status
# 0 means user hit button.
# 1 means user hit button.
# 255 means user hit key.
response=$?
case $response in
   0) echo "File deleted.";;
   1) echo "File not deleted.";;
   255) echo " key pressed.";;
esac</PRE>
<P><SPAN style="FONT-WEIGHT: bold" minmax_bound="true">7. logger 命令</SPAN> <BR minmax_bound="true">这个命令可以让你写入系统日志例如 /var/log/messages: <BR minmax_bound="true"></P><PRE class="brush:shell; toolbar: true; auto-links: false;" minmax_bound="true">logger "MySQL database backup failed."
tail -f /var/log/messages
logger -t mysqld -p daemon.error "Database Server failed"
tail -f /var/log/syslog</PRE>
<P>输出: <BR minmax_bound="true"><SPAN style="FONT-FAMILY: Courier New" minmax_bound="true">Apr 20 00:11:45 vivek-desktop kernel: CPU0: Temperature/speed normal</SPAN> <BR minmax_bound="true"><SPAN style="FONT-FAMILY: Courier New" minmax_bound="true">Apr 20 00:12:20 vivek-desktop mysqld: Database Server failed</SPAN> <BR minmax_bound="true"><BR minmax_bound="true"><SPAN style="FONT-WEIGHT: bold" minmax_bound="true">8. setterm 命令</SPAN> <BR minmax_bound="true">这个命令可以设置中断的属性。下面的例子是强制屏幕全黑15分钟,并且60分钟后把显示器设为待机状态: <BR minmax_bound="true"></P><PRE class="brush:xml; toolbar: true; auto-links: false;" minmax_bound="true">setterm -blank 15 -powersave powerdown -powerdown 60</PRE>
<P>下面这段命令可以在中断显示加下划线的文字: <BR minmax_bound="true"></P><PRE class="brush:shell; toolbar: true; auto-links: false;" minmax_bound="true">setterm -underline on;
echo "Add Your Important Message Here"
setterm -underline off</PRE>
<P>或者你可以关闭光标: <BR minmax_bound="true"></P><PRE class="brush:shell; toolbar: true; auto-links: false;" minmax_bound="true">setterm -cursor off</PRE>
<P><SPAN style="FONT-WEIGHT: bold" minmax_bound="true">9. smbclient:向 MS-Windows 系统发送消息</SPAN> <BR minmax_bound="true">smbclient可以和 SMB/CIFS服务器通信。它可以向MS-Windows系统的指定用户发送消息: <BR minmax_bound="true"></P><PRE class="brush:shell; toolbar: true; auto-links: false;" minmax_bound="true">smbclient -M WinXPPro &lt;&lt;EOF
Message 1
Message 2
...
..
EOF</PRE>
<P>或者 <BR minmax_bound="true"></P><PRE class="brush:shell; toolbar: true; auto-links: false;" minmax_bound="true">echo "${Message}" | smbclient -M salesguy2</PRE>
<P><SPAN style="FONT-WEIGHT: bold" minmax_bound="true">10. Bash Socket 编程</SPAN> <BR minmax_bound="true">你可以在bash中开启一个socket链接,并且传输数据。Bash有两个特殊的设备文件: <BR minmax_bound="true"></P>
<UL minmax_bound="true">
<LI minmax_bound="true">&nbsp; /dev/tcp/host/port - 如果hostname,和port是合法的话,bash会尝试开启一个TCP连接。
<LI minmax_bound="true">&nbsp; /dev/udp/host/port - 如果hostname和port是合法的话,bash会开启一个UDP连接。 </LI></UL>
<P>&nbsp; <BR minmax_bound="true">你可以利用这个技术来测试一台主机的端口是否是开启的,而不需要使用nmap或者port扫描器: <BR minmax_bound="true"></P><PRE class="brush:shell; toolbar: true; auto-links: false;" minmax_bound="true"># find out if TCP port 25 open or not
(echo &gt;/dev/tcp/localhost/25) &amp;&gt;/dev/null &amp;&amp; echo "TCP port 25 open" || echo "TCP port 25 close"</PRE>
<P>你可以 <A href="http://www.cyberciti.biz/faq/bash-for-loop/" target=_blank rel=nofollow minmax_bound="true"><FONT color=#0000ff>使用循环来查找开着的端口</FONT></A>: <BR minmax_bound="true"></P><PRE class="brush:shell; toolbar: true; auto-links: false;" minmax_bound="true">echo "Scanning TCP ports..."
for p in {1..1023}
do
(echo &gt;/dev/tcp/localhost/$p) &gt;/dev/null 2&gt;&amp;1 &amp;&amp; echo "$p open"
done</PRE>
<P>输出: <BR minmax_bound="true"><SPAN style="FONT-FAMILY: Courier New" minmax_bound="true">Scanning TCP ports...</SPAN> <BR minmax_bound="true"><SPAN style="FONT-FAMILY: Courier New" minmax_bound="true">22 open</SPAN> <BR minmax_bound="true"><SPAN style="FONT-FAMILY: Courier New" minmax_bound="true">53 open</SPAN> <BR minmax_bound="true"><SPAN style="FONT-FAMILY: Courier New" minmax_bound="true">80 open</SPAN> <BR minmax_bound="true"><SPAN style="FONT-FAMILY: Courier New" minmax_bound="true">139 open</SPAN> <BR minmax_bound="true"><SPAN style="FONT-FAMILY: Courier New" minmax_bound="true">445 open</SPAN> <BR minmax_bound="true"><SPAN style="FONT-FAMILY: Courier New" minmax_bound="true">631 open</SPAN> <BR minmax_bound="true"><BR minmax_bound="true">下面的这个例子让你的脚本扮演HTTP客户端: <BR minmax_bound="true"></P><PRE class="brush:shell; toolbar: true; auto-links: false;" minmax_bound="true">#!/bin/bash
exec 3&lt;&gt; /dev/tcp/${1:-www.cyberciti.biz}/80

printf "GET / HTTP/1.0\r\n" &gt;&amp;3
printf "Accept: text/html, text/plain\r\n" &gt;&amp;3
printf "Accept-Language: en\r\n" &gt;&amp;3
printf "User-Agent: nixCraft_BashScript v.%s\r\n" "${BASH_VERSION}"   &gt;&amp;3
printf "\r\n" &gt;&amp;3

while read LINE &lt;&amp;3
do
   # do something on $LINE
   # or send $LINE to grep or awk for grabbing data
   # or simply display back data with echo command
   echo $LINE
done</PRE>
<P><SPAN style="FONT-WEIGHT: bold" minmax_bound="true">关于GUITools和Cronjob</SPAN> <BR minmax_bound="true">如果你使用cronjob来调用你的脚本的话,你要通过“ <SPAN style="FONT-FAMILY: Courier New" minmax_bound="true">export DISPLAY=:0 </SPAN>”命令来设置本地的 display/input 服务。例如调用 /home/vivek/scripts/monitor.stock.sh脚本,它使用了 zenity 工具: <BR minmax_bound="true"></P><PRE class="brush:shell; toolbar: true; auto-links: false;" minmax_bound="true">@hourly DISPLAY=:0.0 /home/vivek/scripts/monitor.stock.sh</PRE>
<P><BR minmax_bound="true">所有的命令你都可以通过“man”来查询详细的使用方式。 <BR minmax_bound="true"></P>
页: [1]
查看完整版本: 10个工具让你的 shell 脚本更强大