HICK 者,乡巴佬也

shell/命令行下的光标移动等操作快捷键

Posted in: Linux/Unix工具 — Hick @ 2008/12/27 14:54:15 Comments (1)

shell 命令行下不少键盘操作跟 Emacs 是类似的,可能不能说是从 Emacs 借鉴过来的,这些快捷的使用,谁早谁晚还不好考证,不过思路是一致的。之前某次突然来劲试了 C-a C-e 等,刚搜索并实践了若干操作,在 SecureCRT 登录 suse 的环境验证了,整理如下(不一定是所有 shell 都支持):

(注意下面的”前”都是指”左”或”上”,”后”是”右”或”下”)

C-r 查找历史执行命令,很便捷的调用历史命令的方式,输入历史命令关键字,就会及时显示匹配命令,enter 即可执行
C-p 前一条指令
C-n 后一条指令
C-c 终止已经运行的命令(针对还没有运行完的命令和脚本,实际为向相关进程发送中断信号)或者取消已经输入的命令
C-o/C-j/C-m 执行当前行输入的命令,跟 enter 类似
C-l 清屏,clear 命令

C-a 移动光标到行首
C-e 移动光标到行尾

C-t 交换光标前俩字符的位置
C-h 往后删除一字符
C-d 往前删除一字符
C-b 往后移动一个字符
C-f 往前移动一个字符

下面几个操作原理估计类似 emacs 的 yank 操作,可以理解为一套独立的粘贴板机制:
C-w 剪切前一个单词(空格间隔的字符串单元)
C-u 剪切到行首
C-k 剪切到行尾
C-y 粘贴剪切

linux 下的文本”图形”界面工具: dialog

Posted in: Linux/Unix工具 — Hick @ 2008/12/02 23:42:40 Comments (0)

回头来看 Beginning Linux Programming 的 shell 部分还是有不少收获,其中一个 dialog 的工具,就激发了我写个简单的选择 ip 的小脚本的想法: 把 ip 分类保存成文件,每个文件中的一行(unix 风格换行符)为一个 ip,然后把下面的脚本的 host_path 值修改成这些 ip 文件所在目录—该目录下不要有其他文件即可。

# list all the host files
host_path=/home/hick/hosts/
# compose the menu tag
menu_tag=''
for file in $(ls $host_path); do
menu_tag="$menu_tag $file $file"
done
# display the host files
dialog --title "QzoneCity host list" --menu "please select the type" 30 30 15 $menu_tag 2>/tmp/_hosttype.txt
# get the select host file for ip list
host_file=$(cat /tmp/_hosttype.txt)

# list all ip of the select host type
menu_tag=''
loop=1
for ip in $(cat ${host_path}${host_file} | tr '\n' ' '); do
menu_tag="$menu_tag $ip $loop"
loop=$(($loop+1))
done
dialog --title "ip list of == $host_file ==" --menu "please select the ip" 30 30 15 $menu_tag 2>/tmp/_ip.txt
select_ip=$(cat /tmp/_ip.txt)

# login the ip by ssh
ssh $select_ip user_00 -q

效果图:dialog

其中有一个不太常见的小技巧: cat file.txt | tr ‘\n’ ‘ ‘ 用来把文件中的换行符替换成空格。

不过 dialog 这个命令并不是所有 linux 版本都默认安装的,其官方主页为 http://hightek.org/dialog/

(more…)

gnuplot

Posted in: Linux/Unix工具 — Hick @ 2008/11/24 21:46:38 Comments (0)

gnuplot 能够按照指定的数据生成二维图或三维图,提供丰富的可选图表样式。当然对复杂的三维图,建议使用专业 3D 绘图软件。这是一款非一般意义上的开源的”公开源代码”的免费软件—copyright reserved。有 windows 版,官方网站 http://www.gnuplot.info 。生成的图片参见上文 利用 vmstat+gnuplot+python脚本生成CPU和内存使用率图表

安装好以后运行 gnuplot 即可进入交互模式。然后用下面的简单命令可以在当前目录创建一个 sin(x) 的图表文件 sin.gif(注意:下面的输出代码中的某些英文引号可能被替换成了中文引号):

set terminal gif
set output 'sin.gif'
plot sin(x)

