python 远程操作linux 增删目录以及文件

import paramiko
import os
import   logging
import json
import unittest
from stat import S_ISDIR,S_ISREG
logging.basicConfig(level = logging.ERROR,format = ‘%(asctime)s  - %(levelname)s -->%(funcName)s  at line %(lineno)d: \n %(message)s‘)
log= logging.getLogger()

"""
  version 0.1:
  fit initial client can not be shut down ,submit a func close client auto after
called paramiko func,support remote upload and download func extend WebankSftp class 

"""
# paramiko client
def  WebankClients(ip,port,user,pwd):
       return WebankSftp(ip,port,user,pwd)

class  WebankSftp(object):
     """
     how to use it fast:

        >>ip = ‘192.168.110.137‘
        >>port = 22
        >>pwd = ‘admin‘
        >>user = ‘root‘
        >>client=WebankClients(ip,port,user,pwd)
        >>client.query_dir("/usr/local/")
        2019-10-27 12:44:41,191  - INFO -->query_dir  at line 58:
          [‘python36‘, ‘Python-3.6.6‘, ‘Python-3.6.6.tgz‘, ‘test6‘]
        >>  client.query_dir("/usr/local/test677")
        2019-10-27 12:56:22,275  - INFO -->_log  at line 1819:
        Authentication (password) successful!
        Traceback (most recent call last):
              File "D:/workspace/ReportProject/wbsftp/minisftp.py", line 223, in <module>
                client.query_dir("/usr/local/test677")
              File "D:/workspace/ReportProject/wbsftp/minisftp.py", line 64, in query_dir
                raise FileExistsError(err.decode("utf-8"))
              FileExistsError: ls: cannot access /usr/local/test677: No such file or directory

     """

     def __init__(self,ip,port,user,pwd):
         self.port = port
         self.pwd = pwd
         self.user = user
         self.ip = ip
         self.cli=SftpOpen(self.ip, self.port, self.user, self.pwd).sftp
         self.para_cli=self.initial()
         self.para_sftp=self.para_cli.open_sftp()
     def initial(self):
          client = paramiko.SSHClient()
          try:
              client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
              client.connect(self.ip, self.port, self.user, self.pwd)
              if client:
                  return client
          except Exception as e:
              log.error(e)

     # paramiko function
     def query_dir(self,dir)->list:
         # connection=self.initial()
         connection = self.para_cli
         shell="ls -l  %s | awk ‘{print $9}‘" % dir
         input, out, err = connection.exec_command(shell)
         res, err = out.readlines(), err.read()
         if res:
             files_list = [i.strip(‘\n‘) for i in res if i.strip(‘\n‘)]
             # log.info(files_list)
             return files_list
         if err:
             # log.info(err)
             raise FileExistsError(err.decode("utf-8"))

     # paramiko  function
     def callback_stdout(self,shell:str):
         """ this is method for execute Shell COMMAND and CallBack Result"""
         #connection = self.initial()
         connection=self.para_cli
         try:
             input, out, err = connection.exec_command(shell,get_pty=True)
             res, err = out.readlines(), err.read()
             if res:
                 i=‘‘
                 for j in res:
                     i =i+j
                 log.info("call_back res:%s" % i)
                 return i
             if err:
                 log.info("call_back err: %s" % err.decode("utf-8"))
                 return err.decode("utf-8")
         except Exception as f:
             log.error(f)

     def  touch(self,filepath):
        # client = self.initial()
        client =self.para_cli
        def create_file(filepath):
            client.exec_command("touch %s"%filepath)
        # create_file(filepath)
        # try:
        a=self.exists_file(filepath)

        if a:
            self.removes(filepath)
            create_file(filepath)
            info={"msg": "File {}  found and have remove it ,success create {}".format(filepath,filepath),"cdde":200,"status":"success"}
            log.info(json.dumps(info,indent=4,ensure_ascii=False) )

        else:
            create_file(filepath)

            # if self.exists_dir(os.path.dirname(filepath)):
            #     create_file(filepath)
            #     info={‘msg‘: ‘{} exists but {} not found,so directly create {} sucess‘.format(os.path.dirname(filepath),filepath,filepath)
            #         ,‘code‘:200,‘status‘:‘success‘}
            #     log.info(json.dumps(info,indent=4,ensure_ascii=False) )
            # else:
            #     # create pdir of filename
            #     self.mkdir(os.path.dirname(filepath))
            #     create_file(filepath)
            #     log.info("{‘msg‘: ‘mkdir %s  success,touch  %s  success ,status:200‘}"%(os.path.dirname(filepath),filepath))
        # except Exception as e:
        #     log.error(e)

     #sftp func
     def mkdir(self,dir):
         """  if linux have exists filename the same with dir ,
         dir will create failed because it have exists  else it will create success """
         #client=SftpOpen(self.ip,self.port,self.user,self.pwd).sftp
         client=self.cli
         # os.path.isdir() just for win not work for linux, will always return False
         try:
              bool=self.exists_dir(dir)
              if bool:
                  client.rmdir(dir)
                  client.mkdir(dir)
              else:
                  client.mkdir(dir)
         except Exception as e:
             #log.error(e)
             log.error("{‘msg‘: ‘mkdir %s failed maybe it have exists same name file or dir ,‘code‘:100,‘status‘: ‘failed‘}"%dir)
         else:
             log.info("{‘msg‘: ‘mkdir %s success‘,‘code‘: 200,‘status‘:‘success‘}"% dir)

     #sftp  func
     def  removes(self,filepath):
         """ this is a method for delete file"""
        #client=SftpOpen(self.ip,self.port,self.user,self.pwd).sftp
         client=self.cli
         try:
           if self.exists_file(filepath):
               client.remove(filepath)
           else:
               pass
         except FileNotFoundError as e:
               info={‘msg‘: ‘File %s Not Found Error‘%filepath, ‘retcode‘:100,‘status‘: ‘failed‘}
               log.error(json.dumps(info,ensure_ascii=False,indent=4),exc_info=False)
         else:
             log.info("{‘msg‘:‘remove file %s success‘,‘recode‘:200,‘status‘: ‘success‘}"% filepath)

     # sftp func
     def list_dir(self,dir):
         #client = SftpOpen(self.ip, self.port, self.user, self.pwd).sftp
         client=self.cli
         try:
             res= client.listdir(dir)
             information=json.dumps({"files": res},indent=4,ensure_ascii=False)
             log.info("%s  contains files:" %dir+information)
             return res
         except FileNotFoundError:
             log.error("{‘msg‘: ‘%s Not Found Error‘, ‘retcode‘:100,‘status‘: ‘failed‘}"%dir)

     # sftp function
     def rm(self,absdir):
         def isdirs(filepath, sftp):
             """this is a method for recursion and delete files and  dir
                 ,os.path.isdir() it doesn‘t work for linux
             """
             return S_ISDIR(sftp.stat(filepath).st_mode)
         def subsftp(absdir,sftp):
             files = sftp.listdir(absdir)
             try:
                 for file in files:
                     filepath = os.path.join(absdir, file)
                     fps = filepath.replace("\\", "/")
                     if isdirs(fps, sftp):
                         # recur util del all files under file path is dir
                         self.rm(fps)
                     else:
                         sftp.remove(fps)
                         log.info("{‘msg‘: ‘remove file %s success,‘retcode‘: 200}"  %(fps))
                 # rmdir empty dir
                 sftp.rmdir(absdir)
             except Exception as e:
                 log.error(e)
             else:
                 log.info("{‘msg‘: ‘recursion delete %s whole dir,‘recode‘:200}" % (absdir))

         #sftp = SftpOpen(self.ip, self.port, self.user, self.pwd).sftp
         sftp=self.cli
         try:
             subsftp(absdir, sftp)
         except Exception as e:
             log.error(e)

     # sftp func
     def  exists_dir(self,dir):
         # client=SftpOpen(self.ip, self.port, self.user, self.pwd).sftp
         client=self.cli
         def isdirs(dir, sftp):
             return S_ISDIR(sftp.stat(dir).st_mode)
         try:
                isdirs(dir,client)
                log.info("exists %s " % dir)
                return True
         except FileNotFoundError as f:
             # info={"msg": "Not Found DIR %s"%dir,"retcode":100,"status": "failed"}
             # log.error(json.dumps(info,indent=4,ensure_ascii=False))
             return False

     #sftp func
     def exists_file(self,filepath):
          client=self.para_sftp
          try:
            client.stat(filepath)
          except Exception as e:
              # info = {"msg": "Not Found FileName %s" % filepath, "retcode": 100, "status": "failed"}
              # log.error(info)
              return False
          else:
                log.info("exists %s file "%filepath)
                return True

     def sftp_close(self):
         self.cli.close()
         self.para_cli.close()
         self.para_sftp.close()

