day9---paramiko ssh ftp

安装 paramiko模块

win下:

进入到\Python35\Scripts>

执行:pip install paramiko

Linux:

先升级下pip :

pip3.5 install --upgrade pip

然后安装paramiko

pip3.5 install paramiko

Downloading paramiko-2.0.2-py2.py3-none-any.whl

Downloading cryptography-1.5.tar.gz

Downloading pyasn1-0.1.9-py2.py3-none-any.whl

Downloading idna-2.1-py2.py3-none-any.whl

Downloading six-1.10.0-py2.py3-none-any.whl

Downloading cffi-1.8.3.tar.gz 

Downloading pycparser-2.14.tar.gz

Downloading cffi-1.8.3-cp35-cp35m-manylinux1_x86_64.whl
>>> import paramiko
>>> 
代表成功

paramiko实现ssh、scp

SSHClient 用于连接远程服务器并执行基本命令

#!/usr/bin/env python
#-*- coding:utf-8 -*-
# Author:DCC

import paramiko

# 创建SSH对象
ssh = paramiko.SSHClient()
# 允许连接不在know_hosts文件中的主机
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
# 连接服务器
ssh.connect(hostname='192.168.193.128', port=22, username='root', password='123dc.')
# 执行命令
stdin, stdout, stderr = ssh.exec_command('df')
# 获取命令结果
#result = stdout.read()
res,err = stdout.read(),stderr.read()
#print(result.decode())
print(res.decode())
print(err.decode())
# 关闭连接
ssh.close()

#执行结果
C:\Users\congcong.du\AppData\Local\Programs\Python\Python35\python.exe C:/Users/congcong.du/PycharmProjects/test/day9/paramiko_test/ssh_test.py
Filesystem           1K-blocks    Used Available Use% Mounted on
/dev/mapper/VolGroup-lv_root
                       8649736 8152208     51476 100% /
tmpfs                   502068       0    502068   0% /dev/shm
/dev/sda1               487652  128958    333094  28% /boot

SFTPClient 用于连接远程服务器并执行上传下载

#!/usr/bin/env python
#-*- coding:utf-8 -*-
# Author:DCC

import paramiko

transport = paramiko.Transport(('localhost', 22))
transport.connect(username='root', password='123dc.')

sftp = paramiko.SFTPClient.from_transport(transport)
# 将location.py 上传至服务器 /tmp/test.py
sftp.put('/tmp/location.py', '/tmp/test.py')
# 将remove_path 下载到本地 local_path
sftp.get('remove_path', 'local_path')
transport.close()

基于公钥密钥连接

服务器无密码验证

&&&&&&&&&&&&&&
#ssh-keygen -t rsa 
连着三个回车
#ssh-copy-id -i root@119.40.36.231 
输入密码,回车。
哦了。
&&&&&&&&&&&&&&

基于公钥链接

import paramiko

private_key = paramiko.RSAKey.from_private_key_file('/home/auto/.ssh/id_rsa')

# 创建SSH对象

ssh = paramiko.SSHClient()

# 允许连接不在know_hosts文件中的主机

ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())

# 连接服务器

ssh.connect(hostname='c1.salt.com', port=22, username='wupeiqi', key=private_key)

# 执行命令

stdin, stdout, stderr = ssh.exec_command('df')

# 获取命令结果

result = stdout.read()

# 关闭连接

ssh.close()

基于公钥ftp

import paramiko

private_key = paramiko.RSAKey.from_private_key_file('/home/auto/.ssh/id_rsa')

transport = paramiko.Transport(('hostname', 22))

transport.connect(username='wupeiqi', pkey=private_key )

sftp = paramiko.SFTPClient.from_transport(transport)

# 将location.py 上传至服务器 /tmp/test.py
sftp.put('/tmp/location.py', '/tmp/test.py')

# 将remove_path 下载到本地 local_path
sftp.get('remove_path', 'local_path')

transport.close()

 

posted on 2016-09-18 19:18  DCC_python  阅读(799)  评论(0编辑  收藏  举报