我们可以使用paramiko来执行多个shell的命令

时间:2017-05-05 08:30:08

标签: python paramiko

hosts=['10.101.x.x'] username='root' password=''


from datetime import datetime date = datetime.now() dates
=date.strftime('%d%b%Y') print dates CheckStr = "Log-" + dates print CheckStr

cmd1='cd /usr/local/element/log/global/log/;ls -ltr' cmd2='/usr/local/element/rel/RAN_RCS_1.0.0.31/bin/linux-x86_pcm64/execCmdCli' cmd3='element add filter rule SPNLOGS enable class SPNAPP severity Informational,Major,Debug,Critical,Minor target log'

def work_comp():

    for host in hosts:
        print host

        ssh = paramiko.SSHClient()
        global ssh
        ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        ssh.connect(host, username=username, password=password, timeout=5.0 )
        exe_comm(cmd1)
        exe_comm(cmd2)
        ssh.close()
        # cmd='cd /usr/local/element/log/global/log/;ls -ltr'
        # cmd2='pwd'
        # #cmd2='/usr/local/element/rel/RAN_RCS_1.0.0.31/bin/linux-x86_pcm64/execCmdCli'
        # cmd3='element add filter rule SPNLOGS enable class SPNAPP severity Informational,Major,Debug,Critical,Minor target log'
        # #cmd5="date |awk '{print $2,$3,$6}'"


def exe_comm(cmd):

        stdin, stdout, stderr = ssh.exec_command(cmd)
        for line in stdout:
            print (line)
        print "Done"

work_comp()

1 个答案:

答案 0 :(得分:0)

有一个parallel SSH client(使用paramiko)可以做到这一点也是异步的。

export class MaDetailsComponent{
static get instance(): IComponentOptions {
    return {
        bindings: {
            uid: "<",
        },
        templateUrl: "./details.html",
        controller: MADetailsController,
        controllerAs: "maCtrl"
    };
  }