使用python ssh 执行远程命令

python ssh 模块: paramiko

CentOS 组件安装:

yum install -y python-devel
yum install -y python-pip
yum install -y libffi-devel
pip install –upgrade pip
pip install paramiko
pip install paramiko –upgrade

为执行此脚本的用户配置好目标机器的ssh免登录,编写python 脚本:

import paramiko, base64
paramiko.common.logging.basicConfig(level=paramiko.common.ERROR)
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(‘your server ip’,username=’login name’)

#  此命令查询目标机器上storm的6704执行进程的ID
cmd=”ps -ef | grep -v grep | grep 6704 | grep org.apache.storm.LogWriter | awk  -F\” \” ‘{print $2}’ ”
stdin, stdout, stderr = client.exec_command(cmd)
for line in stdout:
print ‘storm executor pid for xxx 6704 : ‘ + line.strip(‘\n’)
client.close()

 

 

仅有1条评论

  1. 韭菜 / 本文作者

    看各个状态的连接数
    netstat -n | awk ‘/^tcp/ {++S[$NF]} END {for(a in S) print a, S[a]}’
    ss -ant | awk ‘NR>1 {++s[$1]} END {for(k in s) print k,s[k]}’

评论已关闭。