Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Paramiko Connect via proxy
#!/usr/bin/env python
#-*- coding:utf8 -*-
# sources
# 1. https://gist.github.com/tell-k/4943359#file-paramiko_proxycommand_sample-py-L11
# 2. https://github.com/paramiko/paramiko/pull/97
# info: http://bitprophet.org/blog/2012/11/05/gateway-solutions/
# local -> proxy-server -> dest-server
# ~/.ssh/config
#
# Host proxy-server
# User hoge
# HostName proxy.example.com
# IdentityFile ~/.ssh/id_rsa_proxy
#
# Host dest-server
# User fuga
# HostName proxy.example.com
# IdentityFile ~/.ssh/id_rsa_dest
# ProxyCommand ssh proxy-server nc %h %p
#
import os
import sys
import paramiko
def test_client(host_name):
conf = paramiko.SSHConfig()
conf.parse(open(os.path.expanduser('~/.ssh/config')))
host = conf.lookup(host_name)
client = paramiko.SSHClient()
client.load_system_host_keys()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(
host['hostname'], username=host['user'],
# if you have a key file
# key_filename=host['identityfile'],
password='yourpassword',
sock=paramiko.ProxyCommand(host.get('proxycommand'))
)
stdin, stdout, stderr = client.exec_command('command to run on dest-host')
print stdout.read()
if __name__ == '__main__':
test_client(sys.argv[1])
@jimmygns

This comment has been minimized.

Copy link

@jimmygns jimmygns commented Aug 8, 2016

%d format: a number is required, not str
do you how to solve this problem? it happens at where you create the sock
tho my proxycommand is a little different ProxyCommand nc -x localhost:8158 %h %p

@eloyz

This comment has been minimized.

Copy link

@eloyz eloyz commented Oct 11, 2017

I know nothing about Paramiko and next-to-nothing about proxy commands and this script got me setup in minutes. Thank you!

@paustefa

This comment has been minimized.

Copy link

@paustefa paustefa commented Nov 23, 2017

Thanks a billion. Saved me a lot of time.

@dasayan05

This comment has been minimized.

Copy link

@dasayan05 dasayan05 commented May 9, 2018

This helped me a lot !! Thanks.

@sreekaanth

This comment has been minimized.

Copy link

@sreekaanth sreekaanth commented Sep 4, 2019

Where is host_name in the ssh config file?

How does it pickup host name from the config file is it that host_name is like a keyword for parsing the config file?

@JaldhiPandya

This comment has been minimized.

Copy link

@JaldhiPandya JaldhiPandya commented Mar 18, 2020

Getting error with this code -
paramiko.ssh_exception.ProxyCommandFailure: ProxyCommand("") returned nonzero exit status: Broken pipe

How to solve the above error?

@JaldhiPandya

This comment has been minimized.

Copy link

@JaldhiPandya JaldhiPandya commented Mar 18, 2020

Any help is appreciated here.

@andrejesusdasilva

This comment has been minimized.

Copy link

@andrejesusdasilva andrejesusdasilva commented May 15, 2020

Any help is appreciated here.

Try this @JaldhiPandya:
ProxyCommand ssh -q -W %h:%p proxy-server

@andrejesusdasilva

This comment has been minimized.

Copy link

@andrejesusdasilva andrejesusdasilva commented May 15, 2020

Any help is appreciated here.

Try this @JaldhiPandya:
ProxyCommand ssh -q -W %h:%p proxy-server

one more thing, here I needed to set the port in my "dest-server".

Hope this helps you

@nikita01021998

This comment has been minimized.

Copy link

@nikita01021998 nikita01021998 commented Aug 26, 2020

What argument do we need to give in expanduser() in the paramiko-proxy.py code?

@GaetanLepage

This comment has been minimized.

Copy link

@GaetanLepage GaetanLepage commented Apr 22, 2021

I can't get ssh working through a bastion: I always get the error paramiko.ssh_exception.SSHException: Error reading SSH protocol banner.
Did some of you encountered the same issue ?

@hqqns

This comment has been minimized.

Copy link

@hqqns hqqns commented May 20, 2021

Ty Ty Ty

Live saver!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment