版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/chengqiuming/article/details/88045252
一 实战——SFTPClient类应用
1 说明
本示例为SFTPClient类的一个完整示例,实现了文件上传、下载、创建与删除目录等,需要注意的是,put和get方法需要指定文件名,不能省略。
2 代码
#coding=utf-8
#! /usr/bin/env python
import paramiko
username = "root"
password = "123456"
hostname = "192.168.0.120"
port = 22
try:
t = paramiko.Transport((hostname,port))
t.connect(username=username,password=password)
sftp = paramiko.SFTPClient.from_transport(t)
sftp.put("2_2_1.py","/home/temp.py") #上传文件
sftp.get("/home/temp.py", "temp.py") #下载文件
sftp.mkdir("/home/userdir",0755) #创建目录
sftp.rmdir("/home/userdir") #删除目录
sftp.rename("/home/temp.py","/home/temp2.py") #文件重命名
print sftp.stat("/home/temp2.py") #打印文件信息
print sftp.listdir("/home") #打印目录列表
t.close()
except Exception,e:
print "some wrong!"
3 结果
E:\Python\python_auto_maintain\venv\Scripts\python.exe E:/Python/python_auto_maintain/6_2_2.py
-rw-r--r-- 1 0 0 824 27 Feb 21:44 ?
[u'cakin24', u'pyauto-master', u'clamav', u'test.txt', u'test.tar.gz', u'temp2.py']
Process finished with exit code 0
二 实战——实现密钥方式登录远程主机
1 点睛
实现自动密钥登录方式。
第一步:需要配置与目标设备的密钥认证支持。
[root@localhost home]# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
/root/.ssh/id_rsa already exists.
Overwrite (y/n)? y
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:9/pGNxnQVWAWpss7PYtJcUDyHsCexgYY6NGWy/oOhTg [email protected]
The key's randomart image is:
+---[RSA 2048]----+
| o.+ .o ..*++|
| o = . .=.=. |
| . + . + .=. |
| ...o *o +. |
| E ... So. = .o |
| ... . ..=+ |
| .. .=.o. |
| .. o.+ o |
| .. .o+ . |
+----[SHA256]-----+
私钥文件可以存放在默认路径“~/.ssh/id_rsa”。
第二步:接下来同步公钥文件id_rsa.pub到目标主机,推荐使用ssh-copy-id公钥拷贝工具
[root@localhost home]# ssh-copy-id -i /root/.ssh/id_rsa.pub [email protected]
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host '192.168.0.101 (192.168.0.101)' can't be established.
ECDSA key fingerprint is SHA256:yCwrpGtjs8IILCWCLRqO3p05DPFWTZp/2iCEimgj1uU.
ECDSA key fingerprint is MD5:e8:13:79:55:21:e3:40:67:f2:fd:5d:76:ab:4a:88:86.
Are you sure you want to continue connecting (yes/no)? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
\S
Kernel \r on an \m
[email protected]'s password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh '[email protected]'"
and check to make sure that only the key(s) you wanted were added.
2 代码
#coding=utf-8
#!/usr/bin/env python
import paramiko
import os
hostname='192.168.0.101'
username='root'
paramiko.util.log_to_file('syslogin.log')
ssh=paramiko.SSHClient()
ssh.load_system_host_keys()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
privatekey = os.path.expanduser('/root/.ssh/id_rsa') #定义私钥存放路径
key = paramiko.RSAKey.from_private_key_file(privatekey) #创建私钥对象key
ssh.connect(hostname=hostname,username=username,pkey = key)
stdin,stdout,stderr=ssh.exec_command('free -m')
print stdout.read()
ssh.close()
3 结果
[root@localhost paramiko]# python simple5.py
total used free shared buff/cache available
Mem: 993 289 461 6 242 561
Swap: 4095 0 4095