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)
- 执行结果