单台登录:

import paramiko

ssh = paramiko.SSHClient()
key = paramiko.AutoAddPolicy()
ssh.set_missing_host_key_policy(key)
ssh.connect('127.0.0.1', 22, 'user', 'passwd' ,timeout=5)
stdin, stdout, stderr = ssh.exec_command('ls -l')

for i in stdout.readlines():
    print(i)

for i in stdout.readlines():
print(i)
这两段是输出屏幕命令。

如果已经添加免秘钥登录,把passwd留空即可。
下面是多台服务器登录:

#!/usr/bin/python
# -*- coding: utf-8 -*-

import time,shutil,os
import paramiko

appname = "root"
ssh = paramiko.SSHClient()
key = paramiko.AutoAddPolicy()
ssh.set_missing_host_key_policy(key)

uptime = time.strftime("%Y%m%d")
warpath="/var/tmp/cslc/aw/update/"+uptime+"/aw.war"

def update_all_aw():
    for line in open("awip.conf"):
        print line
        ssh.connect(line, 22, appname, '' ,timeout=5)
        stdin, stdout, stderr = ssh.exec_command('python /var/tmp/awpy/createcopy.py')
        for i in stdout.readlines():
            print(i)
        print("+++++++下一个++++++")
    return

awip.conf文件防止多台IP地址即可。


转载于:https://blog.51cto.com/weiruoyu/2140919