首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > perl python >

Paramiko: SSH and SFTP With Python的装配

2012-06-26 
Paramiko: SSH and SFTP With Python的安装一、MinGW的安装与配置?1.下载MinGW.??? 登陆网址http://sourcef

Paramiko: SSH and SFTP With Python的安装

一、MinGW的安装与配置

?

1.下载MinGW.

??? 登陆网址http://sourceforge.net/projects/mingw/files/,下载在线安装程序。

2.设置环境变量

?? 计算机->右键属性->高级设置->环境变量->系统环境变量,然后添加以下几个环境变量

?? MINGW=c:\MinGW????????????????? //MInGW的根目录?

?? MINGW_HOME=C:\MinGW?? //MInGW的根目录

?? C_INCLUDE_PATH=%MINGW_HOME%\include;C:\MinGW\lib\gcc\mingw32\4.5.2\include\? //gcc头文件位置

?? CPLUS_INCLUDE_PATH=C:\MinGW\include\;C:\MinGW\;lib\gcc\mingw32\4.5.2\include;?????? //c++头文件位置

??? Path=原来的path;;%MINGW_HOME%\bin;C:\MinGW\libexec\gcc\mingw32\4.5.2????????//将gcc,g++等命令引入到环境变量中

??? LIBRARY_PATH=%MINGW_HOME%\lib;C:\MinGW\lib\gcc\mingw32\4.5.2???????????????? //gcc,g++需要用到lib库

注:以下3~5点,大多数场景可忽略。

3.万事大吉了,点击 开始->所有程序->MinGW->MinGW shell,写个hello word测试下

4. 有时候使用MinGW编译一些开源项目会找不到某些头文件,只需要再c:/MinGW下搜索对应的头文件,然后将文件所在的

?? 路径加入到C_INCLUDE_PATH,和CPLUS_INCLUDE_PATH中就好了。

5.可以结合Eclipse来做开发,Eclipse下载地址

?? http://www.eclipse.org/downloads/。选择C/C++版本就好

?? 当然,想运行Eclipse要先安装java,下载地址

?? http://java.com/zh_CN/。只下载jre就好,不做JAVA开发的话没必要下载JDK.

?

二、 windows 2003/ WIN7 的64bits版本的paramiko的安装流程:

?

1. 首先确认安装的python的版本,不要相信下载的activepython的标注的版本号,需要在命令行中敲入python -V的命令。因为我就是因为activepython版本号的误导,而下载的pycrpto版本不对,搞了好久,郁闷死了。

2. 如果不想要安装vs2008这个大家伙的话,那么还是请使用MinGW吧,然后把安装路径导入到环境变量中。

3. 下载正确的pycrpto版本在这个地址:http://www.voidspace.org.uk/python/modules.shtml

4. 去下载paramiko的压缩包,http://www.lag.net/paramiko/

5. 去解压路径,打开命令行,敲入"python setup.py install build --compiler=mingw32"回车,就应该可以使用了。

?

三、Paramiko?例子

?

Paramiko?is a module for?python 2.2?(or higher) that implements the SSH2 protocol for secure (encrypted and authenticated) connections to remote machines.

Emphasis is on using SSH2 as an alternative to SSL for making secure connections between python scripts. All major ciphers and hash methods are supported. SFTP client and server mode are both supported too.

Installing paramiko

First, we need to install paramiko, if you don’t have it already.

On Ubuntu/Debian:

$ sudo apt-get install python-paramiko

On Gentoo Linux:

$ emerge paramiko

Or install from source:

$ wget http://www.lag.net/paramiko/download/paramiko-1.7.6.tar.gz $ tar xzf paramiko-1.7.6.tar.gz $ cd paramiko-1.7.6 $ python setup.py build $ su -c "python setup.py install"
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:

import paramiko ssh = paramiko.SSHClient() ssh.connect('192.168.1.2', username='vinod', password='screct')

Another way is to use an SSH key:

import paramiko import os privatekeyfile = os.path.expanduser('~/.ssh/id_rsa') mykey = paramiko.RSAKey.from_private_key_file(privatekeyfile) ssh.connect('192.168.1.2', username = 'vinod', pkey = mykey)
Running Simple Commands

Lets run some simple commands on a remote machine.

import paramiko ? ssh = paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ssh.connect('beastie', username='vinod', password='secret') stdin, stdout, stderr = ssh.exec_command('df -h') print stdout.readlines() ssh.close()

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

Using sudo in running commands:

import paramiko ? cmd    = "sudo /etc/rc.d/apache2 restart" ? ssh    = paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ssh.connect('beastie', username='vinod', password='secret') stdin, stdout, stderr = ssh.exec_command(cmd) stdin.write('secret\n') stdin.flush() print stdout.readlines() 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).

First we will create a Transport

import paramiko import os privatekeyfile = os.path.expanduser('~/.ssh/id_rsa') mykey = paramiko.RSAKey.from_private_key_file(privatekeyfile) username = 'vinod' transport.connect(username = username, pkey = mykey)

Now we can start the SFTP client:

sftp = paramiko.SFTPClient.from_transport(transport)

Now lets pull a file across from the remote to the local system:

remotepath='/var/log/system.log' localpath='/tmp/system.log' sftp.get(remotepath, localpath)

Now lets push a file to remote system:

remotepath='/var/www/images/file.png' localpath='/tmp/file.png' sftp.put(remotepath, localpath)

Finally, close the SFTP connection and the transport:

sftp.close() transport.close()

热点排行