python笔记-paramiko模块和idfflib模块

paramiko远程密码连接

# 基于ssh用于连接远程服务器做操作:远程执行命令, 上传文件, 下载文件
import  paramiko

# ssh root@172.25.254.250
# 创建一个ssh对象;
client = paramiko.SSHClient()

# 2. 解决问题:如果之前没有;连接过的ip, 会出现
# Are you sure you want to continue connecting (yes/no)? yes
# 自动选择yes
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())

# 3. 连接服务器
client.connect(hostname='172.25.254.1',
               port=22,
               username='root',
               password='redhat')
# 4. 执行操作
stdin, stdout, stderr = client.exec_command('hostnaewdeme')


# 5. 获取命令的执行结果;
result = stdout.read().decode('utf-8')
print(result)

print(stderr.read())

# 6. 关闭连接
client.close()

paramiko批量远程密码连接

# 基于ssh用于连接远程服务器做操作:远程执行命令, 上传文件, 下载文件
import  paramiko
import  logging

from paramiko.ssh_exception import NoValidConnectionsError, AuthenticationException
def connect(cmd, hostname, port=22, username='root', password='westos'):
    # ssh root@172.25.254.250
    # 创建一个ssh对象;
    client = paramiko.SSHClient()

    # 2. 解决问题:如果之前没有;连接过的ip, 会出现
    # Are you sure you want to continue connecting (yes/no)? yes
    # 自动选择yes
    client.set_missing_host_key_policy(paramiko.AutoAddPolicy())

    try:
        # 3. 连接服务器
        client.connect(hostname=hostname,
                       port=port,
                       username=username,
                       password=password)

        print("正在连接主机%s......." %(hostname))
    except NoValidConnectionsError as e:
        print("连接失败")
    except AuthenticationException as e:
        print("密码错误")
    else:
        # 4. 执行操作
        stdin, stdout, stderr = client.exec_command(cmd)

        # 5. 获取命令的执行结果;
        result = stdout.read().decode('utf-8')
        print(result)

        # 6. 关闭连接
        client.close()

with open('host.txt') as f:
    for line in f:
        line = line.strip()
        hostname, port, username, password = line.split(':')
        print(hostname.center(50, '*'))
        connect('hostname', hostname, port, username, password)

paramiko基于公钥密钥连接

# 基于ssh用于连接远程服务器做操作:远程执行命令, 上传文件, 下载文件
import  paramiko
from paramiko.ssh_exception import NoValidConnectionsError, AuthenticationException


def connect(cmd, hostname, port=22, user='root'):
    # ssh root@172.25.254.250
    # 创建一个ssh对象;
    client = paramiko.SSHClient()

    # 返回一个私钥对象
    private_key = paramiko.RSAKey.from_private_key_file('id_rsa')


    # 2. 解决问题:如果之前没有;连接过的ip, 会出现
    # Are you sure you want to continue connecting (yes/no)? yes
    # 自动选择yes
    client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    try:
        # 3. 连接服务器
        client.connect(hostname=hostname,
                       port=port,
                       username=user,
                       pkey=private_key
                      )
        # 4. 执行操作
        stdin, stdout, stderr = client.exec_command(cmd)
    except NoValidConnectionsError as e:
        print("连接失败")
    except AuthenticationException as e:
        print("密码错误")
    else:
        # 5. 获取命令的执行结果;
        result = stdout.read().decode('utf-8')
        print(result)
    finally:
        # 6. 关闭连接
        client.close()


for count in range(254):
    host = '172.25.254.%s' %(count+1)
    print(host.center(50, '*'))
    connect('uname', host)

基于用户名密码上传下载

import  paramiko
transport = paramiko.Transport(('172.25.254.39', 22))
transport.connect(username='root', password='westos')
sftp = paramiko.SFTPClient.from_transport(transport)
# 上传文件, 包含文件名
sftp.put('/tmp/kiosk', '/mnt/kiosk1')
sftp.get('/mnt/kiosk', '/home/kiosk/Desktop/day18/kiosk')
transport.close()


基于密钥上传下载


import  paramiko
# 返回一个私钥对象
private_key = paramiko.RSAKey.from_private_key_file('id_rsa')
transport = paramiko.Transport(('172.25.254.39', 22))
transport.connect(username='root',pkey=private_key)
sftp = paramiko.SFTPClient.from_transport(transport)
# 上传文件, 包含文件名
sftp.put('/tmp/kiosk', '/mnt/kiosk2')
sftp.get('/mnt/kiosk2', '/home/kiosk/Desktop/day18/kiosk')
transport.close()

paramiko再次封装

import os
import paramiko
from paramiko.ssh_exception import NoValidConnectionsError, AuthenticationException, SSHException


