练习
shell的总结。
函数库中的部门代码
远程执行命令,传送文件,或者文件同步。自动输入密码。需要expect的支持。
常在linux下操作的话,这些肯定是容易用到的。绿色的。不用使用rexec和rsync。
更方便。存入文件,source 一下即可。
用法
登陆 se ip/主机名
gexec ip/主机名 ip/主机名 'cmd'
gsyn ip/主机名 ip/主机名 文件或者目录的位置
#ssh login or execute comd
se()
{
[[ -z $1 ]]&& echo se se3 who
[[ "$1" > = ]]&&host=$1.xxxxx.com||host=119.42.239.$1
ssh $host $2
}
#execute cmd in group, example, gexec se2 se3 35 'ps -x'
gexec()
{
for i in ${*:1:$#-1};
do
[[ $i > = ]]&&host=$i.xxxxx.com||host=119.42.239.$i
ex ssh $host ${*:$#}
done
}
#distribute directorys or files, example, gsyn se2 se3 35 ~/dir
gsyn()
{
cd `dirname ${*:$#}`
cd $OLDPWD
for i in ${*:1:$#-1};
do
[[ $i > = ]]&&host=$i.xxxxx.com||host=119.42.239.$i
ex scp -r ${*:$#} $host:$OLDPWD/
done
}
gscp()
{
for i in ${*:1:$#-2};
do
[[ $i > = ]]&&host=$i.xxxxx.com||host=119.42.239.$i
ex scp -r ${*:$#-1:1} $host:~/${*:$#}
done
}
check()
{
if [ $# = 0 ]
then
echo "check ip|file|config string"
return
fi
case $* in
file)
find . -name "*.*" | xargs grep /home/.* | awk -F/home '{print $1":/home"$2}'| sed -e 's/[^a-zA-Z0-9]*$//'|awk -F: '{gsub("\""," ");system("if [ ! -e "$3" ] ;then echo \""$0"\" ;fi;")}'
echo "WARNNING, those files wasn't exist,please check "
;;
ip)
find . -name "*.*" |xargs grep "[1-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}"
;;
*)
ls|xargs grep "$1"
temp=`ls|xargs grep "$1"|sed -n $3p|uniq`
if [[ -n $2 ]]
then
temp=`echo $temp|awk -F= '{print $2}'`
[[ $temp = "$2" ]]&&print OK||print WARNNING
fi
;;
esac
}
#set for load seven.sh when login
autostart()
{
sed -i -e '/source seven/d' ~/.bashrc
echo "source seven.sh ||: ; source /home/huangysh/seven.sh ||:" >>~/.bashrc && print "OK, you already have the power of qa shell functions, good luck"
}
#the core of whole script. used for automation
ex()
{
expect -c "set timeout -1;spawn $*; while {1} {expect eof {puts \nEX_OK\n;exit} Password {puts Password;send ${PW}\n} (yes/no)? {puts EX_YES;send yes\n} timeout {puts EX_TIMEOUT;interact} }"
}