Skip to content

Instantly share code, notes, and snippets.

Last active October 12, 2023 16:53
  • Star 18 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
Paramiko Connect via proxy
#!/usr/bin/env python
#-*- coding:utf8 -*-
# sources
# 1.
# 2.
# info:
# local -> proxy-server -> dest-server
# ~/.ssh/config
# Host proxy-server
# User hoge
# HostName
# IdentityFile ~/.ssh/id_rsa_proxy
# Host dest-server
# User fuga
# HostName
# 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()
host = conf.lookup(host_name)
client = paramiko.SSHClient()
host['hostname'], username=host['user'],
# if you have a key file
# key_filename=host['identityfile'],
stdin, stdout, stderr = client.exec_command('command to run on dest-host')
if __name__ == '__main__':
Copy link

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

Copy link

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!

Copy link

Thanks a billion. Saved me a lot of time.

Copy link

This helped me a lot !! Thanks.

Copy link

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?

Copy link

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

How to solve the above error?

Copy link

Any help is appreciated here.

Copy link

Any help is appreciated here.

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

Copy link

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

Copy link

What argument do we need to give in expanduser() in the code?

Copy link

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 ?

Copy link

hqqns commented May 20, 2021

Ty Ty Ty

Live saver!

Copy link

Any ideas on passing CertificateFile and identityfile in the proxy commands?
This is what the working ssh config looks like

TCPKeepAlive yes
ServerAliveCountMax 20
ServerAliveInterval 15

Host <vm name>
    Hostname <vm ip>
    IdentityFile /c/Users/<my ntid>/.ssh/autobahn_rsa
    CertificateFile /c/Users/<my ntid>/.ssh/
    ProxyCommand ssh -qx -i /c/Users/<my ntid>/.ssh/autobahn_rsa -o "CertificateFile /c/Users/<my ntid>/.ssh/" -o "TCPKeepAlive yes" -o "ServerAliveCountMax 20" -o "ServerAliveInterval 15" <proxy user>@<proxy host> -W %h:%p

I use this from git bash with a command like this ssh my_ntid@vm_host

I tried the above but getting

Traceback (most recent call last):
  File "C:\Users\sfager001\AppData\Local\Programs\Python\Python39\lib\site-packages\paramiko\", line 107, in recv
    r, w, x = select([self.process.stdout], [], [], select_timeout)
OSError: [WinError 10038] An operation was attempted on something that is not a socket

Copy link

I don't know how this works, but it worked for me for access a host via a jumphost. I tried the proxycommand and got "paramiko.ssh_exception.SSHException: Error reading SSH protocol banner" error,

This just works...ask long as the workstation has propert ssh config file!

Copy link

thank you a lot

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