本文介绍了如何通过SSH Tunnel访问内网Web服务(即,请求内网HTTP)
背景
你在本地局域网(内网)有一台HTTP服务器,但是HTTP服务无法通过公网地址访问。 但是你可以通过公网连接到该相同局域网的一台SSH服务器(跳板机),你想通过这台SSH服务器向HTTP服务器发出请求。
确认SSH(隧道)服务可以正常连接
使用sshuttle工具,在终端建立(全局)SSH隧道连接
$ sudo sshuttle -r <username>@<host>:<port> <proxy-ip>[/<mask>]
如果隧道无法连接,或者不清楚sshuttle是什么, 请参考这一篇博客👉🏻如何使用Python(基于sshtunnel)通过SSH隧道访问内网数据库服务
在浏览器中请求Web服务
可以通过建立全局的 SSH Tunnel 以代理特定局域网地址的网络请求,从而可以访问目标Web服务
建立SSH隧道
$ sudo sshuttle -r <username>@<host>:<port> <proxy-ip>[/<mask>]
访问内网Web服务
现在你可以直接访问内网的Web服务了
使用Python请求内网HTTP服务
安装 sshtunnel-requests
$ pip install sshtunnel-requests
使用 sshtunnel-requests 请求内网HTTP服务
发出一个HTTP请求
import sshtunnel_requests
requests = sshtunnel_requests.Requests(
host="<ssh server host>",
username="<username login to ssh server>",
port=<ssh server port>,
password="<password login to ssh server>",
private_key="<path>/<to>/<ssh private key>",
private_key_password="<default None>"
)
response = requests.get("http://some-internal-host:port/endpoint")
print(response.status_code)
print(response.text)
发出多个HTTP请求
import sshtunnel_requests
requests = sshtunnel_requests.Requests.from_url(
"ssh://<username>@<ssh server host>[:<port>]",
"<path>/<to>/<private key>"
)
urls = [
"http://internal-host:port/endpoint/1",
"http://internal-host:port/endpoint/2",
"http://internal-host:port/endpoint/3",
]
results = []
for url in urls:
response = requests.get(url)
result.append(response.json())
使用多线程并发请求HTTP
import sshtunnel_requests
requests = sshtunnel_requests.from_url(
"ssh://<username>@<ssh server host>[:<port>]",
"<path>/<to>/<private key>"
)
urls = [
"http://internal-host:port/endpoint/1",
"http://internal-host:port/endpoint/2",
"http://internal-host:port/endpoint/3",
]
from concurrent.futures import ThreadPoolExecutor
from concurrent.futures import as_completed
results = []
with ThreadPoolExecutor(max_workers=3) as pool:
futures = []
for url in urls:
f = pool.submit(
lambda _req, _url: _req.get(_url),
requests, url
)
futures.append(f)
done_iter = as_completed(futures)
for future in done_iter:
response = future.result()
results.append(response.json())
from pprint import pp
for result in results:
pp(result)
带有登录信息的内网HTTP请求
from sshtunnel_requests.sessions import Session
session = Session.from_url(
"ssh://<username>@<ssh server host>[:<port>]",
"<path>/<to>/<private key>"
)
session.post("http://internal-host:port/login",
json={"username": "<login username>",
"password": "<login password>"})
response = session.get("http://internal-host:port/endpint/")
|