[英]How to do a scaffold over a ssh tunnel
I have a postgres database on a remote computer that I need to use我在需要使用的远程计算机上有一个 postgres 数据库
To log on to that computer I do the usual,要登录到那台计算机,我通常会这样做,
ssh <myuser>@<subdomain.domain.com> -p <custom port>
Once logged in, I must again登录后,我必须再次
ssh <someServerName>
Then I can do那我可以做
psql -U postgres -W
and get into the database并进入数据库
So in short, I need to do ssh something
twice简而言之,我需要执行两次
ssh something
Is it possible to connect to this database using entity framework core ?是否可以使用实体框架核心连接到该数据库?
Bonus question: Would this be called a ssh tunnel?额外问题:这会被称为 ssh 隧道吗?
Solution解决方案
It´s called Port Fowarding它被称为端口转发
using (var client = new SshClient("<public ip>", <port>, "<myuser>", "<mypassword>"))
{
client.Connect();
var port = new ForwardedPortLocal("localhost", 5432, "10.0.0.5", 5432);
client.AddForwardedPort(port);
port.Start();
using (var conn = new NpgsqlConnection("Server=localhost;Database=<secret>;Port=5432;User Id=postgres;Password=<secret>;"))
{
conn.Open();
}
port.Stop();
client.Disconnect();
}
Assuming you want to run psql command from your local host, you can do :假设您想从本地主机运行 psql 命令,您可以执行以下操作:
ssh -t <myuser>@<subdomain.domain.com> -p <custom port> ssh -t <someServerName> psql -U postgres -W
To do the port forwarding (SSH tunneling), you can do in one terminal :要进行端口转发(SSH 隧道),您可以在一个终端中进行:
ssh -L 5432:someServerName:5432 <myuser>@<subdomain.domain.com> -p <custom port>
-L 5432:someServerName:5432
means now the port 5432 on localhost is forwarded to someServerName:5432 (port used by postgres) -L 5432:someServerName:5432
表示现在本地主机上的端口 5432 被转发到 someServerName:5432(postgres 使用的端口)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.