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 """)
复制代码

 

posted @   chengxuyonghu  阅读(310)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
点击右上角即可分享
微信分享提示