栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

windows通过python远程到linux服务器执行脚本

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

windows通过python远程到linux服务器执行脚本

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等目录及相关文件

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/929218.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号