用python的paramiko批量杀掉jps进程后重启hadoop&spark集群

hadoop集群关闭异常可能会在集群中残留jps下的进程影响后面的重启,下面通过python的paramiko模块批量杀死影响重启的进程

 

import paramiko
#建立连接,这里假定集群中的hadoop用户都用相同的账号密码
def connect(host):
'this is use the paramiko connect the host,return conn'
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
try:

ssh.connect(host,username='zkpk',password='zkpk',allow_agent=True)
return ssh
except:
return None

#执行命令
def exec_commands(conn,cmd):
'this is use the conn to excute the cmd and return the results of excute the command'
stdin,stdout,stderr = conn.exec_command(cmd)
results=stdout.read()
return results



if __name__ == '__main__':
#集群主机ip,逐个登陆主机执行jps命令查找进程id,然后干掉,如果集群主机较多可以通过并行的方式提高效率
for ip in ['192.168.134.174','192.168.134.175','192.168.134.176']:
a=exec_commands(connect(ip), '/usr/java/jdk1.7.0_71/bin/jps')
ps=[]
#字符串分析,提取进程id
ls=a.split('\n')
for l in ls:
if len(l.split(' ')[0])>=1:
ps.append(l.split(' ')[0])

for psd in ps:
a = exec_commands(connect(ip), 'kill -9 %s' % psd)
print a



代码比较粗糙,当然完全可以用更高封装的自动运维工具fabric做上述工作
posted @ 2017-03-16 09:52  嘻哈怒叱  阅读(839)  评论(0编辑  收藏  举报