zoukankan      html  css  js  c++  java
  • 第9章:Python自动化管理

    1.使用SSH协议访问远程服务器

        SSH协议

        OpenSSH协议

        使用密钥登陆远程服务器

        使用ssh-agent管理私钥

    2.使用Polysh批量管理服务器

    Polysh requires python 3.4 or later.
    pip install polysh
    # cat hosts
    192.168.1.121
    192.168.1.122
    # polysh --ssh='exec ssh -p 22' --user=root --hosts-file=hosts
    waiting (2/2)> cd /
    192.168.1.122 : cd /
    192.168.1.121 : cd /

    3.SSH协议的Python实现paramiko

    1).paramiko的安装

        pip install paramiko

    2).SSHClient类与SFTPClient类

        paramiko包含两个核心组件:SSHClient和SFTPClient

    3).paramiko的基本使用

    import paramiko
    
    ssh = paramiko.SSHClient()
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    ssh.connect('192.168.1.202',22,'root','123456')
    stdin,stdout,stderr = ssh.exec_command('ip a')
    print stdout.read()
    ssh.close()

    4).使用paramiko部署监控程序

    import paramiko
    
    def deploy_monitor(ip):
        with paramiko.SSHClient() as client:
            client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
            client.connect(ip,22,'root','123456')
            stdin,stdout,stderr = client.exec_command('ls -lh /')
            print(stdout.readlines())
    
            with client.open_sftp() as sftp:
                sftp.put('monitor.py','/monitor.py')
                sftp.chmod('/monitor.py', 0o755)
    
    def main():
        with open('/py/hosts') as f:
            for line in f:
                deploy_monitor(line.strip())
    
    if __name__ == '__main__':
        main()

    4.自动化部署工具Fabric

    Fabric是基于Python2(2.5~2.7)的一个Python库,也是一个命令行工具
    使用Fabric提供的命令行工具调用程序,能够非常方便地执行应用部署和系统管理操作
    Fabric依赖paramiko进行SSH交互,Fabric作者也是paramiko作者
  • 相关阅读:
    88. Merge Sorted Array
    87. Scramble String
    86. Partition List
    85. Maximal Rectangle
    84. Largest Rectangle in Histogram
    [Windows] Diskpart Scripts and Examples
    [Windows] 对于 mount 到文件夹路径下的分区,也可以使用 GetDiskFreeSpaceExA 函数
    [Windows] DiskPart commands
    [Windows] 如何用编程的方式格式化硬盘
    Windows API Index
  • 原文地址:https://www.cnblogs.com/allenhu320/p/11353865.html
Copyright © 2011-2022 走看看