本文摘自php中文网,作者PHP中文网,侵删。
1 2 3 4 5 6 7 8 9 10 11 12 13 | python通过paramiko实现,ssh功能
import paramiko
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(hostname = '192.168.11.51' ,port = 22 ,username = 'yjj' ,password = 'yjj' )
stdin,stdout,stderr = ssh.exec_command( 'df' )
result = stdout.read()
result = result.decode()
print (result)
ssh.close()
|
ftp功能
1 2 3 4 5 6 7 | import paramiko
transport = paramiko.Transport(( '192.168.11.50' , 22 ))
transport.connect(username = 'root' ,password = 'yjj' )
sftp = paramiko.SFTPClient.from_transport(transport)
sftp.put( 'test2' , '/home/yjj/test2' )
sftp.get( '/home/yjj/test1' , 'test1' )
|
为了安全起见,不使用明文密码,采用 RSA 非对称密钥自动登陆
在linux下:生成密钥

传输到要登陆的服务器端:

传输成功就可以成功登陆服务器上对应的用户
如果是windows登陆linux
可以将私钥复制到windows下
通过paramiko.RSAKey指定私钥进行访问
ssh功能:
1 2 3 4 5 6 7 8 9 10 | import paramiko
priv_key = paramiko.RSAKey.from_private_key_file( 'id_rsa' )
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(hostname = '192.168.11.50' ,port = 22 ,username = 'root' ,pkey = priv_key)
stdin,stdout,stderr = ssh.exec_command( 'df' )
result = stdout.read()
result = result.decode()
print (result)
ssh.close()
|
ftp功能:
1 2 3 4 5 6 7 8 9 10 11 | import paramiko
priv_key = paramiko.RSAKey.from_private_key_file( 'id_rsa' )
transport = paramiko.Transport(( '192.168.11.50' , 22 ))
transport.connect(username = 'root' ,pkey = priv_key)
sftp = paramiko.SFTPClient.from_transport(transport)
sftp.put( 'test2' , '/home/yjj/test2-2' )
sftp.get( '/home/yjj/test1' , 'test1-2' )
with open ( 'test1-2' , 'r' ,encoding = 'utf-8' ) as f:
s = f.readlines()
print (s)
|
以上就是python学习日记(50)--paramiko的详细内容,更多文章请关注木庄网络博客!!
相关阅读 >>
一起看看Python常用字符串及其操作
Python day05 Python 环境变量和import模块导入
Python的除法运算符是什么意思
Python中文件的读取和写入操作
Python如何实现猜数字游戏
pytho 中闭包与装饰器详解
Python怎么读音
Python线程下queue(队列)模块的用法(附实例)
Python语言能做什么工作
深入理解Python中多重继承
更多相关阅读请进入《Python》频道 >>
人民邮电出版社
python入门书籍,非常畅销,超高好评,python官方公认好书。
转载请注明出处:木庄网络博客 » python学习日记(50)--paramiko