利用xargs实现多任务并发
利用xargs实现多任务并发
[4aiur@localhost Temp]$ echo 127.0.0.1 192.168.2.50 192.168.10.108 | xargs -n1 -P2 -I % ping -c 3 %
PING 127.0.0.1 (127.0.0.1): 56 data bytes
64 bytes from 127.0.0.1: icmp_seq=0 ttl=64 time=0.048 ms
PING 192.168.2.50 (192.168.2.50): 56 data bytes
64 bytes from 192.168.2.50: icmp_seq=0 ttl=64 time=0.026 ms
64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.091 ms
64 bytes from 192.168.2.50: icmp_seq=1 ttl=64 time=0.022 ms
64 bytes from 192.168.2.50: icmp_seq=2 ttl=64 time=0.039 ms
64 bytes from 127.0.0.1: icmp_seq=2 ttl=64 time=0.077 ms
--- 192.168.2.50 ping statistics ---
3 packets transmitted, 3 packets received, 0.0% packet loss
round-trip min/avg/max/stddev = 0.022/0.029/0.039/0.007 ms
--- 127.0.0.1 ping statistics ---
3 packets transmitted, 3 packets received, 0.0% packet loss
round-trip min/avg/max/stddev = 0.048/0.072/0.091/0.018 ms
PING 192.168.10.108 (192.168.10.108): 56 data bytes
64 bytes from 192.168.10.108: icmp_seq=0 ttl=63 time=6.244 ms
64 bytes from 192.168.10.108: icmp_seq=1 ttl=63 time=1.392 ms
64 bytes from 192.168.10.108: icmp_seq=2 ttl=63 time=1.062 ms
--- 192.168.10.108 ping statistics ---
3 packets transmitted, 3 packets received, 0.0% packet loss
round-trip min/avg/max/stddev = 1.062/2.899/6.244/2.369 ms
[4aiur@localhost Temp]$
使用xargs扫描同网段内存活的设备
[4aiur@localhost Temp]$ for ((x=1; x/dev/null | awk /ttl/'{print $4}'
Show File System Hierarchy
Show File System Hierarchy
$ man hier
Curious about differences between /bin, /usr/bin, and /usr/local/bin? What should be in the /sbin dir? Try this command to find out.
Tested against Red Hat & OS X
目录名大小写转换
使用find把目录名修改为大写——-
find . -type d | sort -r |\
while read name
do
echo "mv $name ${name%/*}/echo ${name##*/} | tr '[:lower:]' '[:upper:]'
"
mv $name ${name%/*}/echo ${name##*/} | tr '[:lower:]' '[:upper:]'
done
使用递归方式把目录名修改为小写
#!/bin/bash
# set -x
tolower () {
ls | while read name
do
if [ -d $name ] ; then
new_name=echo $name | tr "[:upper:]" "[:lower:]"
if [[ $name != $new_name ]] ; then
mv $name $new_name
fi
cd $new_name
tolower
fi
done
}
tolower
定制MacOSX ls颜色
定制MacOSX ls颜色
习惯了linux的ls颜色,不适应MacOSX终端中的ls颜色修改了ls颜色使用的默认环境变量,跟linux有些接近了。
首先增加一个
aliasalias ll='ls -lG'
之后修改LSCOLORS环境变量
export LSCOLORS=ExGxCxDxCxEgEdAbAgAcAd
LSCOLORS from man ls:
LSCOLORS The value of this variable describes what color to use for which attribute when colors are enabled with CLICOLOR. This string is a concatenation of pairs of the format fb, where f is the fore- ground color and b is the background color.
The default is "exfxcxdxbxegedabagacad", i.e. blue foreground and default background for regular directories, black foreground and red background for setuid executables, etc.
linux颜色可以细化到后缀名,这一点做的很不错,下面是Linux的ls颜色控制变量
[root@localhost ~]# env | grep LS_COLORS
LS_COLORS=no=00:fi=00:di=01;34:ln=01;36:pi=40;33:so=01;35:bd=40;33;01:cd=40;33;01:or=01;05;37;41:mi=01;05;37;41:ex=01;32:*.cmd=01;32:*.exe=01;32:*.com=01;32:*.btm=01;32:*.bat=01;32:*.sh=01;32:*.csh=01;32:*.tar=01;31:*.tgz=01;31:*.arj=01;31:*.taz=01;31:*.lzh=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:*.gz=01;31:*.bz2=01;31:*.bz=01;31:*.tz=01;31:*.rpm=01;31:*.cpio=01;31:*.jpg=01;35:*.gif=01;35:*.bmp=01;35:*.xbm=01;35:*.xpm=01;35:*.png=01;35:*.tif=01;35:
使用多种方法打印50个连续的横杠
使用多种方法打印50个连续的横杠
python -c 'print "-" * 50'
perl -le'print"-"x50'
awk 'BEGIN{while (a++
perl发送邮件注意事项
perl发送邮件注意事项
1) 程序手工执行可以发送mail,而把程序加入到crontab后无法执行的解决方法在crontab中执行脚本前,加入. /etc/profile
;
0 * * * * * . /etc/profile ; /usr/bin/perl /app_path/mail.pl >/dev/null 2>&1
2) smtp服务器需要认证,而Net::SMTP的auth方法不起作用的解决方法Net::SMTP的auth方法依赖Authen::SASL这个模块,
可以用perl -e 'use Authen::SASL'
试试,看下是否报错,如果报错的话需要安装Authen::SASL模块。
安装方法:
# perl -MCPAN -e 'install Authen::SASL'
顺便提一下,用抓包工具看的话smtp认证过程账户和密码部分是乱码,其实可以转换出来,乱码的unicode是base64,使用python的decode方法可以转换出明文。
>>> s='string'
>>> s.encode('base64')
'c3RyaW5n\n'
>>> u=u'c3RyaW5n\n'
>>> u.decode('base64')
'string'
>>>
unicode的问题在RFC 2554中有所描述。
附代码:
#!/usr/bin/perl
use strict;
use warnings;
use Net::SMTP;
my $mailhost='mail.example.com'; #Mail Server .
my $mailfrom='foo@example.com'; #Mail from
my @mailto=('bar@example.com'); #Receive list
my $subject='subject: Topic';
my $text='content';
my $smtp=Net::SMTP->new($mailhost,Timeout=>120,Debug=>1) or die "Error.\n";
$smtp->auth('foo','passwd'); #username and password are needed
foreach my $mailto(@mailto) {
$smtp->mail($mailfrom);
$smtp->to($mailto);
$smtp->data();
$smtp->datasend("To: $mailto\n");
$smtp->datasend("From:$mailfrom\n");
$smtp->datasend("Subject: $subject\n");
$smtp->datasend("\n");
#Message
$smtp->datasend("$text\n\n");
$smtp->dataend();
}
$smtp->quit;
摘自www.commandlinefu.com的一些命令
摘自www.commandlinefu.com的一些命令
# change to the previous working directory
$ cd -
# quickly backup or copy a file with bash
$ cp filename{,.bak}
# mtr, better than traceroute and ping combined
# mtr combines the functionality of the traceroute and ping programs in a single network diagnostic tool.
# As mtr starts, it investigates the network connection between the host mtr runs on and HOSTNAME. by sending packets with purposly low TTLs. It continues to send packets with low TTL, noting the response time of the intervening routers. This allows mtr to print the response percentage and response times of the internet route to HOSTNAME. A sudden increase in packetloss or response time is often an indication of a bad (or simply over‐loaded) link.
$ mtr google.com
# Salvage a borked terminal
# If you bork your terminal by sending binary data to STDOUT or similar, you can get your terminal back using this command rather than killing and restarting the session. Note that you often won't be able to see the characters as you type them.
$ reset
# Empty a file
# For when you want to flush all content from a file without removing it (hat-tip to Marc Kilgus).
$ > file.txt
使用Python解析Excel
使用Python解析Excel
解析Excel文件常用的module有pyExcelerator与xlrd,目前这两个模块只能支持excel2004以下版本生成的文件。
我写了一个从excel文件读取工资,并给员工发送工资条邮件的程序放在了github上,里面有一些解析excel文件细节的代码,有兴趣的同学可以下回去看看。
代码存放地址: https://github.com/mlf4aiur/payslip
pyExcelerator范例
查看pyExcelerator的tools中xls2txt.py了解到pyExcelerator使用parse_xls方法解析excel文件。
调用parse_xls方法后,生成的结果为list,list的结构示例如下:
[(u'Sheet1', {(0, 0): 111, (0, 1): 112, (1, 0): 121, (1, 1): 122}),
(u'Sheet2', {(0, 0): 211, (0, 1): 212, (1, 0): 221, (1, 1): 222}),
(u'Sheet3', {})]
安装pyExcelerator
sudo pip install pyExcelerator
localhost:Share 4aiur$ ipython
Leopard libedit detected.
Python 2.6.1 (r261:67515, Jul 7 2009, 23:51:51)
Type "copyright", "credits" or "license" for more information.
IPython 0.10 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object'. ?object also works, ?? prints more.
In [1]: from pyExcelerator import *
In [2]: l = parse_xls('foo.xls')
In [3]: l
Out[3]:
[(u'Sheet1', {(0, 0): 111, (0, 1): 112, (1, 0): 121, (1, 1): 122}),
(u'Sheet2', {(0, 0): 211, (0, 1): 212, (1, 0): 221, (1, 1): 222}),
(u'Sheet3', {})]
In [4]:
了解到解析的数据结构后,既可根据自己的需求进行数据分析。
localhost:Share 4aiur$ cat > foo.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from pyExcelerator import *
l = parse_xls('foo.xls', 'utf-8') # parse_xls(arg) -- default encoding
for sheet_name, values in l:
print 'Sheet = "%s"' % sheet_name.encode('utf-8', 'backslashreplace')
for row_idx, col_idx in sorted(values.keys()):
v = values[(row_idx, col_idx)]
if isinstance(v, unicode):
v = v.encode('utf-8', 'backslashreplace')
else:
v = str(v)
print '(%d, %d) =' % (row_idx, col_idx), v
localhost:Share 4aiur$ chmod +x foo.py
localhost:Share 4aiur$ ./foo.py
Sheet = "Sheet1"
(0, 0) = 111
(0, 1) = 112
(1, 0) = 121
(1, 1) = 122
Sheet = "Sheet2"
(0, 0) = 211
(0, 1) = 212
(1, 0) = 221
(1, 1) = 222
Sheet = "Sheet3"
localhost:Share 4aiur$
安装xlrd
sudo pip install xlrd
xlrd范例
localhost:Share 4aiur$ cat > bar.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from xlrd import open_workbook
workbook = open_workbook("foo.xls")
for sheet in workbook.sheets():
print("Sheet = \"%s\"" % (sheet.name))
for row_idx in range(sheet.nrows):
col_idx = 0
for row in sheet.row(row_idx):
print("(%d, %d) = %s" % (row_idx, col_idx, str(row.value)))
col_idx += 1
[mlf4aiur@4aiur excel]$ chmod +x bar.py
[mlf4aiur@4aiur excel]$ ./bar.py
Sheet = "Sheet1"
(0, 0) = 111.0
(0, 1) = 112.0
(1, 0) = 121.0
(1, 1) = 122.0
Sheet = "Sheet2"
(0, 0) = 211.0
(0, 1) = 212.0
(1, 0) = 221.0
(1, 1) = 222.0
Sheet = "Sheet3"
localhost:Share 4aiur$
FreeBSD内存文件系统
FreeBSD内存文件系统
[4aiur@FreeBSD ~/memeory_fs]$ sudo mount -t tmpfs -o size=1m tmpfs tmpfs/
[4aiur@FreeBSD ~/memeory_fs]$ sudo mdmfs -s 1m md mfs
[4aiur@FreeBSD ~/memeory_fs]$ df -h tmpfs/ mfs/
Filesystem Size Used Avail Capacity Mounted on
tmpfs 648M 4.0K 648M 0% /usr/home/4aiur/memeory_fs/tmpfs
/dev/md5 846K 4.0K 776K 1% /usr/home/4aiur/memeory_fs/mfs
安装好FreeBSD后的一些基础配置修改
安装好FreeBSD后的一些基础配置修改
- 配置alias
[root@FreeBSD ~]# alias
alias cp='cp -i'
alias grep='grep --color'
alias ll='ls -lG'
alias mv='mv -i'
alias rm='rm -i'
alias vi='vim'
[root@FreeBSD ~]#
- 安装sudo
[root@FreeBSD ~]# cd /usr/ports/security/sudo [root@FreeBSD /usr/ports/security/sudo]# make [root@FreeBSD /usr/ports/security/sudo]# make install [root@FreeBSD /usr/ports/security/sudo]# make clean
- 修改man的环境变量,使用less来查看手册
FreeBSD -P pager Specify which pager to use. By default, man uses ``more -s''. This option overrides the PAGER environment variable.
Linux
-P pager
Specify which pager to use. This option overrides the MANPAGER environment
variable, which in turn overrides the PAGER variable. By default, man uses
/usr/bin/less -is.
[4aiur@FreeBSD ~]$ tail -1 /etc/profile
PAGER="less -is"; export PAGER
- 更新locate数据库
[4aiur@FreeBSD ~]$ sudo /etc/periodic/weekly/310.locate Password: Rebuilding locate database: # 让vim支持彩色 [4aiur@FreeBSD ~]$ locate vimrc_example.vim /usr/local/share/vim/vim72/gvimrc_example.vim /usr/local/share/vim/vim72/vimrc_example.vim [4aiur@FreeBSD ~]$ cp -pf /usr/local/share/vim/vim72/vimrc_example.vim ~/.vim