再次换一张图片需要先 reset ,下面从 tsv 文件中获取信息生成图表:

reset
set terminal gif
set output 'hick.gif’
set xdata time # The x axis data is time
set timefmt “%d-%b-%y” # The dates in the file look like 10-Jun-04
set format x “%b %d” # On the x-axis, we want tics like Jun 10
plot [”31-May-04″:”11-Jun-04″] ‘hick.dat’ using 1:2 with linespoints

hick.dat 文件如下:

#Date Open High Low Close
1-Jun-04 88.00 88.48 87.30 88.12
2-Jun-04 88.64 88.64 87.89 87.98
3-Jun-04 87.85 88.10 87.35 87.35
4-Jun-04 87.95 88.49 87.50 87.56
7-Jun-04 88.75 88.99 88.01 88.64
8-Jun-04 88.64 90.50 88.40 90.04
9-Jun-04 89.90 90.55 89.81 90.09
10-Jun-04 90.23 90.75 89.89 90.46

plot 可以通过逗号分割的定义,生成对比曲线:

reset
set terminal gif
set output 'hick.gif'
plot "hick.dat" using 1:2 with lines title "open", \
"hick.dat" using 1:3 with lines title "high", \
"hick.dat" using 1:4 with lines title "low", \
"hick.dat" using 1:5 with lines title "close"

每次在交互模式下输入这么多命令比较麻烦,可以把上述”脚本”保存到 hick.p 文件,下次需要使用的时候,进入 gnuplot 以后,再 load “hick.p” 即可生成图表图片了。下面的例子增加了若干可选参数的设置:

# xy 轴的说明
set xlabel "Date"
set ylabel "Stock Value"
# 输出图片类型: png/gif/jpeg 等
set terminal png
# 输出图片名
set output 'hick.png'
# 输出图片大小为默认值的:宽 * 1.2, 高 * 0.5
set size 1.2, 0.5
# 设置网格
set grid
# 图例说明的位置: top/bottom left/right outside
set key top left
# 设置 x 轴为时间格式
set xdata time
# 输入的时间格式(%d-%d-%y 表示输入时间类似 1-Jun-04 这样的)
set timefmt "%d-%b-%y"
# 输出的时间格式(%m 为月份的数字表示)
set format x "%m/%d"
# 渲染图片
plot "hick.dat" using 1:2 with linespoints title "open", \
"hick.dat" using 1:3 with linespoints title "high", \
"hick.dat" using 1:4 with linespoints title "low", \
"hick.dat" using 1:5 with linespoints title "close"

而更简单的一种方式,为上面的”脚本”指定解释器路径,即可作为一般的 shell 脚本来运行。

gnuplot 允许数据文件是 tab 和空格分割的文本数据,以 # 号开头的行将会被忽略。

下面是 gnuplot 的几个功能说明,更多具体的信息参考官方文档 http://www.gnuplot.info/documentation.html

. 4.0 版支持数据文件和脚本文件合在一起写
. 4.3 版才支持 utf8 编码
. set datafile 可以设置数据文件的格式,比如指定分割符、注释
. 支持对数据进行函数运算,除了常用的数学函数,还可以自己定义函数
. gnuplot 有 windows 版,不通过 cygwin 也可以在 windows 下运行

利用 vmstat+gnuplot+python脚本生成CPU和内存使用率图表

Posted in: Linux/Unix工具, Python — Hick @ 2008/11/02 16:07:07 Comments (1)

最近接触了 gnuplot 这个不错的工具,虽然生成图表的功能可以通过 excel 容易的实现,但是在 linux 命令行下可以跟其他工具配合,轻松实现”自动化”。vmstat 可以不断的显示 linux 系统的 CPU/内存以及 io 的使用情况: vmstat 2 1000 表示每俩秒显示一次系统各种资源消耗情况,一共执行 1000 次。于是做了个比较简单丑陋的 python 脚本,格式化 vmstat 的输出数据并利用 gnuplot 生成图表—脚本中实际上是调用测试 web server 压力的工具 autobench 对 gnuplot 的一个再封装脚本 bench2png,生成完以后用 sz 命令下载到本地,然后删除服务器上的文件。gnuplot 的用法后面再整理下 。

