python自动化运维六:paramiko
paramiko是基于python实现的SSH2远程安全连接,支持认证以及密钥方式,可以实现远程命令执行,文件传输,中间SSH代理等功能。也就是采用SSH的方式进行远程访问。SSH登陆的方式可以参考之前的一片帖子:http://www.cnblogs.com/zhanghongfeng/p/7749489.html
下面来看一个远程登陆的例子如下:
def paramiko_function_try():
hostname="192.168.0.9"
username='root'
password='root'
paramiko.util.log_to_file('syslogin.log')
ssh=paramiko.SSHClient()
ssh.load_system_host_keys()
ssh.connect(hostname=hostname,username=username,password=password)
stdin,stdout,stderr=ssh.exec_command('ls -al')
print stdout.read()
ssh.close()
执行结果:
total 64
drwx------ 10 root root 4096 Oct 29 10:02 .
drwxr-xr-x 22 root root 4096 Jul 9 16:59 ..
-rw------- 1 root root 4143 Oct 29 10:14 .bash_history
-rw-r--r-- 1 root root 3106 Feb 20 2014 .bashrc
drwx------ 5 root root 4096 Aug 31 21:47 .cache
drwx------ 4 root root 4096 Jul 26 10:47 .config
drwx------ 3 root root 4096 Jul 9 15:59 .dbus
drwx------ 2 root root 4096 Jul 9 16:23 .gvfs
drwxr-xr-x 3 root root 4096 Jul 26 11:23 .local
drwxr-xr-x 2 root root 4096 Jul 26 11:33 .pip
-rw-r--r-- 1 root root 140 Feb 20 2014 .profile
drwxr-xr-x 3 root root 4096 Jul 26 11:41 .python-eggs
drwx------ 2 root root 4096 Oct 27 23:11 .ssh
-rw-r--r-- 1 root root 0 Oct 29 10:02 test.txt
-rw------- 1 root root 5436 Oct 27 22:11 .viminfo
从syslogin.log中也可以查看整个建链的过程。
DEB [20171029-20:41:29.564] thr=1 paramiko.transport: Kex agreed: ecdh-sha2-nistp256
DEB [20171029-20:41:29.564] thr=1 paramiko.transport: HostKey agreed: ecdsa-sha2-nistp256
DEB [20171029-20:41:29.565] thr=1 paramiko.transport: Cipher agreed: aes128-ctr
DEB [20171029-20:41:29.565] thr=1 paramiko.transport: MAC agreed: hmac-sha2-256
DEB [20171029-20:41:29.565] thr=1 paramiko.transport: Compression agreed: none
DEB [20171029-20:41:29.627] thr=1 paramiko.transport: kex engine KexNistp256 specified hash_algo <built-in function openssl_sha256>
DEB [20171029-20:41:29.628] thr=1 paramiko.transport: Switch to new keys ...
DEB [20171029-20:41:29.639] thr=2 paramiko.transport: Trying discovered key 267fb51feeeaf45abbf324467ee574d8 in /root/.ssh/id_rsa
DEB [20171029-20:41:29.676] thr=1 paramiko.transport: userauth is OK
INF [20171029-20:41:29.808] thr=1 paramiko.transport: Authentication (publickey) successful!
DEB [20171029-20:41:29.841] thr=2 paramiko.transport: [chan 0] Max packet in: 32768 bytes
DEB [20171029-20:41:30.281] thr=1 paramiko.transport: [chan 0] Max packet out: 32768 bytes
DEB [20171029-20:41:30.281] thr=1 paramiko.transport: Secsh channel 0 opened.
DEB [20171029-20:41:30.330] thr=1 paramiko.transport: [chan 0] Sesch channel 0 request ok
DEB [20171029-20:41:30.356] thr=1 paramiko.transport: [chan 0] EOF received (0)
DEB [20171029-20:41:30.357] thr=1 paramiko.transport: EOF in transport thread
接下来介绍下connect方法中的参数:
hostname: 连接的目标主机地址
port:端口,默认为22
username:校验的用户名
password: 密码用于身份校验或解锁私钥
pkey:私钥方式用于身份验证
key_filename:一个文件名或文件名的列表,用于私钥的身份验证
timeout: 可选的超时时间的TCP连接
allow_agent: 设置为false用于禁用连接到SSH代理
look_for_keys:设置为False用来禁用在~/.ssh中搜索私钥文件
compress:设置为True时打开压缩。
前面只是连接到了远程电脑并执行命令,如果要上传下载文件的话还是要采用SFTP的方法。
示例代码如下,首先要创造一个已连通的SFTP客户端通道。然后采用put上传get下载的方法进行文件的上传和下载。注意的是put的时候本地路径为第一个参数,远端路径为第二个参数。get的时候远端路径为第一个参数,本地路径为第二个参数。
def SFTP_function_try():
t=paramiko.Transport(("192.168.0.9",22))
t.connect(username='root',password='root')
sftp=paramiko.SFTPClient.from_transport(t)
localpath='/home/zhf/zhf/python_prj/auto_manintance/syslogin.log'
remotepath='/home/zhf/syslogin.log'
sftp.put(localpath,remotepath)
localpath='/home/zhf/zhf/python_prj/auto_manintance/log.log'
remotepath='/home/root'
sftp.get(remotepath,localpath)
在前面介绍SSH的时候讲过免密码登陆的方式,现在我们来看下通过paramiko如何免密码,通过证书登陆
示例代码如下:
def paramiko_function_auto():
hostname="192.168.0.9"
username='root'
password='root'
paramiko.util.log_to_file('syslogin.log')
ssh=paramiko.SSHClient()
ssh.load_system_host_keys()
privatekey=os.path.expanduser('~/.ssh/id_rsa')
key=paramiko.RSAKey.from_private_key_file(privatekey)
ssh.connect(hostname=hostname,username=username,pkey=key)
stdin,stdout,stderr=ssh.exec_command('ls -al')
print stdout.read()
ssh.close()
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架