继Python制作简单的GUI界面,实现批量修改excel文件特定行列单元格之后,笔者又写了一个Linux下Python远程执行matlab脚本并将生成的数据下载到本地的脚本,GUI采用之前现成的框架比较容易。
1.需要配置环境
1.1编译安装sshpass 配置到环境变量
1.2安装python3并安装pysimplegui库
pip install pysimplegui
2.脚本如下
#-*-Python-*-
# Created by [哥的迷思](https://www.jianshu.com/u/e3dc5c8207e8) at 13 Oct 2021 09:22
"""
This script runs matlab script at remote server then downloads to ~/tmp/
defaultVars parameters
----------------------
:param param1/param2/...: such params can be passed to this script as <path to script>.run(param1=103496,param2=3,...)
"""
#defaultVars(param1=103496, param2=3,username = '',password ='')
import os
import array
import datetime
from random import randint
import PySimpleGUI as sg
class RemoteLinux:
def __init__(self, ip, username, password):
self.ip = ip
self.username = username
self.password = password
def run_cmd(self, cmd):
cmd_str = "sshpass -p {} ssh {}@{} \"{}\"".format(self.password, self.username, self.ip, cmd)
result = os.popen(cmd_str).read().splitlines()
for msg in result:
print(msg)
return result
def run_scp_2_remote(self, local_dir, remote_dir):
cmd_str = "sshpass -p {} scp -r {}/* {}@{}:{}".format(self.password, local_dir, self.username, self.ip,remote_dir)
result = os.popen(cmd_str).read().splitlines()
for msg in result:
print(msg)
return result
def run_scp_2_local(self, local_dir, remote_dir):
cmd_str = "sshpass -p {} scp -r {}@{}:{}/* {}".format(self.password, self.username, self.ip, remote_dir,local_dir)
result = os.popen(cmd_str).read().splitlines()
for msg in result:
print(msg)
return result
sg.theme('Dark Blue 3') # please make your windows colorful
frame1_layout = [
[sg.Text('Param1', size=(10, 1)), sg.InputText('', key='param1', size=(10, 1)), sg.Text('Param2', size=(10, 1)), sg.InputText('3', key='param2', size=(10, 1))],
[sg.Text('Username', size=(10, 1)), sg.InputText('', key='username', size=(10, 1)),sg.Text('Password', size=(10, 1)), sg.InputText('',password_char='*', key='password', size=(10, 1))],
[sg.Text('Server', size=(10, 1)), sg.InputText('192.168.1.8', key='server', size=(10, 1))],
]
frame2_layout = [
#[sg.Image(r'logo4.png')]
]
layout = [
[sg.Frame('Please set parameters below', frame1_layout , font='Any 8', title_color='blue'),sg.Frame('BLANK', frame2_layout , font='Any 12', title_color='blue')],
[sg.FolderBrowse('Choose output path'), sg.InputText('~/tmp', key='output_folder', size=(20, 1))],
[sg.Submit('OK'), sg.Cancel('CLOSE')],
[sg.Text('', size=(40, 1), font=('Helvetica', 8), justification='center', key='text')],
]
#
window = sg.Window('Get file from server', layout)
#
while True:
event, values = window.read()
if event == sg.WIN_CLOSED or event == 'CLOSE': #
break
##########################################################################
param1= int(values['param1'])
param2 = float(values['param2'])
username = values['username'] # username at remote server
password = values['password'] # password at remote server
server = values['server']
output_folder = values['output_folder']
#
MyLinux=RemoteLinux(server,username,password)
remote_dir='/path_to_the_matlabscript/'#
CMD="pwd && matlab -r 'cd remote_dir;[matlab script name(without .m)]({},{});exit'".format(param1, param2) # asuming that the script needs to parameters(param1, param2)
window['text'].update(CMD)
MyLinux.run_cmd(CMD)
CMD="if [ ! -d "+output_folder+" ];then mkdir "+output_folder+"; fi"
os.popen(CMD)
local_dir=output_folder+'/'
MyLinux.run_scp_2_local(local_dir, remote_dir)
window['text'].update('file path:'+local_dir)
sg.Popup('Done!')
##########################################################################
window.close()
网友评论