zoukankan      html  css  js  c++  java
  • python ssh 执行shell命令实例

    import paramiko
    
    def sshcmd(ip, port,username,password,command):
        ssh = paramiko.SSHClient()
        ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        ssh.connect(ip, port, username, password)
        stdin, stdout, stderr = ssh.exec_command(command)
        
        stdout_info = stdout.readlines()
        for ii in  stdout_info:
            print ii
    
    
    
    
        #err_info = stderr.readlines()
        #if err_info:
        #    print("{} is failed: {}".format(ip,err_info))
        #else:
        #    print("{} is successful: {}".format(ip, stdout_info))
        ssh.close()
    
    iplist  = {
        
        "192.168.1.10":[22,"root","asawwkl&00"],
    
    
    
    
    
    }
    
    for ip in iplist:
    
        print  "#" * 50 +"%s"%ip + "#" * 50 
        #sshcmd(ip, iplist[ip][0], iplist[ip][1], iplist[ip][2], """  df -Th   """)
        #sshcmd(ip, iplist[ip][0], iplist[ip][1], iplist[ip][2], """  ntpq -p ;df -h;ps -x  """)
        #sshcmd(ip, iplist[ip][0], iplist[ip][1], iplist[ip][2], """  sed -i '21 iserver 192.168.1.132' /etc/ntp.conf """)
        #sshcmd(ip, iplist[ip][0], iplist[ip][1], iplist[ip][2], """  cat /etc/ntp.conf |grep "server 192.168.1.132"  ;ps -eaf |grep ntp |grep -v grep  """)
        #sshcmd(ip, iplist[ip][0], iplist[ip][1], iplist[ip][2], """  /bin/systemctl restart ntpd.service ; systemctl enable ntpd.service ; systemctl is-enabled ntpd.servicE """)
        sshcmd(ip, iplist[ip][0], iplist[ip][1], iplist[ip][2], """  systemctl is-enabled ntpd ;systemctl list-unit-files |grep ntp """)
  • 相关阅读:
    每日日报2021 5/25
    每日日报2021 5/24
    Rust-Cargo是什么
    Rust学习-Intellij开发环境配置
    设计模式-命令模式
    918. Maximum Sum Circular Subarray
    不错的画类图工具-PlantUML
    Daily Coding Problem: Problem #793
    读懂UML类图
    1753. Maximum Score From Removing Stones
  • 原文地址:https://www.cnblogs.com/chengxuyonghu/p/13750077.html
Copyright © 2011-2022 走看看