You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hi, I'd like to create a SOCKS proxy from an accessible SSH server, in order to pass Python Requests over that server.
Is it possible to do this using paramiko?
Reacted by austinbutler, Tung Dao, Maxim Smirnov, 玖亖伍, Grant Hernandez, linkpwd, Mike, Niko, Pieter Bork, Thijs and 14 more
Thought there was an open feature req for SOCKS but all I see is #508 which mentions it in passing. Let's make it this ticket. Thanks for the report!
I also saw some offhand references to using ProxyCommand support to talk to a SOCKS process; I don't use SOCKS myself so not sure how workable that is, but if you haven't already looked into that I'd suggest doing so.
If anybody is wondering about the current status: I'm still working on getting the code into an upstreamable state, whenever I find some time. It might still take a few more weeks before I get to a state I'm satisfied with.
I wrote a small python app using this new branch, and it's ok for the most part. It connects to an SSH server and creates a socks proxy. It works fine when I set my OS network proxy setting to manual (Ubuntu 20) using ('localhost', 1080). But when I try to use a PAC file for automatic proxy, it raises the error below. I should mention that the same PAC file is completely functional when I use an 'ssh -D' command. It seems to me that it's unhappy about the struct.unpack on line 143, but I can't spot the issue. Would you please look into this and help me solve it? Many thanks.
Exception happened during processing of request from ('127.0.0.1', 57916)
Traceback (most recent call last):
File "/home/fred/anaconda3/lib/python3.8/socketserver.py", line 650, in process_request_thread
self.finish_request(request, client_address)
File "/home/fred/anaconda3/lib/python3.8/socketserver.py", line 360, in finish_request
self.RequestHandlerClass(request, client_address, self)
File "/home/fred/anaconda3/lib/python3.8/site-packages/paramiko/socks_proxy.py", line 239, in init
super(SOCKS5RequestHandler, self).init(*args, **kwargs)
File "/home/fred/anaconda3/lib/python3.8/socketserver.py", line 720, in init
self.handle()
File "/home/fred/anaconda3/lib/python3.8/site-packages/paramiko/socks_proxy.py", line 257, in handle
version = m.get_char()
File "/home/fred/anaconda3/lib/python3.8/site-packages/paramiko/socks_proxy.py", line 143, in get_char
return byte_ord(struct.unpack("!c", self.get_bytes(1))[0])
struct.error: unpack requires a buffer of 1 bytes
PS: The contents of the PAC file which is accessed via a SimpleHttpServer:
function FindProxyForURL(url, host)
{
return 'SOCKS localhost:1080; DIRECT';
}
Here's my code snippet for firing up the connection:
I'm getting another exception that is not affecting the connection, but I thought there might be some value in pointing it out. What do you think is going wrong here?
Secsh channel 29 open FAILED: open failed: Connect failed
Exception happened during processing of request from ('127.0.0.1', 60522)
Traceback (most recent call last):
File "/home/fred/anaconda3/lib/python3.8/socketserver.py", line 650, in process_request_thread
self.finish_request(request, client_address)
File "/home/fred/anaconda3/lib/python3.8/socketserver.py", line 360, in finish_request
self.RequestHandlerClass(request, client_address, self)
File "/home/fred/anaconda3/lib/python3.8/site-packages/paramiko/socks_proxy.py", line 239, in init
super(SOCKS5RequestHandler, self).init(*args, **kwargs)
File "/home/fred/anaconda3/lib/python3.8/socketserver.py", line 720, in init
self.handle()
File "/home/fred/anaconda3/lib/python3.8/site-packages/paramiko/socks_proxy.py", line 318, in handle
channel, af, addr = self._open_channel(af_and_addrs)
File "/home/fred/anaconda3/lib/python3.8/site-packages/paramiko/socks_proxy.py", line 361, in _open_channel
channel = self.server.ssh_transport.open_channel(
File "/home/fred/anaconda3/lib/python3.8/site-packages/paramiko/transport.py", line 1017, in open_channel
raise e
paramiko.ssh_exception.ChannelException: ChannelException(2, 'Connect failed')
I guess it'd be easier if I provided my code and you see it for yourself.
Here's its Git repo. It's a simple GUI app. The primary purpose was to access our university server and IP address.
You're using the branch git+https://github.com/yomagroup/paramiko.git@wip-socks-proxy, which is an older branch to test viability of the whole approach. The branch proposed for inclusion in paramiko as referenced in #1873 (yomagroup:add-socks-proxy) includes various improvements related to stability. Can you please try with the add-socks-proxy branch and check whether you get the same problem with it as well?
Thanks for your help and your quick reply. I tested this new branch, and it led to the same issues. I should also clarify that I'm not a network guru, and I may have made a mistake in setting up the PAC file through the HttpServer. After all, the SSH proxy is working fine when I manually access it. So, I play around with my code and let you know if I can resolve the issue.
Activity
bitprophet commentedon May 9, 2017
Thought there was an open feature req for SOCKS but all I see is #508 which mentions it in passing. Let's make it this ticket. Thanks for the report!
I also saw some offhand references to using ProxyCommand support to talk to a SOCKS process; I don't use SOCKS myself so not sure how workable that is, but if you haven't already looked into that I'd suggest doing so.
[-]Is there an equivalent to "ssh -D"?[/-][+]SOCKS proxy support, like 'ssh -D'[/+]Cellebyte commentedon Nov 11, 2017
maxim-smirnov commentedon Feb 11, 2018
Any updates here?
Cellebyte commentedon Feb 11, 2018
Look for your needs.
Problem
maxim-smirnov commentedon Feb 11, 2018
@Cellebyte Yes, I really need socks functionality...
utkonos commentedon Jan 8, 2019
Support for dynamic port forwarding would be great!
obikao commentedon Apr 17, 2019
Same
ksantr commentedon Jun 3, 2019
We need it
hamadrehman commentedon Jul 28, 2019
we need it
linkpwd commentedon Aug 11, 2019
i need it .
anarchy9388 commentedon Aug 13, 2019
I need it!
15 remaining items
Dunedan commentedon May 30, 2021
@bitprophet Sounds good. I'll do. 👍
If anybody is wondering about the current status: I'm still working on getting the code into an upstreamable state, whenever I find some time. It might still take a few more weeks before I get to a state I'm satisfied with.
anarchy9388 commentedon Jun 3, 2021
@Dunedan Thanks!
Add SOCKS5 proxy functionality
Add SOCKS5 proxy functionality
Dunedan commentedon Jul 2, 2021
I've finally found time to put the SOCKS proxy code into a PR, so here it is: #1873
@bitprophet I'm looking forward to your review. 🙂
FredAmouzgar commentedon Sep 19, 2021
Hi @Dunedan,
I wrote a small python app using this new branch, and it's ok for the most part. It connects to an SSH server and creates a socks proxy. It works fine when I set my OS network proxy setting to manual (Ubuntu 20) using ('localhost', 1080). But when I try to use a PAC file for automatic proxy, it raises the error below. I should mention that the same PAC file is completely functional when I use an 'ssh -D' command. It seems to me that it's unhappy about the struct.unpack on line 143, but I can't spot the issue. Would you please look into this and help me solve it? Many thanks.
Exception happened during processing of request from ('127.0.0.1', 57916)
Traceback (most recent call last):
File "/home/fred/anaconda3/lib/python3.8/socketserver.py", line 650, in process_request_thread
self.finish_request(request, client_address)
File "/home/fred/anaconda3/lib/python3.8/socketserver.py", line 360, in finish_request
self.RequestHandlerClass(request, client_address, self)
File "/home/fred/anaconda3/lib/python3.8/site-packages/paramiko/socks_proxy.py", line 239, in init
super(SOCKS5RequestHandler, self).init(*args, **kwargs)
File "/home/fred/anaconda3/lib/python3.8/socketserver.py", line 720, in init
self.handle()
File "/home/fred/anaconda3/lib/python3.8/site-packages/paramiko/socks_proxy.py", line 257, in handle
version = m.get_char()
File "/home/fred/anaconda3/lib/python3.8/site-packages/paramiko/socks_proxy.py", line 143, in get_char
return byte_ord(struct.unpack("!c", self.get_bytes(1))[0])
struct.error: unpack requires a buffer of 1 bytes
PS: The contents of the PAC file which is accessed via a SimpleHttpServer:
function FindProxyForURL(url, host)
{
return 'SOCKS localhost:1080; DIRECT';
}
Here's my code snippet for firing up the connection:
FredAmouzgar commentedon Sep 19, 2021
I'm getting another exception that is not affecting the connection, but I thought there might be some value in pointing it out. What do you think is going wrong here?
Secsh channel 29 open FAILED: open failed: Connect failed
Exception happened during processing of request from ('127.0.0.1', 60522)
Traceback (most recent call last):
File "/home/fred/anaconda3/lib/python3.8/socketserver.py", line 650, in process_request_thread
self.finish_request(request, client_address)
File "/home/fred/anaconda3/lib/python3.8/socketserver.py", line 360, in finish_request
self.RequestHandlerClass(request, client_address, self)
File "/home/fred/anaconda3/lib/python3.8/site-packages/paramiko/socks_proxy.py", line 239, in init
super(SOCKS5RequestHandler, self).init(*args, **kwargs)
File "/home/fred/anaconda3/lib/python3.8/socketserver.py", line 720, in init
self.handle()
File "/home/fred/anaconda3/lib/python3.8/site-packages/paramiko/socks_proxy.py", line 318, in handle
channel, af, addr = self._open_channel(af_and_addrs)
File "/home/fred/anaconda3/lib/python3.8/site-packages/paramiko/socks_proxy.py", line 361, in _open_channel
channel = self.server.ssh_transport.open_channel(
File "/home/fred/anaconda3/lib/python3.8/site-packages/paramiko/transport.py", line 1017, in open_channel
raise e
paramiko.ssh_exception.ChannelException: ChannelException(2, 'Connect failed')
Dunedan commentedon Sep 19, 2021
@FredAmouzgar To debug this, can you please provide the PAC file you're using and a recording of the TCP packets between the browser and the proxy?
FredAmouzgar commentedon Sep 19, 2021
I guess it'd be easier if I provided my code and you see it for yourself.
Here's its Git repo. It's a simple GUI app. The primary purpose was to access our university server and IP address.
https://github.com/FredAmouzgar/SshTunneller
Dunedan commentedon Sep 19, 2021
You're using the branch
git+https://github.com/yomagroup/paramiko.git@wip-socks-proxy, which is an older branch to test viability of the whole approach. The branch proposed for inclusion in paramiko as referenced in #1873 (yomagroup:add-socks-proxy) includes various improvements related to stability. Can you please try with theadd-socks-proxybranch and check whether you get the same problem with it as well?FredAmouzgar commentedon Sep 19, 2021
Thanks for your help and your quick reply. I tested this new branch, and it led to the same issues. I should also clarify that I'm not a network guru, and I may have made a mistake in setting up the PAC file through the HttpServer. After all, the SSH proxy is working fine when I manually access it. So, I play around with my code and let you know if I can resolve the issue.
Add SOCKS5 proxy functionality (#1)
su115 commentedon Jan 22, 2025
What the hell is going on here? Why is this feature still here? I thought it was in the prod. True shame. I need this feature!!!!
mfat commentedon Aug 3, 2025
Was this ever implemented?
TX7083 commentedon Nov 26, 2025
@Dunedan did you ever finish working on this feature?