首页 > 系统相关 >python-实现远程windows机器上传文件和远程执行命令

python-实现远程windows机器上传文件和远程执行命令

时间:2023-01-31 11:14:18浏览次数:37  
标签:__ python self windows result test 上传 远程

python-实现远程windows机器上传文件和远程执行命令

1. python-实现远程windows机器上传文件和远程执行命令

  • 编写上传测试文件

    #!/usr/bin/env python3
    # _*_ coding: utf-8 _*_
    # Author:shichao
    # File: .py
    import json
    
    def test():
        result = {
            "test": "Uploaded file successfully, executed successfully",
        }
        json_data = json.dumps(result)
        return json_data
    
    
    
    if __name__ == "__main__":
        data = test()
        try:
            print(data)
        except Exception as e:
            result = {'code': 500, 'msg': 'Execution failed'}
            print(json.dumps(result))
    
  • 编写windows.py文件, 实现远程windows机器上传文件和远程执行命令

    #!/usr/bin/env python3
    # _*_ coding: utf-8 _*_
    # Author:shichao
    # File: .py
    
    import winrm
    import paramiko
    import os
    
    class windows_ssh():
        def __init__(self, ip, username, password):
            self.ip = ip
            self.username = username
            self.password = password
    
        def win_command(self, shell):
            try:
                wintest = winrm.Session('http://' + self.ip + ':5985/wsman', auth=(self.username, self.password))
                ret = wintest.run_cmd(shell)
                ret = ret.std_out.decode()
                return {'code':200, 'msg': '执行命令成功', 'data': ret}
            except Exception as e:
                return {'code': 500, 'msg': '执行命令失败! 错误信息: %s' % e}
    
    
        def win_scp(self, local_file, remote_file):
            try:
                ts = paramiko.Transport(self.ip, 22)  # 获取Transport实例,其中22为端口号
                ts.connect(username=self.username, password=self.password)  # 建立连接
    
                try:
                    # 获取SFTP实例
                    sftp = paramiko.SFTPClient.from_transport(ts)
    
                    # 执行上传动作
                    sftp.put(localpath=local_file, remotepath=remote_file)
                    ts.close()
                    return {'code': 200, 'msg': '上传文件成功'}
                except Exception as e:
                    return {'code':500, 'msg':'上传文件失败 %s' %e }
            except Exception as e:
                return {'code':500, 'msg':'SSH连接失败 %s' %e }
    
    
        # 新增一个远程连接测试方法
        def test(self):
            result = self.win_command('dir')
            return result
    
    
    if __name__ == '__main__':
        ssh = windows_ssh("172.16.128.98", "admin", "123456")
        ssh.test()  # 验证是否能连接
        local_file = os.path.join(os.getcwd(), 'test.py')   # 本地需要上传的文件
    
        ssh.win_scp(local_file, "E:\\test\\test.py")    # 执行paramiko上传文件功能
        result = ssh.win_command('python E:/test/test.py')  # 使用winrm功能进行执行命令
    
        print(result)
    
  • 执行结果
    image

标签:__,python,self,windows,result,test,上传,远程
From: https://www.cnblogs.com/scajy/p/17078316.html

相关文章

  • 配置远程仓库
    方式一:在项目pom.xml文件中配置:<repositories>    <repository>        <id>CustomCentralRepository</id>        <url>https://repo.maven.apach......
  • Python3.7采用CMD自动安装Pygame1.9.4
    ​​Python全栈工程师核心面试300问深入解析(2020版)----全文预览​​​​​​Python3.7采用CMD自动安装Pygame1.9.4,一步即可最近正在学习python开发游戏,需要安装Pygam......
  • Python 反爬虫——文本混淆反爬虫
    文中案例参考GitHub项目4文本混淆反爬虫4.1图片伪装为文字反爬虫有些文字内容实际是图片伪装的提取图片的内容(图片请求响应结果res.content就是图片的字节数据,可以直接......
  • Python 通用爬虫思路
    文章目录​​通用爬虫思路​​​​1.准备URL​​​​2.发送请求,获取响应​​​​3.提取数据​​​​4.保存​​通用爬虫思路1.准备URL准备start_urlurl地址规律不......
  • Windows10中macOS10.14虚拟机性能优化教程
    ​​Python全栈工程师核心面试300问深入解析(2020版)----全文预览​​Windows10中采用VMware15安装安装macOS10.14教程虚拟机中masOS运行并不是完美流畅,需要进行性能......
  • PYTHON基础
    PYTHON基础字面量指在代码中,被写下来的固定的值常用的值类型类型描述数字(Number)支持-整数(int)-浮点数(float)-复数(complex)(-复数:如4+3j,以j结尾......
  • Python-​​pprint的简单使用
    ​​Dataprettyprinter 一、简介​​​print()​和​​pprint()​都是python的打印模块,功能基本一样,唯一的区别就是​​pprint()​模块打印出来的数据结构更加完整,每......
  • 合宙 esp32c3 烧录 MicroPython
    首先安装USB串口驱动(win10以上会自动安装,CH343串口驱动)安装Python安装esptool->pipinstallesptool下载MicroPython固件固件地址名称为:esp32c3-20220618-v1.......
  • Python 错误:TypeError: range() takes no keyword arguments
    问题描述:for循环时使用range()出错:forpageinrange(start=1,stop=8+1,step=1):print(page)结果报错TypeError:range()takesnokeywordargument......
  • Windows 命令行关闭进程
    TASKKILLTASKKILL[/Ssystem[/Uusername[/P[password]]]]{[/FIfilter][/PIDprocessid|/IMimagename]}[/T][/F]描述:使用该工具按照进程......