9

再见Xshell、Xftp!Python执行Linux命令、上传下载远程文件

 3 years ago
source link: https://my.oschina.net/u/4638454/blog/5070375
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

相信大家应该都接触过Linux操作系统(Ubuntu、Centos等),那么在使用的Linux操作系统需要使用一些远程ssh工具,尤其是公网服务器。

常用的ssh工具主要有:Xshell、MobaXterm、Termius等

上传文件到远程Linux服务器:Xftp、Filezilla 等

这些工具有免费,有收费的。并且价格小贵。今天辰哥就来教大家如何用Python连接远程Linux服务器,实现执行命令和上传文件。这里用到的Python库是Paramiko

核心知识点(亮点):

1、Python连接远程Linux服务器

2、执行命令并返回结果

3、上传文件到远程Linux服务器

4、从远程Linux下载文件

01 Paramiko库

1.安装paramiko库

在开始编写代码之前先安装一下Python连接Linux服务器的库,安装命令如下:

#1、安装依赖包
pip install ecdsa
pip install Crypto
pip install Twisted
#2、安装paramiko
pip install paramiko

提示:先安装三个依赖库,再安装Paramiko,直接安装Paramiko会报错。

2. paramiko核心组件

SSH组件:SSHClient,作用类似于Linux的ssh命令,用于执行远程命令。

SFTP组 件:SFTPClient,作用类似于Linux的sftp命令,用于文件上传、下载、修改文件权限等。

02 SSHClient

1、SSHClient常用方法介绍

这里辰哥以自己的远程服务器为例给大家演示,首先先连接服务器

import paramiko
# ip、用户名、密码
ip = "远程Linux的IP地址"
port = 22
user = "root"
password = "密码"
 
 
# 创建SSHClient 实例对象
ssh = paramiko.SSHClient()
# 调用方法,表示没有存储远程机器的公钥,允许访问
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
# 连接远程机器,地址,端口,用户名密码
ssh.connect(ip, port, user, password, timeout=10)

执行linux命令,如:ls

# 输入linux命令
command1 = "ls"
ssh.exec_command(command1)
# stdout 为正确输出,stderr为错误输出
stdin, stdout, stderr = ssh.exec_command(command2)
# 输出命令执行结果
result = stdout.read()
print(result)

xshell返回结果:

Python执行代码结果:

2、执行多条Linux命令

上面的案例只是执行一条Linux命令,在实际应用中往往需要执行2条或者2条以上的Linux,下面演示如何执行多条命令

错误的方式

# 输入linux命令
command1 = "cd /www/wwwroot"
command2 = "ls"
ssh.exec_command(command1)
stdin, stdout, stderr = ssh.exec_command(command2)
# 输出命令执行结果
result = stdout.read()
print(result)

上面的命令是先执行:cd /www/wwwroot ,进入到wwwroot,再查看里面的文件夹,下面是xshell里面执行的结果

Python代码执行的结果

可以看到结果和之前的一样,所以这种方式是错误(辰哥一开始也是这样干的,这里就给大家提个醒)

正确的方式:

# 输入linux命令
command = "cd /www/wwwroot ;ls"
stdin, stdout, stderr = ssh.exec_command(command)
# 输出命令执行结果
result = stdout.read()
## bytes 转 str
result = str(result)
result = result.split('\\n')
for i in result:
     print(i)

直接在用;把多条命令隔开即可,转为str后,通过**\\n**去分割结果,按行输出

03 SFTPClient

1、SFTPClient常用方法介绍

2、 上传文件到Linux

这里先在本地新建一个文本文件:辰哥.txt ,并在里面输入了一些内容

连接Linux服务器

# 获取Transport实例
tran = paramiko.Transport(('远程Linux的ip', 22))
# 连接SSH服务端,使用password
tran.connect(username="用户名", password='密码')
# 获取SFTP实例
sftp = paramiko.SFTPClient.from_transport(tran)

上传文件

# 设置上传的本地/远程文件路径
localpath = "D:/公众号/0603/辰哥.txt"
remotepath = "/www/wwwroot//辰哥.txt"
 
 
# 执行上传动作
sftp.put(localpath, remotepath)
# 关闭连接
tran.close()

效果如下:

3、从Linux服务器下载文件

辰哥在Linux服务器新建了一个文本文件:Python研究者.txt ,并写入内容

下载文件

localpath2 = "D:/公众号/0603/Python研究者.txt"
remotepath2 = "/www/wwwroot/Python研究者.txt"
# 执行下载动作
sftp.get(remotepath2, localpath2)
# 关闭连接
tran.close()

效果:

本文讲解了Paramiko库,并讲解ssh和sftp两大组件,设计的核心知识点(亮点):

1、Python连接远程Linux服务器

2、执行命令并返回结果

3、上传文件到远程Linux服务器

4、从远程Linux下载文件


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK