加载中…
个人资料
麦兜搞IT
麦兜搞IT
  • 博客等级:
  • 博客积分:0
  • 博客访问:1,533,161
  • 关注人气:531
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
相关博文
推荐博文
谁看过这篇博文
加载中…
正文 字体大小:

paramiko ssh sftp(转)

(2010-07-07 21:57:32)
标签:

python

paramiko

ssh

it

分类: Python

http://segfault.in/2010/03/paramiko-ssh-and-sftp-with-python/

 

Working with paramiko

 

SSHClient is the main class provided by the paramkio module. It provides the basic interface you are going to want to use to instantiate server connections. The above code creates a new SSHClient object, and then calls ”connect()” to connect us to the local SSH server.

Here’s a simple example:

1

import paramiko

2

ssh = paramiko.SSHClient()

Another way is to use an SSH key:

1

import paramiko

2

import os

3

privatekeyfile = os.path.expanduser('~/.ssh/id_rsa')

4

mykey = paramiko.RSAKey.from_private_key_file(privatekeyfile)

5

ssh.connect('192.168.1.2', username = 'vinod', pkey = mykey)

注意:(这里的key,用的是RSAkey,我们在用ssh-keygen -t rsa来指定它,才可以在这里用,否则将会报无法识别的RSA KEY。而且如果你的RSA Key有密码的话,你还需要

mykey = paramiko.RSAKey.from_private_key_file(privatekeyfile,password='12345678')

不过,我们可以用publickey来登录的。

解法如下:

serverHost = "127.0.0.1"

serverPort = 22

userName = "root"

keyFile = "~/.ssh/badboy"

known_host = "~/.ssh/known_hosts"

channel = paramiko.SSHClient();

channel.load_system_host_keys( known_host )

channel.connect( serverHost, serverPort,username = userName, key_filename = keyFile )


Running Simple Commands

Lets run some simple commands on a remote machine.

1

import paramiko

2

ssh = paramiko.SSHClient()

3

ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) <=这样的话,就会报paramiko.SSHException: Unknown server

4

ssh.connect('beastie', username='vinod', password='secret')

5

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

6

print stdout.readlines()

7

ssh.close()

“paramiko.AutoAddPolicy()” which will auto-accept unknown keys.

 

Using sudo in running commands:

01

import paramiko

02

03

cmd    = "sudo /etc/rc.d/apache2 restart"

04

05

ssh    = paramiko.SSHClient()

06

ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())

07

ssh.connect('beastie', username='vinod', password='secret')

08

stdin, stdout, stderr = ssh.exec_command(cmd)

09

stdin.write('secret\n')

10

stdin.flush()

11

print stdout.readlines()

12

ssh.close()

在这个例子中,无法运行,也无法解释,希望志同道合的朋友能给个解释!

Secure File Transfer Using SFTPClient

SFTPClient is used to open an sftp session across an open ssh Transport and do remote file operations.

An SSH Transport attaches to a stream (usually a socket), negotiates an encrypted session, authenticates, and then creates stream tunnels, called Channels, across the session. Multiple channels can be multiplexed across a single session (and often are, in the case of port forwardings).

以下是用密码认证功能登录的

#!/usr/bin/env python

import paramiko

socks=('127.0.0.1',22)

testssh=paramiko.Transport(socks)

testssh.connect(username='root',password='000000')

sftptest=paramiko.SFTPClient.from_transport(testssh)

remotepath="/tmp/a.log"

localpath="/tmp/c.log"

sftptest.put(remotepath,localpath)

sftptest.close()

testssh.close()

 

以下是用DSA认证登录的(PubkeyAuthentication)
#!/usr/bin/env python

import paramiko

 

serverHost = "192.168.1.172"

serverPort = 22

userName = "root"

keyFile = "/root/.ssh/zhuzhengjun"

known_host = "/root/.ssh/known_hosts"

channel = paramiko.SSHClient();

#host_keys = channel.load_system_host_keys(known_host)

channel.set_missing_host_key_policy(paramiko.AutoAddPolicy())

channel.connect(serverHost, serverPort,username=userName, key_filename=keyFile )

testssh=paramiko.Transport((serverHost,serverPort))

mykey = paramiko.DSSKey.from_private_key_file(keyFile,password='xyxyxy')

testssh.connect(username=userName,pkey=mykey)

sftptest=paramiko.SFTPClient.from_transport(testssh)

filepath='/tmp/e.log'

localpath='/tmp/a.log'

sftptest.put(localpath,filepath)

sftptest.close()

testssh.close()

 

以下是用RSA Key认证登录的

#!/usr/bin/evn python

 

import os

import paramiko

 

host='127.0.0.1'

port=22

testssh=paramiko.Transport((host,port))

privatekeyfile = os.path.expanduser('~/.ssh/badboy')

mykey = paramiko.RSAKey.from_private_key_file(privatekeyfile,password='000000')

username = 'root'

testssh.connect(username=username, pkey=mykey)

sftptest=paramiko.SFTPClient.from_transport(testssh)

filepath='/tmp/e.log'

localpath='/tmp/a.log'

sftptest.put(localpath,filepath)

sftptest.close()

testssh.close()

 

0

阅读 评论 收藏 转载 喜欢 打印举报/Report
  • 评论加载中,请稍候...
发评论

    发评论

    以上网友发言只代表其个人观点,不代表新浪网的观点或立场。

      

    新浪BLOG意见反馈留言板 电话:4000520066 提示音后按1键(按当地市话标准计费) 欢迎批评指正

    新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 会员注册 | 产品答疑

    新浪公司 版权所有