目录
简介
ssh是一个协议,OpenSSH是其中一个开源实现,paramiko是Python的一个库,实现了SSHv2协议(底层使用cryptography)。
有了Paramiko以后,我们就可以在Python代码中直接使用SSH协议对远程服务器执行操作,而不是通过ssh命令对远程服务器进行操作。
由于paramiko属于第三方库,所以需要使用如下命令先行安装
1 |
|
Paramiko介绍
paramiko包含两个核心组件:SSHClient和SFTPClient。
- SSHClient的作用类似于Linux的ssh命令,是对SSH会话的封装,该类封装了传输(Transport),通道(Channel)及SFTPClient建立的方法(open_sftp),通常用于执行远程命令。
- SFTPClient的作用类似与Linux的sftp命令,是对SFTP客户端的封装,用以实现远程文件操作,如文件上传、下载、修改文件权限等操作。
1 2 3 4 5 |
|
Paramiko的基本使用
SSHClient常用的方法介绍
connect():实现远程服务器的连接与认证,对于该方法只有hostname是必传参数。
1 2 3 4 5 6 7 8 9 10 11 |
|
set_missing_host_key_policy():设置远程服务器没有在know_hosts文件中记录时的应对策略。目前支持三种策略:
1 2 3 4 5 |
|
exec_command():在远程服务器执行Linux命令的方法。
open_sftp():在当前ssh会话的基础上创建一个sftp会话。该方法会返回一个SFTPClient对象。
1 2 3 4 |
|
SSHClient常用的方法举例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
|
密钥连接方式
1 2 3 4 5 6 7 8 9 10 11 |
|
SSHClient 封装 Transport
1 2 3 4 5 6 7 8 9 10 11 12 13 |
|
SFTPClient常用方法介绍
1 2 3 4 5 6 7 8 9 10 |
|
SFTPClient常用方法举例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
|
Paramiko与expect模块的交互使用
Paramiko Expect为Paramiko SSH库提供了一个类似于期望的扩展,该脚本允许脚本通过真正的SSH连接与主机进行完全交互。
该类由SSH客户端对象构造(将来可能会扩展为支持传输,以提高灵活性)。
# from pypi
pip install paramiko-expect
# from source
pip install git+https://github.com/fgimian/paramiko-expect.git
# Connect to the host
client.connect(hostname=hostname, username=username, password=password)
# Create a client interaction class which will interact with the host
interact = SSHClientInteraction(client, timeout=10, display=True)
interact.expect(prompt)
# Run the first command and capture the cleaned output, if you want the output
# without cleaning, simply grab current_output instead.
interact.send('uname -a')
interact.expect(prompt)
cmd_output_uname = interact.current_output_clean
# Now let's do the same for the ls command but also set a timeout for this
# specific expect (overriding the default timeout)
interact.send('ls -l /')
interact.expect(prompt, timeout=5)
cmd_output_ls = interact.current_output_clean
# To expect multiple expressions, just use a list. You can also selectively
# take action based on what was matched.
# Method 1: You may use the last_match property to find out what was matched
interact.send('~/paramiko_expect-demo-helper.py')
interact.expect([prompt, 'Please enter your name: '])
if interact.last_match == 'Please enter your name: ':
interact.send('Fotis Gimian')
interact.expect(prompt)
# Method 2: You may use the matched index to determine the last match (like pexpect)
interact.send('~/paramiko_expect-demo-helper.py')
found_index = interact.expect([prompt, 'Please enter your name: '])
if found_index == 1:
interact.send('Fotis Gimian')
interact.expect(prompt)
# Send the exit command and expect EOF (a closed session)
interact.send('exit')
interact.expect()
# Print the output of each command
print '-'*79
print 'Cleaned Command Output'
print '-'*79
print 'uname -a output:'
print cmd_output_uname
print 'ls -l / output:'
print cmd_output_ls
交互模式比如自定义的命令行模式,或者fdisk等需要交互的命令:
self.interact = SSHClientInteraction(self.client, timeout=10, display=True)
self.interact.send('/bin/ismcli')
self.interact.expect('Please input user name:', timeout=120)
self.interact.send(config.ICFS_USERNAME)
self.interact.expect('Please input password:', timeout=120)
self.interact.send(config.ICFS_PASSWORD)
self.interact.expect(prompt, timeout=120)
Paramiko的综合使用例子
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
|