Python learning - paramiko simple use

    paramiko is a python-based ssh2 remote secure connection that supports authentication and key methods. It can realize remote command execution, file transfer, intermediate ssh agent and other functions.

Install:

pip install paramiko或 easy_install paramiko

paramiko relies on 3rd party Crypto, Ecdsa and pyhton-devel

Core components:

SSHclient class
method:

connect(): remote ssh connection and verification

parameter:

  • hostname The target host to connect to

  • port=SSH_PORT specifies the port

  • username=None authenticated username

  • password=None authenticated user password

  • pkey=None private key method is used for authentication

  • key_filename=None A filename or list of filenames, specifying the private key file

  • timeout=None optional tcp connection timeout

  • allow_agent=True whether to allow connections to the ssh agent, the default is True to allow

  • look_for_keys=True whether to search for private key files in ~/.ssh, the default is True to allow

  • compress=False whether to turn on compression

  • sock=None

  • gss_auth=False

  • gss_kex = False

  • gss_deleg_creds=True

  • gss_host=None

  • banner_timeout=None

 

exec_command(): used to execute commands remotely, the input and output streams of the command are standard input, marked output, and standard error output

parameter:

  • command the command to execute

  • bufsize=-1 file buffer size

  • timeout=None set timeout

  • get_pty=False

 

load_system_host_key(): Load the system public key, the default is ~/.ssh/known_hosts

parameter:

  • filename=None specifies the local public key file

 

set_missing_host_key_policy(): Sets the policy when the connected remote host does not have a local host key or HostKeys object. Currently, three are supported, that is, there are only three parameters.

parameter:

  • AutoAddPolicy 自动添加主机名及主机密钥到本地的known_hosts,不依赖load_system_host_key的配置。即新建立ssh连接时不需要再输入yes或no进行确认

  • WarningPolicy 用于记录一个未知的主机密钥的python警告。并接受,功能上和AutoAddPolicy类似,但是会提示是新连接

  • RejectPolicy 自动拒绝未知的主机名和密钥,依赖load_system_host_key的配置。此为默认选项

  用法:
  set_missing_host_key_policy(paramiko.AutoAddPolicy())

SFTPClient类

SFTPCLient作为一个sftp的客户端对象,根据ssh传输协议的sftp会话,实现远程文件操作,如上传、下载。

方法:

  • from_transport(cls,t) 创建一个已连通的SFTP客户端通道

  • put(localpath, remotepath, callback=None, confirm=True) 将本地文件上传到服务器 参数confirm:是否调用stat()方法检查文件状态,返回ls -l的结果

  • get(remotepath, localpath, callback=None) 从服务器下载文件到本地

  • mkdir() 在服务器上创建目录

  • remove() 在服务器上删除目录

  • rename() 在服务器上重命名目录

  • stat() 查看服务器文件状态

  • listdir() 列出服务器目录下的文件


使用ssh运行命令,采用明文密码认证方式,通过exec_command()方法执行命令,示例如下

#!/usr/bin/env python
#coding=utf-8
 import paramiko
 
 
 hostname='172.31.101.61'
 username='root'
 password='abc@123'
 paramiko.util.log_to_file('syslogin.log') #发送paramik日志到syslogin.log文件
 
 ssh=paramiko.SSHClient()   #创建一个ssh客户端client对象
 ssh.load_system_host_keys()   #获取客户端host_keys,默认~/.ssh/known_hosts,非默认路径需指定
 ssh.connect(hostname=hostname,username=username,password=password)  #创建ssh连接
 stdin,stdout,stderr=ssh.exec_command('free -m')   #调用远程执行命令方法exec_command()
 print stdout.read()    #打印命令执行结果,得到python列表形式,可以用stdout.readlinses()
 ssh.close()    #关闭ssh连接

image.png

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325302030&siteId=291194637
Recommended