您的位置:首页 > 编程语言 > Python开发

python 利用pexpect进行多机远程命令执行

2014-09-09 11:46 701 查看
在安装之前,确认你的机器安装了python,和easy_install.通常python是自动安装的,如果没有安装easy_install,那么wget -q http://peak.telecommunity.com/dist/ez_setup.py获取一下python ez_setup.pypexpect是python一个模块,可以通过:easy_install pexpect 来安装。

这里主要是用pexpect执行ssh,查看远程uptime和df -h看硬盘状况。 #ssh_cmd.py
#coding:utf-8

import pexpect

def ssh_cmd(ip, user, passwd, cmd):
ssh = pexpect.spawn('ssh %s@%s "%s"' % (user, ip, cmd))
r = ''
try:
i = ssh.expect(['password: ', 'continue connecting (yes/no)?'])
if i == 0 :
ssh.sendline(passwd)
elif i == 1:
ssh.sendline('yes')
except pexpect.EOF:
ssh.close()
else:
r = ssh.read()
ssh.expect(pexpect.EOF)
ssh.close()
return r

hosts = '''
192.168.0.12:smallfish:1234:df -h,uptime
192.168.0.13:smallfish:1234:df -h,uptime
'''

for host in hosts.split("/n"):
if host:
ip, user, passwd, cmds = host.split(":")
for cmd in cmds.split(","):
print "-- %s run:%s --" % (ip, cmd)
print ssh_cmd(ip, user, passwd, cmd)

1、查询30天前的日志文件并删除
[plain] view plaincopy




find /g2ihslog/xmlgen/ -mtime +30 –exec rm {} \;

通过文件名中包含的日期查询:

[plain] view plaincopy




#文件中包含的日期字符串

DEL_DAY=`date -I -d '-30 day'`"-00"

#日志文件夹

logdir="/g2ihslog/*/"

#遍历文件夹下的文件

for n in `ls $logdir` ; do

#取出文件名中的日期

file_dt=`echo $n | awk -F "." '{print $3}'`

#若日期为30天前,则删除

if [[ -n "$file_dt" && "$file_dt" < "$DEL_DAY" ]];then

rm -f $n

fi;

done

2、读取文件,对每一行进行处理

linux shell逐行处理文本的12种方法
方法1、cat $filename|while read line

例如:
[plain] view plaincopy




function while_read_line

{ cat $filename|while read line

do

echo "$line"

: #这行什么都不做,返回值0 ,在此处对变量进行的操作不起作用,出了while循环就失效

done

}

方法2:while read $filename from bottom #建议采用此方法读取

例如:
[plain] view plaincopy




function while_read_line_bottom

{

while read line

do

echo "$line"

:

done < $filename

}

方法3:while_line_line_bottom
例如:
[plain] view plaincopy




function while_line_line_bottom

{

while line line #用line命令替换read

do

echo "$line"

:

done < $filename

}

方法4:cat $filename|while line=`line`
例如:
[plain] view plaincopy




function cat_while_line_line

{

cat $filename | while line=`line`

do

echo "$line"

:

done

}

方法5:cat $filename |while line line
例如:
[plain] view plaincopy




function while_line_LINE

{

cat $filename |while line line

do

echo "$line"

:

done

}

方法6:while line=`line`from the bottom
例如:
[plain] view plaincopy




function while_line_line_bottom

{

while line=`line`

do

echo "$line"

:

done < $filename

}

方法7:cat $filename |while line=$(line)
例如:
[plain] view plaincopy




function while_line_line_cm

{

cat $filename |while line=$(line)

do

echo "$line"

:

done

}

方法8:while line=$(line)from the bottom
例如:
[plain] view plaincopy




function while_line_line_bottom_cm

{

while line=$(line)

do

echo "$line"

done<$filename

}

方法9:while read line
例如:
[plain] view plaincopy




function while_read_line_fd

{

exec 3<&0 #将所有内容重定向到新文件描述符3来关闭文件描述符0

exec 0<$filename #标准输入文件描述符为0,标准输出文件描述符为1,标准错误为2.

while read line #3以后就可以配给普通文件。

do

echo "$line"

done

exec 0<&3

}

方法10:while line=`line`
例如:
[plain] view plaincopy




function while_line_line_fd

{

exec 3<&0

exec 0<$filename

while line=`line`

do

echo "$line"

done

exec 0<&3

}

方法11:while line=$(line)
例如:
[plain] view plaincopy




function while_line_line_cm_fd

{

exec 3<&0

exec 0<$filename

while line=$(line)

do

print "$line"

done

exec 0<&3

}

方法12:while line line
例如:
[plain] view plaincopy




function while_line_line_fd

{

exec 3<&0

exec 0<$filename

while line line

do

echo " $line"

done

exec 0<&3

}

3、查看进程ID

[plain] view plaincopy




#-v 表示排除 -i表示忽略大小写

ps -ef | grep -v grep | grep -i ihs-${name} | awk '{print $2}'
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息