Get files from Linux using paramiko and scp [Python]

at first

I used to get a fixed image file from a fixed server every month and make regular meeting materials, It was troublesome to dive and get it with WinSCP, so I decided to automate it with Python.

When I checked it, it seemed that I could do it using paramiko and scp, so I tried it.

Operating environment

Execution side: Windows Server 2012 Obtained from: CentOS Linux 7

Installation

For terminals connected to the Internet, normal pip is fine

py -m pip install paramiko

If you are in an offline environment, download it once and then install it.

py -m pip download -d C:\tmp --no-binary :all: paramiko

When I typed the above command ERROR: Command errored out with exit status 1: I got an error saying that I could not download. After fighting for a while

py -m pip download -d C:\tmp paramiko

I was able to download it at. The --no-binary option downloads the source, not the binary (It makes it less susceptible to the environment), but it doesn't seem to work well with it. Take the downloaded file to the installation destination and install it with the following command.

pip install --no-index --find-links=tmp paramiko

I wasn't sure why I couldn't do it the usual way, but in my environment it works fine with the above.

use

First of all, let's try to make ʻimport paramiko`. If you cannot, there is a problem with the installation method.

File transfer code

First of all, I will put the necessary code.

import paramiko
import scp

#Server login information
host = "IP address"
user = "User name"
pas  = "password"

#Output directory
out_path = r"C:\temp"

#Target file path
target_path = "/var/tmp/hoge.log"

#Server connection
with paramiko.SSHClient() as sshc:
  sshc.set_missing_host_key_policy(paramiko.AutoAddPolicy())
  sshc.connect(hostname=host, port=22, username=user, password=pas)

  #Copy processing by SCP
  with scp.SCPClient(sshc.get_transport()) as scpc:
    scpc.get(remote_path=target_path, local_path=out_path)

Description

I haven't tried various things yet, so I don't understand the details, but somehow.

    scpc.get(remote_path=target_path, local_path=out_path)

Regarding the two arguments here, it seems that only the string type is accepted. Convert some useful * Path * objects to strings with str () etc. before passing them. By the way, even if you don't declare the element

scpc.get(target_path, out_path)

It works fine with. However, since this function has other arguments, it is specified as a magic.

Usability

I felt it was simple and very easy to use. In Linux, the hierarchy delimiter is / instead of \, so it is easy to combine with a simple string. (Of course, Windows \ can also be treated as a character string)

In my case, I created the target file list while looping as shown below.

    for day in day_list:
      target_list.append(f"{port}/result{port}-{day}.png ")

You can also execute commands with paramiko, but that is summarized separately.

Recommended Posts

Get files from Linux using paramiko and scp [Python]
Read and use Python files from Python
From Python to using MeCab (and CaboCha)
How to get followers and followers from python using the Mastodon API
Get options in Python from both JSON files and command line arguments
Get mail from Gmail and label it with Python3
Get and automate ASP Datepicker control using Python and Selenium
Copy S3 files from Python to GCS using GSUtil
Flatten using Python yield from
[python] Get quotient and remainder
Visualize plant activity from space using satellite data and Python
Recursively copy files from the directory directly under the directory using Python
[Linux] Compress folders and transfer files from remote to local
Predict gender from name using Gender API and Pykakasi in Python
[Python] Random data extraction / combination from DataFrame using random and pandas
Shoot time-lapse from a PC camera using Python and OpenCV
[Scp] Copy files locally from Linux server with Tera Term ssh scp
[Python] Loading csv files using pandas
Using Rstan from Python with PypeR
Get data from Quandl in Python
Authentication using tweepy-User authentication and application authentication (Python)
Notes on using MeCab from Python
Manipulate files and folders in Python
Get data from MySQL on a VPS with Python 3 and SQLAlchemy
About Python, from and import, as
Using Cloud Storage from Python3 (Introduction)
Get and set the value of the dropdown menu using Python and Selenium
Get upcoming weather from python weather api
Run Ansible from Python using API
Get data from Twitter using Tweepy
Precautions when using phantomjs from python
Access spreadsheets using OAuth 2.0 from Python
Try using Amazon DynamoDB from Python
Extract strings from files in Python
Reading from text files and SQLite in Python (+ Pandas), R, Julia (+ DataFrames)
Precautions and error handling when calling .NET DLL from python using pythonnet
Get news from three major mobile companies using Django and the News API
Get and estimate the shape of the head using Dlib and OpenCV with python
[AWS] Using ini files with Lambda [Python]
Python> dictionary> values ()> Get All Values by Using values ()
Get html from element with Python selenium
Get exchange rates from open exchange rates in Python
[Note] Get data from PostgreSQL with Python
Play audio files from Python with interrupts
Get Suica balance in Python (using libpafe)
Get tweets containing keywords using Python Tweepy
Get keystrokes from / dev / input (python evdev)
Using Python and MeCab with Azure Databricks
Decrypt files encrypted with openssl from python with openssl
Get Youtube data in Python using Youtube Data API
I tried using UnityCloudBuild API from Python
Porting and modifying doublet-solver from python2 to python3.
Get battery level from SwitchBot in Python
Read and write JSON files in Python
Get Precipitation Probability from XML in Python
I'm using tox and Python 3.3 with Travis-CI
Download files in any format using Python
[Amazon Linux] Switching from Python 2 series to Python 3 series
Get the address from latitude and longitude
[Python] Get the main color from the screenshot
Get metric history from MLflow in Python