paramiko库实现远程登录主机上传下载

少年击剑更吹箫,剑气箫心一例消。这篇文章主要讲述paramiko库实现远程登录主机上传下载相关的知识,希望能为你提供帮助。


      paramiko 模块是基于 python 实现的 SSH 远程安全连接,用于 SSH 远程执行命令、文件传输等功能。

      默认 Python 没有,需要手动安装: pip install paramiko。如安装失败,可以尝试 yum 安装: yum install python-paramiko
安装完成后,可以正常导入,如下图:



1. 登录远程机器执行命令
1.1 登录单台机器执行命令

1.1.1 脚本名:myssh.py

#!/usr/bin/python
# -*- coding: utf-8 -*-
import paramiko
import sys
ip=sys.argv[1]
cmd=sys.argv[2]
transport = paramiko.Transport( ip, 22 )
pkey = paramiko.RSAKey.from_private_key_file( /root/.ssh/id_rsa )
transport.connect( username = root, pkey = pkey )
ssh = paramiko.SSHClient()
ssh._transport = transport
stdio, stdout, stderr = ssh.exec_command( cmd )
channel = stdout.channel
status = channel.recv_exit_status()
stdout = stdout.read()
stderr = stderr.read()

ssh.close()
transport.close()

print( "stdout is : " + "\\n" + stdout )
print( "stderr is " + stderr )
print( "status is " + str(status) )



1.1.2 脚本传参解释ip=sys.argv[1]  # 传入第一个参数ip
cmd=sys.argv[2] # 传入第二个参数要执行的命令
1.1.3 执行python myssh.py 192.168.0.168 "free -g"
1.1.4 执行结果如下


1.2 登录多台机器执行命令1.2.1 脚本名:myfunc.py
#!/usr/bin/python
# -*- coding: utf-8 -*-
import paramiko
def myCommand(ip, port, username, cmd):
transport = paramiko.Transport(ip, port)
pkey = paramiko.RSAKey.from_private_key_file( /root/.ssh/id_rsa )
transport.connect( username = username, pkey = pkey )
ssh = paramiko.SSHClient()
ssh._transport = transport
stdio, stdout, stderr = ssh.exec_command( cmd )
channel = stdout.channel
status = channel.recv_exit_status()
stdout = stdout.read()
stderr = stderr.read()
#print( "stdout is " + stdout )
return "ip": ip, "status": status, "stdout": stdout, "stderr": stderr

ssh.close()
transport.close()
def myOpen(filename):
with open (filename, "r") as f:
for i in f:
result = myCommand( i , 22, root, "free -g|grep Mem|awk print $2" )
print result


if __name__ == "__main__":
myOpen("/opt/software/test_python/test/ips")

1.2.2 脚本解释封装了2个函数,myCommand和myOpen
myCommand(ip, port, username, cmd)  #传入要执行的ip、端口、用户名和命令,返回了字典,字典值可以自定义
myOpen(filename)    #传入要执行的ip列表



1.2.3 执行执行:python myfunc.py
1.2.4 执行结果如下


2. 实现文件上传
2.1 脚本名:myput.py
#!/usr/bin/python
# -*- coding: utf-8 -*-
import paramiko
import sys
ip=sys.argv[1]
source_file=sys.argv[2]
des_file=sys.argv[3]
transport = paramiko.Transport( ip, 22 )
pkey = paramiko.RSAKey.from_private_key_file( /root/.ssh/id_rsa )
transport.connect( username = root, pkey = pkey )

sftp = paramiko.SFTPClient.from_transport( transport )
result = sftp.put(source_file, des_file)

sftp.close()
transport.close()



2.2 脚本传参解释ip=sys.argv[1]    # 传入第一个参数ip
source_file=sys.argv[2]  #要上传的文件

des_file=sys.argv[3]    #上传的目的地
2.3 执行python myput.py 192.168.0.169 "/opt/software/test_python/test/ips" "/opt/ips"
2.4 执行结果如下





3. 实现文件下载
3.1 脚本名:myget.py
#!/usr/bin/python
# -*- coding: utf-8 -*-
import paramiko
import sys
ip=sys.argv[1]
source_file=sys.argv[2]
des_file=sys.argv[3]
transport = paramiko.Transport( ip, 22 )
pkey = paramiko.RSAKey.from_private_key_file( /root/.ssh/id_rsa )
transport.connect( username = root, pkey = pkey )

sftp = paramiko.SFTPClient.from_transport( transport )
result = sftp.get( source_file, des_file )

sftp.close()
transport.close()

3.2 脚本传参解释ip=sys.argv[1]    # 传入第一个参数ip
source_file=sys.argv[2]  #要下载的远程文件
des_file=sys.argv[3]    #下载到本地
3.3 执行myget.py 192.168.0.169 "/opt/ips" "/opt/software/test_python/test/ips"
3.4 执行结果如下












【paramiko库实现远程登录主机上传下载】


    推荐阅读