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 """)
  • 相关阅读:
    iOS开发之详解剪贴板
    iOS7(Xcode5)中隐藏状态栏的方法
    如何使用iOS手势UIGestureRecognizer
    如何给列表加入搜索功能
    UITabBarController 相关
    UIButton 相关
    UINavigationController 相关
    Apple Watch 会再一次改变世界么?
    编译VLC for IOS
    ffmpeg Win8移植记(二)
  • 原文地址:https://www.cnblogs.com/chengxuyonghu/p/13750077.html
Copyright © 2011-2022 走看看