Ssh connection memo using ProxyCommand of ssh_config in Python

Notes on ssh connection using paramiko with the following settings in ssh_config and ssh connection to app_host via fumidai_host

Host fumidai_host
    User user_name
    Hostname xx.xx.xx.xx (<= GIP)
    IdentityFile ~/.ssh/hoge.pem


Host app_host
    HostName 192.168.0.1 (<= private ip)
    User user_name
    IdentityFile ~/.ssh/hoge.pem
    ProxyCommand ssh -W %h:%p fumidai_host

You can connect like this. Sample to sftp for the time being


import os
import paramiko


#lookup ssh config file
config_file = os.path.join(os.getenv('HOME'), '.ssh/config')
ssh_config = paramiko.SSHConfig()
ssh_config.parse(open(config_file, 'r'))
lkup = ssh_config.lookup(hostname)

#Connect using ProxyCommand settings
ssh = paramiko.SSHClient()
ssh.load_system_host_keys()
ssh.connect(
    lkup['hostname'],
    username=lkup['user'],
    key_filename=lkup['identityfile'],
    sock=paramiko.ProxyCommand(lkup['proxycommand'])
)


sftp = ssh.open_sftp()        
#The rest is normal
# sftp.put(src, dist)Or
# sftp.get(src, dist)Or

sftp.close()
ssh.close()

Recommended Posts

Ssh connection memo using ProxyCommand of ssh_config in Python
A memo of writing a basic function in Python using recursion
(Bad) practice of using this in Python
Meaning of using DI framework in Python
Summary of Excel operations using OpenPyXL in Python
Basics of I / O screen using tkinter in python3
The pain of gRPC using Python. November 2019. (Personal memo)
Equivalence of objects in Python
python: Basics of using scikit-learn ①
Python memo using perl --join
Implementation of quicksort in Python
Translate using googletrans in Python
Using Python mode in Processing
Ssh connection using public key
[Memo] The mystery of cumulative assignment statements in Python functions
A memo when creating a directed graph using Graphviz in Python
How to develop in a virtual environment of Python [Memo]
Pixel manipulation of images in Python
[Python] A memo of frequently used phrases (by myself) in Python scripts
GUI programming in Python using Appjar
[Python] Operation memo of pandas DataFrame
Image capture of firefox using python
Precautions when using pit in Python
Write various forms of phylogenetic tree in Python using ETE Toolkit
Web application development memo in python
Division of timedelta in Python 2.7 series
Removal of haze using Python detailEnhanceFilter
MySQL-automatic escape of parameters in python
Handling of JSON files in Python
Try using LevelDB in Python (plyvel)
Waveform display of audio in Python
Python3 compatible memo of "python start book"
Using global variables in python functions
Get, post communication memo in Python
Let's see using input in python
Infinite product in Python (using functools)
Implementation of desktop notifications using Python
Edit videos in Python using MoviePy
Separate display of Python graphs (memo)
Implementation of original sorting in Python
Reversible scrambling of integers in Python
Try using Leap Motion in Python
Depth-first search using stack in Python
When using regular expressions in Python
GUI creation in python using tkinter 2
(Bad) practice of using this in Python
[Python] [Word] [python-docx] Try to create a template of a word sentence in Python using python-docx
Impressions of people with experience in other languages learning Python using PyQ
Python: Basics of image recognition using CNN
Conversion of string <-> date (date, datetime) in Python
[Python] Extension using inheritance of matplotlib (NavigationToolbar2TK)
Notes using cChardet and python3-chardet in Python 3.3.1.
About building GUI using TKinter of Python
Try using the Wunderlist API in Python
GUI creation in python using tkinter part 1
Get Suica balance in Python (using libpafe)
Slowly hash passwords using bcrypt in Python
[Learning memo] Basics of class by python
General Theory of Relativity in Python: Introduction
Try using the Kraken API in Python
Using venv in Windows + Docker environment [Python]