The Python Paramiko module is capable of connecting to a remote host, executing commands on that host, and transferring files between the hosts. Paramiko supports login to remote host and secret key with plaintext password. Before using to install this module ha, pip install Paramiko, will not be installed Baidu ha.
(1) Password login
#! /usr/bin/env python#-*-coding:utf-8-*-# Author: "Liuyouyuan" # Date:2018/1/23import Paramikodef run_cmd (host, Port, US Er, passwd, cmd): ssh = Paramiko. Sshclient () ssh.set_missing_host_key_policy (Paramiko. Autoaddpolicy ()) Ssh.connect (Hostname=host, Port=port, Username=user, password=passwd) stdin, stdout, stderr = Ssh.exec_command (cmd) stdout = Stdout.read () stderr = Stderr.read () ssh.close () if not stderr : print (Stdout.decode ()) else: print (Stderr.decode ()) If __name__ = = ' __main__ ': HOST = " 10.129.205.151 " PORT = user =" root " PASSWD =" ****** " cmd =" df-h " run_cmd (HOST, PORT, user , PASSWD, CMD)
To see the results of the execution:
Filesystem Size used Avail use%mounted on/dev/mapper/cl-root 17G6.8G 11G +% /Devtmpfs 897M0897M0% /Devtmpfs 912M 84K 912M1%/dev/Shmtmpfs 912M9.0M903M1% /Runtmpfs 912M0912M0%/sys/fs/Cgroup/DEV/SDA1 1014M 173M 842M -% /Boottmpfs 183M 16K 183M1%/run/user/ theTmpfs 183M0183M0%/run/user/0
(2) secret key login
def run_cmd_pkey (host, port, user, Rsa_file): "" " Linux side creation key Ssh-keygen renaming the public key of the machine to be connected MV Id_rsa.pub Authorized_keys want to connect to which machine directly copy the public key to the machine user home format:/root/.ssh "" " Private_key = Paramiko. Rsakey.from_private_key_file (rsa_file) # Specifies the file where the private key is located ssh = Paramiko. Sshclient () # creates an SSH object ssh.set_missing_host_key_policy (Paramiko. Autoaddpolicy ()) # Allow connections to hosts that are not in the Know_hosts file ssh.connect (Hostname=host, Port=port, Username=user, pkey= Private_key) # Connection Server stdin, stdout, stderr = Ssh.exec_command ("df") # Execute command stdout = Stdout.read () C17/>stderr = Stderr.read () ssh.close () # Close Connection if not stderr: print (Stdout.decode ()) else: print (Stderr.decode ())
(3) Upload the download file plaintext password version
Import Paramiko
def ssh_transfer_file (host, port, user, passwd, Local_file, remote_file): transport = Paramiko. Transport (host, port) transport.connect (Username=user, password=passwd) sftp = Paramiko. Sftpclient.from_transport (Transport) # sftp.put (local_file, remote_file) # Upload files locally to remote host Sftp.get ( Remote_file, Local_file) # Download from remote host to local transport.close () if __name__ = = ' __main__ ': host = ' 10.129.205.151 ' PORT = USER = "root" PASSWD = "******" cmd = "Df-h" remote_file = "/data/download/nginx-1.12.0.tar.gz" Local_file = "nginx-1.12.0.tar.gz" ssh_transfer_file (HOST, PORT, USER, PASSWD, Local_file, Remote_file)
Test the download function first.
First look at the files on the remote host:
[Email protected] download]# ll/data/Downloadtotal17444drwxr-xr-x.9Lyy Lyy186Jul + .nginx-1.12.0-rw-r--r--.1Root root980831Apr A .nginx-1.12.0. TAR.GZDRWXR-xr-x. - 501 501 4096Jul + .python-3.6.1-rw-r--r--.1Root root16872064Mar + .python-3.6.1. tar.xz
View Code
Then run the code and look at the Local:
This has already downloaded the Nginx compressed package to the local. Upload function will not do a demonstration, interested can test their own ha.
(4) Upload download file key version
def ssh_transfer_file_pkey (host, port, user, Rsa_file, Local_file, remote_file): Private_key = Paramiko. Rsakey.from_private_key_file (rsa_file) # Specifies the file where the private key is located transport = Paramiko. Transport (host, port) transport.connect (Username=user, pkey=private_key) sftp = Paramiko. Sftpclient.from_transport (transport) sftp.put (Local_file, remote_file) # Upload files locally to remote host # Sftp.get ( Remote_file, Local_file) # Download from remote host to local Transport.close ()
Is it relatively simple?
Python Remote connection host Paramiko module