class  SftpOpen(object):
    def  __init__(self,ip,port,user,pwd):
        self.tp= paramiko.Transport(ip, port)
        self.tp.connect(username=user, password=pwd)
        self.sftp = paramiko.SFTPClient.from_transport(self.tp)

class TestSftp(unittest.TestCase):
    @classmethod
    def setUpClass(cls) -> None:
        ip = "192.168.110.151"
        port = 22
        user = "root"
        pwd = "admin"
        cls.client = WebankClients(ip, port, user, pwd)
        log.info("start selfcheck method of sftp ")

    @classmethod
    def tearDownClass(cls) -> None:
        try:
         cls.client.sftp_close()
        except Exception as e:
            log.error(e)
        else:
            log.info("close all connections")
        log.info("finish all unittest selfcheck method of sftp ")

    def test_query_dir(self):
        """测试查询dir下dirs and files"""
        files=self.client.query_dir("/usr/local/listdir")
        self.assertIn(‘list.txt‘,files)
    def test_call_backstdout(self):
        shell="ls -l /usr/local"
        readlines=self.client.callback_stdout(shell)
        self.assertIn("redisdb",readlines)

    def  test_exsitdir(self):
        a=self.client.exists_dir("/usr/local")
        assert a==True

    def  test_exsistfile(self):
        b=self.client.exists_file("/usr/local/redisdb/logs/redis.log")
        assert b==True

    def test_touch(self):
         """create file """
         path="/usr/local/toutest.txt"
         self.client.touch(path)
         a=self.client.exists_file(path)
         self.assertEqual(a,True)
         self.client.removes(path)
    def test_userremove(self):
        """remove file """
        path="/usr/local/tou.txt"
        self.client.touch(path)
        self.client.removes(path)
        a=self.client.exists_file(path)
        self.assertEqual(a,False)

    def  test_mkandrm(self):
        """bug 已经存在目录无法直接mkdir"""
        self.client.mkdir("/usr/local/test1")
        self.client.rm("/usr/local/test1")