#!/usr/bin/python
# -*- coding: utf-8 -*-
# 处理规则: 目录下文件名包含 vmstat 的 .txt 或者 .log 后缀文件,
 
# 导入正则
import re
# 字符操作
import string
# 调用系统命令
import os
import sys
 
# 确认目录
path = r'./'
if path[-1:] != '/':
    
path = path + '/'
 
### 遍历当前目录下的文件
 
fileList = os.listdir(path)
for file in fileList:
    
# 文件名中没有 vmstat 则不处理
    
if file.find('vmstat') < 0:
        
continue;
    
# 只处理后缀 .txt 或者 .log 的文件
    
if file[-4:] != '.txt' and file[-4:] != '.log':
        
continue
    
# 如果对应的 tsv 文件不存在, 则生成
    
tsvFile = file[0:-4] + '.tsv'
    
if not os.path.exists(path + tsvFile):
        
fileHandle = open(path + file);
        
# 生成 tsv 文件
        
fileContent = ''
        
i = 0
        
header = ''
        
for line in fileHandle.readlines():
        
# 保证每行开始都一样:至少有一个空格
        
line = ' ' + line
            
# 如果该行存在 procs 则不处理
            
if line.find('procs') > -1:
                
continue
            
# 前 10 行出现 cache 字符表明是列说明,则保留
            
if line.find('cache') > -1 and i > 10:
                
continue
            
# 替换字符
            
line = re.sub(r"\n +", "\n", line) # 替换各种换行回车为 \n ,并去掉行尾空格
            
line = re.sub(r" +", "\t", line) # 替换连续空格为 tab
            
# 加一列数字
            
fileContent += str(i) + line
            
i = i + 1
        
# 写入 tsv 文件
        
tsvFileHandle = open(path + tsvFile, 'w')
        
tsvFileHandle.write(fileContent)
        
tsvFileHandle.close()
        
# 关闭原文件
        
fileHandle.close()
 
    
# 如果对应 png 文件没有生成,则生成
    
memPngFile = file[0:-4] + '.mem.png'
    
cpuPngFile = file[0:-4] + '.cpu.png'
    
if not os.path.exists(path + memPngFile):
        
os.system("bench2png " + path + tsvFile + " " + path + memPngFile + " 4 5 6 7 <<EOT " + file[0:-4]+ "\n\rEOT\n")
        
os.system("bench2png " + path + tsvFile + " " + path + cpuPngFile + " 14 15 16 17 <<EOT " + file[0:-4]+ "\n\rEOT\n")
    
# 下载并删除
    
os.system("sz " + path + file[0:-4] + "*")
    
os.system("rm " + path + file[0:-4] + "*")
sys.exit()

有一个值得改进的机制就是最好能够通过 python 脚本内部调用 vmstat 而不是去分析 vmstat 的输出结果,不过作为我第二个实用 python 脚本,感觉还是不错了 :) 下面是生成图效果:

gnuplot

windows下启动cygwin的cron/crontab

Posted in: Linux/Unix工具 — Hick @ 2008/11/01 21:14:28 Comments (0)

windows 的计划任务管理 taskschedule 实在太不合我口味了,老早以前就搜过 windows 下的 cron 实现,找到一个运行不怎么稳定,莫名其妙的崩溃。老早以前用 cygwin 的时候,以 linux 的思维方式, crontab -e 制定完任务以后,发现怎么也执行不了。

前阵子又进一步了解了 cygwin ,原来还需要启动一个服务才能够搞定 cron 。cygwin 下不仅仅是 cron 需要以 windows 服务的方式启动,消息队列、共享内存以及信号量等进程间通信方式也需要以 windows 服务方式启动 cygserver 以后才可用。除了 cron 包,还需要确认 cygrnsrv 包已经安装:

# 安装 cron 服务
cygrunsrv -I cron -p /usr/sbin/cron -a -D
# 启动服务: 也可以用 windows 的启动方式 net start cron
cygrunsrv -S cron

# 启动 cygserver
cygrunsrv -I cygserver -p /usr/sbin/cygserver -e "CYGWIN=server"

启动 cron 以后,即使关闭 cygwin 命令窗口,crond 还是会继续运行 :) ,而且默认是自动启动的服务,重启机器以后依然运行—不需要开 cygwin 窗口。

Next Page »