class SshRemoteHost(object):
    def __init__(self, hostname, port, user, passwd, cmd):
        # 指的不是shell命令
        #   cmd shell命令
        #   put
        #   get
        self.hostname  = hostname
        self.port = port
        self.user = user
        self.passwd = passwd
        self.cmd = cmd
    def run(self):
        """默认调用的内容"""
        # cmd hostname
        # put
        # get
        cmd_str =  self.cmd.split()[0] # cmd
        # 类的反射, 判断类里面是否可以支持该操作?
        if hasattr(self, 'do_'+ cmd_str):  # do_cmd
            getattr(self, 'do_'+cmd_str)()
        else:
            print("目前不支持该功能")
    def do_cmd(self):
        # 创建一个ssh对象;
        client = paramiko.SSHClient()

        # 2. 解决问题:如果之前没有;连接过的ip, 会出现
        # Are you sure you want to continue connecting (yes/no)? yes
        # 自动选择yes
        client.set_missing_host_key_policy(paramiko.AutoAddPolicy())

        try:
            # 3. 连接服务器
            client.connect(hostname=self.hostname,
                           port=self.port,
                           username=self.user,
                           password=self.passwd)

            print("正在连接主机%s......." % (self.hostname))
        except NoValidConnectionsError as e:
            print("连接失败")
        except AuthenticationException as e:
            print("密码错误")
        else:
            # 4. 执行操作
            # cmd uname
            # cmd ls /etc/
            # *******注意:
            cmd = ' '.join(self.cmd.split()[1:])
            stdin, stdout, stderr = client.exec_command(cmd)

            # 5. 获取命令的执行结果;
            result = stdout.read().decode('utf-8')
            print(result)

            # 6. 关闭连接
            client.close()
    def do_put(self):
        # put /tmp/passwd /tmp/passwd
        # put /tmp/passwd /tmp/pwd
        # put /tmp/passwd   # 将本机的/tmp/passwd文件上传到远程主机的/tmp/passwd;
        print("正在上传.....")
        try:
            transport = paramiko.Transport((self.hostname, int(self.port)))
            transport.connect(username=self.user, password=self.passwd)
        except SSHException as e:
            print("连接失败")
        else:
            sftp = paramiko.SFTPClient.from_transport(transport)
            newCmd  = self.cmd.split()[1:]
            if len(newCmd) == 2:
                # 上传文件, 包含文件名
                sftp.put(newCmd[0], newCmd[1])
                print("%s文件上传到%s主机的%s文件成功" %(newCmd[0],
                                             self.hostname,  newCmd[1]))
            else:
                print("上传文件信息错误")

            transport.close()

    def do_get(self):
        print("正在下载.....")
# 2. 根据选择的主机组, 显示包含的主机IP/主机名;
# 3. 让用户确认信息, 选择需要批量执行的命令;
#       - cmd shell命令
#       - put 本地文件 远程文件
#       - get 远程文件  本地文件
def main():
    # 1. 选择操作的主机组:eg: mysql, web, ftp
    groups = [file.rstrip('.conf') for file in os.listdir('conf')]
    print("主机组显示:".center(50, '*'))
    for group in groups: print('\t', group)
    choiceGroup = input("清选择批量操作的主机组(eg:web):")

    # 2. 根据选择的主机组, 显示包含的主机IP/主机名;
    #   1). 打开文件conf/choiceGroup.conf
    #   2). 依次读取文件每一行,
    #   3). 只拿出ip

    print("主机组包含主机:".center(50, '*'))
    with open('conf/%s.conf' %(choiceGroup)) as f:
        for line in f:
            print(line.split(':')[0])
        f.seek(0,0)  # 把指针移动到文件最开始
        hostinfos = [line.strip() for line in f.readlines()]
    # 3. 让用户确认信息, 选择需要批量执行的命令;
    print("批量执行脚本".center(50, '*'))
    while True:
        cmd = input(">>:").strip()  # cmd uname
        if cmd:
            if cmd == 'exit' or cmd =='quit':
                print("执行结束, 退出中......")
                break
            # 依次让该主机组的所有主机执行
            for info in hostinfos:
                # 'ip:port:user:passwd'
                host, port, user, passwd = info.split(":")
                print(host.center(50, '-'))
                clientObj = SshRemoteHost(host, port, user, passwd, cmd)
                clientObj.run()
if __name__ == '__main__':
    main()

模块difflib

比较并找出两个文本内容的不同

import difflib

text1="""1. Beautiful is better than ugly.
2. Explicit is better than implicit.
3. Simple is better than complex.
4. Complex is better than complicated.
""".splitlines(keepends=True)

text2="""1. Beautiful is better than ugly.
3.   Simple is better than complex.
4. Complicated is better than complex.
5. Flat is better than nested.
""".splitlines(keepends=True)

#第一种方式:
d = difflib.Differ()
result = list(d.compare(text1,text2))
result = ''.join(result)
print(result)
"""
运行结果以文本行方式显示,不容易理解
"""

#第二种方式:
d = difflib.HtmlDiff()
with open('diffirent.html','w') as f:
    f.write(d.make_file(text1,text2))
"""
生成一个html文件,用浏览器打开,会显示两个文本内容的不同之处
"""

第一种方式
这里写图片描述

第二种方式

这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值