windows通过python远程到linux服务器执行脚本
windows主机需要安装python , 安装paramiko等模块,执行脚本的时候,确什么模块,根据提示安装什么什么模块就行
python脚本如下:
# -*- coding:utf-8 -*-
import paramiko
import os
class My_ssh():
def __init__(self,remote_ip, remote_ssh_port, ssh_username,ssh_pkey ):
self.remote_ip = remote_ip
self.remote_ssh_port = remote_ssh_port
self.ssh_username = ssh_username
self.key_file = ssh_pkey
def connect_ssh(self):
try:
self.ssh = paramiko.SSHClient()
self.ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
self.ssh.connect(hostname=self.remote_ip, port=self.remote_ssh_port, username=self.ssh_username,pkey=self.key_file)
except Exception as e:
print(e)
return self.ssh
def close_ssh(self):
try:
self.ssh.close()
except Exception as e:
print(e)
def exec_shell(self, shell):
ssh = self.connect_ssh()
try:
stdin, stdout, stderr = ssh.exec_command(shell)
return stdin, stdout, stderr
except Exception as e:
print(e)
def main():
remote_ip = '192.168.2.100'
remote_ssh_port = 5333
ssh_username = 'root'
key_file = 'C:\Users\test\.ssh\id_rsa'
private_key = paramiko.RSAKey.from_private_key_file(key_file)
ph = My_ssh(remote_ip=remote_ip,remote_ssh_port=remote_ssh_port,ssh_username=ssh_username,ssh_pkey=private_key)
# 远程执行ssh命令
get_remote_file = "bash /data/scripts/test/send_files.sh"
stdin, stdout, stderr = ph.exec_shell(get_remote_file)
for line in stdout.readlines():
print(line)
ph.close_ssh()
if __name__ == '__main__':
main()
上面的脚本,打包成exe程序
进入代码目录,打开cmd命令行终端
pyinstaller -F -w -i you_code.py
注意看cmd命令行终端的输出,那里会告诉你exe可执行程序在哪里的
此外,还会在同级目录生成build,dist等目录及相关文件