if __name__ == ‘__main__‘:

    unittest.main()

.......
----------------------------------------------------------------------
Ran 7 tests in 1.030s


OK


Process finished with exit code 0

 

原文地址:https://www.cnblogs.com/SunshineKimi/p/11823647.html

时间: 2024-08-01 14:40:00

python 远程操作linux 增删目录以及文件的相关文章

如何使用python远程操作linux

在云服务测试中,往往需要我们进入云服务内容进行相关内容的测试.这测试可以使用平台自身的noVNC.外部辅助xshell等工具连接到云服务内部进行测试.但是在如此反复的测试操作中,就需要用到自动化测试方法去解决这方面的需求.在python中我们可以通过第三方库paramiko,对linux的云服务器进行操作.如下命令先行安装 pip install paramiko paramiko包含两个核心组件:SSHClient和SFTPClient. SSHClient的作用类似于Linux的ssh命令,

java使用Jsch实现远程操作linux服务器进行文件上传、下载,删除和显示目录信息

1.java使用Jsch实现远程操作linux服务器进行文件上传.下载,删除和显示目录信息. 参考链接:https://www.cnblogs.com/longyg/archive/2012/06/25/2556576.html https://www.cnblogs.com/longyg/archive/2012/06/25/2561332.html https://www.cnblogs.com/qdwyg2013/p/5650764.html#top 引入jar包的maven依赖如下所示:

Python远程登录Linux操作系统,执行命令、创建目录、上传及下载文件

主要是创建文件: #! /bin/bash # -*- coding: utf-8 -*- import paramiko import os ssh = paramiko.SSHClient() key = paramiko.AutoAddPolicy() ssh.set_missing_host_key_policy(key) pkey = paramiko.RSAKey.from_private_key_file('/root/.ssh/id_rsa') paramiko.util.log

远程操作linux的利器:putty和psftp

通过SSH远程操作Linux的好工具:putty(远程操作linux)  psftp(远程上传下载) 占用空间小,操作方便,不需安装配置即可使用,官方网站(http://www.chiark.greenend.org.uk/~sgtatham/putty/ )可提供相应软件的下载,免费的哦 到 Unix-Center.net/Unix体验中心注册个帐后就可 利用putty登陆 学学unix了 : psftp工具相关命令使用(转载于http://live.haliluya.org/weblog/2

linux下目录与文件的权限及特殊权限

一.Linux下目录与文件的权限 chown    更改文件或目录的所属主,所属组   /etc/passwd 配置文件存放用户信息 useradd    添加用户 chown 所属主:所属组 文件或目录 chown -R    更改目录及目录下面的文件所有权限 chown -v    可视化显示权限更改 [[email protected] tmp]# chown user1:root aa [[email protected] tmp]# ls -l drwxr-xr--. 2 user1 

linux获取目录下文件

查看当前目录下的文件:find . -type f查看当前目录下的文件夹: find . -type d如果文件file1不为空: if [ -s file1 ];then       echo "file1 不为空"fi #!/bin/sh for f in `find ./testdir -type f`; do         if [ -s $f ];then                 echo $f is not empty.                 echo 

Python实例31[批量对目录下文件重命名]

经常会遇到下载的文件或电子书,名字中间都包含了一些网址信息,实际使用中由于名字太长不方便,下面的脚本使用正则表达式来对目录下的所有文件重命名:例如: 修改前:[大家网]Mac OS X for Unix Geeks[www.TopSage.com].mobi修改后:Mac OS X for Unix Geeks.mobi python代码如下 import osimport re def rename_dir(dir,regex,f):  if not os.path.isdir(dir) or

linux 查看目录下文件占用情况

系统盘满会造成网卡不能编辑  df  -l 对于du命令,-h表示 查看以M 为单位显示文件大小结果,-s 统计此目录中所有文件大小总和. du * / --exclude=home --exclude=bkup   排除 /home  /backup 目录下的不算,查看哪个占用空间最大 linux 查看目录下文件占用情况

Linux下目录和文件的三种权限

在Linux下,目录和文件会有3种权限,分别是:可读r,可写w,可执行x.那么目录和文件的权限有什么区别呢?这篇博文解释的就是这个问题. 在解释之前,我们应该有一些基础知识:Linux系统下有3种身份(所有组.用户组.其他人),3种权限(r,w,x),3个修改权限的命令(chown, chgrp, chmod). 1. 文件的3种权限 1)read:可读取文件的内容,例如读取文本文件的内容. 2)writer:可以编辑.新增或者修改文件的内容,但是不可以删除该文件.这里的修改都是基于文件内容的,