码迷,mamicode.com
首页 > 编程语言 > 详细

python paramiko

时间:2021-04-19 14:58:53      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:操作   HCL   local   python   tde   def   username   settime   ignore   

  

paramiko基于SSH连接ssh Server执行操作(SSHClient远程连接,SFTPClient上传下载)

  1. pip install paramiko

     

  2. 简单示例
    import paramiko
    
    
    ssh = paramiko.SSHClient()  # 创建ssh客户端
    know_hosts = paramiko.AutoAddPolicy()  # 创建ssh白名单
    ssh.set_missing_host_key_policy(know_hosts)  # 加载白名单
    ssh.connect(  # 链接server
        hostname=192.168.100.41,
        port=22,
        username=root,
        password=aegis
    )
    stdin, stdout, stderr = ssh.exec_command(ip r)  # 执行命令
    print(stdout.read().decode(encoding=utf8))
    ssh.close()

     

  3. 结合多线程
    from settings import pool
    import paramiko, threading
    
    
    class ParamikoThreading(threading.Thread):
        def __init__(self, host, username, password, port=22, command=ip a):
            self.host = host
            self.username = username
            self.password = password
            self.port = port
            self.command = command
            super(ParamikoThreading,self).__init__()
    
        def run(self) -> None:
            ssh=paramiko.SSHClient()
            know_hosts=paramiko.AutoAddPolicy()
            ssh.set_missing_host_key_policy(know_hosts)
            ssh.connect(
                hostname=self.host,
                port=self.port,
                username=self.username,
                password=self.password
            )
            stdin,stdout,stderr=ssh.exec_command(self.command)
            print(**60)
            print(ip:%s,\ncommand:%s,\n % (self.host,self.command))
            print(stdout.read().decode(encoding=utf8))
            print(**60)
            ssh.close()
    
    if __name__ == __main__:
        command=ip a
        t_pool=[]
        for host in pool:
            t=ParamikoThreading(
                host=host.get(host,localhost),
                username=host.get(username,root),
                password=host.get(password,aegis),
                command=command
            )
            t_pool.append(t)
        for t in t_pool:
            t.start()
        for t in t_pool:
            t.join()

    settings.py

    pool=[
        dict(host=192.168.100.41,username=root,password=aegis),
        dict(host=192.168.100.42,username=root,password=aegis),
        dict(host=192.168.100.43,username=root,password=aegis),
    ]

     

  4. 交互式链接
    import paramiko
    ssh=paramiko.SSHClient()
    know_hosts=paramiko.AutoAddPolicy()
    ssh.set_missing_host_key_policy(know_hosts)
    ssh.connect(
        hostname=192.168.100.41,
        username=root,
        password=aegis,
        port=22
    )
    shell=ssh.invoke_shell()
    shell.settimeout(1)
    
    command=input(>>>+\n)
    shell.send(bytes(command,encoding=utf8))
    while True:
        try:
            recv=shell.recv(1024).decode(encoding=utf8,errors=ignore)
            if recv:
                print(recv)
            else:
                continue
        except:
            command=input(>>>+\n)
            shell.send(bytes(command,encoding=utf8))
    
    ssh.close()

     

  5. sftp文件upload and download
    import paramiko
    trans=paramiko.Transport(
        sock=(192.168.100.41,22)
    )
    
    trans.connect(
        username=root,
        password=aegis
    )
    sftp=paramiko.SFTPClient.from_transport(trans)
    # upload
    # sftp.put(settings.py,/root/settings.py)
    # download 
    sftp.get(/root/oom.c,oom.c)
    sftp.close()

     

python paramiko

标签:操作   HCL   local   python   tde   def   username   settime   ignore   

原文地址:https://www.cnblogs.com/dissipate/p/14668191.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!