Learning Python Paramiko-->one

Source: Internet
Author: User

1.1 Introduction:

Paramiko is a module written in the Python language that follows the SSH2 protocol and supports the connection of remote servers in a way that is encrypted and authenticated.

All Python-supported platforms, such as Linux, Solaris, BSD, MacOS X, Windows, and so on, are supported by the use of Python, which can run across platforms, so Paramiko Paramiko is one of the best tools when you need to use SSH to connect to another platform from one platform and perform a series of operations.

1.2 Installation:

There are two prerequisites for installing Paramiko, Python and another module named Pycrypto.

The standard Python module is usually installed and only needs to be run in the root directory of the module:

Python setup.py Build

Python setup.py Install

Note: Check the installation of GCC (yum-y install gcc) before installing

2.1 Pycrypto Installation

wget http://ftp.dlitz.net/pub/dlitz/crypto/pycrypto/pycrypto-2.6.tar.gz

TAR-ZXVF pycrypto-2.6.tar.gz

CD pycrypto-2.6/

Python setup.py build && python setup.py install

Test:

Python>> Import Crypto

(Compile times Error:error:command ' gcc ' failed with exit status 1; This is due to a missing Python-dev package, yum-y install Python-devel )

2.2 Paramiko Installation

wget http://www.lag.net/paramiko/download/paramiko-1.7.7.1.tar.gz

Tar xvzf paramiko-1.7.7.1.tar.gz

CD PARAMIKO-1.7.7.1/

Python setup.py build && python setup.py install

Crypto error: ' module ' object has no attribute ' have_decl_mpz_powm_sec '

Test:

Python>> Import Paramiko

(Crypto Error: ' Module ' object has no attribute ' have_decl_mpz_powm_sec '

Find/usr/lib/python2.7/site-packages/crypto/util/number.py

Put if _fastmath is isn't None and not _fastmath. Have_decl_mpz_powm_sec:

Commented on

#if _fastmath is not None and not _fastmath. Have_decl_mpz_powm_sec:

3. Use 3.1 to execute remote command

import Paramiko

ssh = Paramiko. Sshclient ()

ssh.set_missing_host_key_policy (Paramiko. Autoaddpolicy ())

ssh.connect ("One IP Address", 22, "username", "password")

stdin, stdout, stderr = Ssh.exec_command ("Your Command")

print stdout.readlines ()

ssh.close ()

3.2 Uploading files to remote

#!/usr/bin/python

Import Paramiko

t = Paramiko. Transport (("An IP address", 22))

T.connect (username = "username", password = "password")

SFTP = Paramiko. Sftpclient.from_transport (t)

Remotepath= '/tmp/test.txt '

Localpath= '/tmp/test.txt '

Sftp.put (Localpath,remotepath)

T.close ()

3.3 Downloading files from remote

#!/usr/bin/python

Import Paramiko

t = Paramiko. Transport (("An IP address", 22))

T.connect (username = "username", password = "password")

SFTP = Paramiko. Sftpclient.from_transport (t)

Remotepath= '/tmp/test.txt '

Localpath= '/tmp/test.txt '

Sftp.get (RemotePath, LocalPath)

T.close ()




This article is from the "Heart of the Past" blog, please be sure to keep this source http://jwlin.blog.51cto.com/7560932/1787882

Learning Python Paramiko-->one

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.