paramiko 模块远程管理 liunx 服务器

简介:

paramiko是用python语言写的一个模块,遵循SSH2协议,支持以加密和认证的方式,进行远程服务器的连接。

由于使用的是python这样的能够跨平台运行的语言,所以所有python支持的平台,如Linux, Solaris, BSD, MacOS X, Windows等,paramiko都可以支持,因此,如果需要使用SSH从一个平台连接到另外一个平台,进行一系列的操作时,paramiko是最佳工具之一。


一、远程连接服务器 

  方式1 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import   paramiko
 
#实例化客户端
ssh  =  paramiko.SSHClient()
 
#设置默认授信列表
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
 
# 设置连接信息
ssh.connect(
     hostname = "192.168.10.32" ,
     port = 22 ,
     username = "root" ,
     password = "123456"
)
# 输入远程需要执行的命令
stdin,stdout,stderr  =  ssh.exec_command( "ls" )
       #stdin 需要输入的部分
       #stdout 返回输出的部分
       #stderr 错误部分
print  (stdout.read())
ssh.close()


方式2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#coding:utf-8
 
import  paramiko
trans  =  paramiko.Transport(( "192.168.10.32" , 22 ))
trans.connect(username = "root" ,password = "123456" )
ssh  =  paramiko.SSHClient() #实例化一个客户端
ssh._transport  =  trans  #设置客户端使用该通道
shell  =  ssh.invoke_shell() #实例化一个shell
shell.settimeout( 10 ) #设置超时时间
shell.send( "ls\n" )
while  True :
     recv  =  shell.recv( 9999 )
     print  (recv)
ssh.close()


二、远程连接服务器创建交互式的shell 终端

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
trans  =  paramiko.Transport(( "192.168.10.32" , 22 ))
trans.connect(username  =  "root" ,password  =  "123456" )
#登录前必须实例化一个客户端
ssh  =  paramiko.SSHClient()
ssh._transport = trans #设置客户端使用该通道
shell  =  ssh.invoke_shell() #实例化一个shell
shell.settimeout( 0.1 #设置超时等待时间
shell.send( raw_input ( ">>>" ) + "\n" )
 
while  True :
     try :
         recv  =  shell.recv( 99999 )
         if  recv:
             print  (recv)
         else :
             continue
     except :
         command  =  raw_input ( ">>>" )
         shell.send(command + "\n" )
         if  command  = =  "exit" :
             break
ssh.close()

三 、使用 paramiko 模块上传下载文件


1
2
3
4
5
6
7
8
9
10
11
12
#上传文件
 
trans  =  paramiko.Transport(( "192.168.10.32" , 22 ))
trans.connect(username = "root" ,password = "123456" )
sftp  =  paramiko.SFTPClient.from_transport(trans) #实例化一个文件上传下载的客户端
下载文件  将服务器的文件下载到本地
sftp.get( "/root/11.py" , "aa.py" # 服务器路径 ,本地路径
trans.close()
 
上传文件文件 将本地文件上传到服务器上
 
sftp.put( "404.html" , "/root/404.html" ) 本地路径 ,服务器路径




本文转自 水滴石川1 51CTO博客,原文链接:http://blog.51cto.com/sdsca/2046146,如需转载请自行联系原作者
相关文章
|
6月前
|
PHP Apache
PHP环境搭建(配置php模块到apache服务器)
PHP环境搭建(配置php模块到apache服务器)
62 0
|
6月前
|
存储 运维 数据挖掘
服务器数据恢复-服务器硬盘掉线导致银行业务模块崩溃的数据恢复案例
某银行的某一业务模块崩溃,无法正常使用。排查服务器故障,发现运行该业务模块的服务器中多块硬盘离线,导致上层应用崩溃。 故障服务器内多块硬盘掉线,硬盘掉线数量超过服务器raid阵列冗余级别所允许的硬盘掉线数量,导致服务器瘫痪。可以通过修复硬盘物理故障,提取故障盘数据后重组raid的方案来恢复服务器数据。
|
1月前
|
网络安全 Apache PHP
Liunx服务器如何配置https(二)
Liunx服务器如何配置https(二)
32 0
Liunx服务器如何配置https(二)
|
1月前
|
Apache
Liunx服务器如何配置https(一)
Liunx服务器如何配置https(一)
32 0
Liunx服务器如何配置https(一)
|
9月前
|
安全 Unix 应用服务中间件
安装Nginx服务器,并添加SSL模块
安装Nginx服务器,并添加SSL模块
97 0
|
9月前
|
Python
python自带模块获取服务器主机名称、IP地址和mac地址
python自带模块获取服务器主机名称、IP地址和mac地址
95 1
|
10月前
|
网络协议 数据库
nf_conntrack模块导致服务器Drop Packet
nf_conntrack模块导致服务器Drop Packet
109 0
|
24天前
|
Ubuntu JavaScript 关系型数据库
在阿里云Ubuntu 20.04服务器中搭建一个 Ghost 博客
在阿里云Ubuntu 20.04服务器上部署Ghost博客的步骤包括创建新用户、安装Nginx、MySQL和Node.js 18.x。首先,通过`adduser`命令创建非root用户,然后安装Nginx和MySQL。接着,设置Node.js环境,下载Nodesource GPG密钥并安装Node.js 18.x。之后,使用`npm`安装Ghost-CLI,创建Ghost安装目录并进行安装。配置过程中需提供博客URL、数据库连接信息等。最后,测试访问前台首页和后台管理页面。确保DNS设置正确,并根据提示完成Ghost博客的配置。
在阿里云Ubuntu 20.04服务器中搭建一个 Ghost 博客
|
27天前
|
存储 弹性计算 数据可视化
要将ECS中的文件直接传输到阿里云网盘与相册(
【2月更文挑战第31天】要将ECS中的文件直接传输到阿里云网盘与相册(
413 4

热门文章

最新文章