python写的一些小工具——python连接linux并操作

封装了paramiko这个库

# -*- coding:utf-8 -*-
import paramiko


class SSHConnection:
    # 初始化连接创建Transport通道
    def __init__(self, host='xxx.xxx.xxx.xxx', port=22, user='xxx', pwd='xxxxx'):
        self.host = host
        self.port = port
        self.user = user
        self.pwd = pwd
        self.__transport = paramiko.Transport((self.host, self.port))
        self.__transport.connect(username=self.user, password=self.pwd)
        self.sftp = paramiko.SFTPClient.from_transport(self.__transport)

    # 关闭通道
    def close(self):
        self.sftp.close()
        self.__transport.close()

    # 上传文件到远程主机
    def upload(self, local_path, remote_path):
        self.sftp.put(local_path, remote_path)

    # 从远程主机下载文件到本地
    def download(self, local_path, remote_path):
        self.sftp.get(remote_path, local_path)

    # 在远程主机上创建目录
    def mkdir(self, target_path, mode='0777'):
        self.sftp.mkdir(target_path, mode)

    # 删除远程主机上的目录
    def rmdir(self, target_path):
        self.sftp.rmdir(target_path)

    # 查看目录下文件以及子目录(如果需要更加细粒度的文件信息建议使用listdir_attr)
    def listdir(self, target_path):
        return self.sftp.listdir(target_path)

    # 删除文件
    def remove(self, target_path):
        self.sftp.remove(target_path)

    # 查看目录下文件以及子目录的详细信息(包含内容和参考os.stat返回一个FSTPAttributes对象,对象的具体属性请用__dict__查看)
    def listdirattr(self, target_path):
        try:
            list = self.sftp.listdir_attr(target_path)
        except BaseException as e:
            print(e)
        return list

    # 获取文件详情
    def stat(self, remote_path):
        return self.sftp.stat(remote_path)

    # SSHClient输入命令远程操作主机
    def cmd(self, command):
        ssh = paramiko.SSHClient()
        ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy)
        ssh._transport = self.__transport
        stdin, stdout, stderr = ssh.exec_command(command)
        result = stdout.read()
        print(result)
        return result
link_ssh = SSHConnection()

利用封装好的库进行,连接linux再执行sh文件

# -*- coding:utf-8 -*-
import os
import sys

from luyouadd import linklinux
# 当前目录
localpath = os.path.dirname(__file__)
# 本地放的sh文件
shpath = localpath + "/objective_ip.sh"
# 需要连接的zhuji文件
zhujipath = localpath + "/zhuji.txt"

def test():
    
    with open(zhujipath,"r") as f1:
        zhuji = f1.readlines()
        for line in zhuji:
            print(line)
            try:
                class1=linklinux.SSHConnection(host=line,user="root",pwd="root")
            except:
                print("连接失败")
            else:
                # 创建文件夹
                class1.cmd("mkdir /etc/luyou")
                class1.upload(shpath,"/etc/luyou/objective_ip.sh")
                class1.cmd("chmod 777 /etc/luyou/objective_ip.sh")
                class1.cmd("sh /etc/luyou/objective_ip.sh")
            finally:
                class1.close()
test()



  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值