我有一台服务器,将从本地计算机登录到该服务器并创建隧道。
我有一个不创建隧道的bash脚本
sshpass -p ${1} ssh ${2}@${3}
ssh -L <port1>:<host>:80 -i /home/<user>/private_key <user_ID>@<host2>
结果我得到-
sh ssh_to_box.sh pwd username remotehost
Welcome to Ubuntu 16.04.5 LTS (GNU/Linux 4.4.0-145-generic x86_64)
* Documentation: https://help.ubuntu.com
* Management: https://landscape.canonical.com
* Support: https://ubuntu.com/advantage
144 packages can be updated.
0 updates are security updates.
New release '18.04.2 LTS' available.
Run 'do-release-upgrade' to upgrade to it.
*** System restart required ***
Last login: Thu May 30 09:36:36 2019 from remotehost
$
脚本未执行隧道操作。
还应该做$ bash
如何ssh到远程并从该远程计算机创建隧道并使隧道保持 Activity 状态?
最佳答案
可能您实际上是在创建隧道,应该检查是否
ss -lptn | awk '{ print $4 }' | awk -F ':' '{ print $2 }' | sed -e '/<port1>/!d'
当打开ssh控制台时,正在返回某些信息,如果为true,则说明您具有隧道。
(也许有更好的方法来执行此检查,但是我对awk不太了解)
如果您希望隧道是持久的,可以在tmux / screen中执行它或运行
nohup <tunnel_command> &
重要的是要注意,名称也将由远程主机解析。
关于bash - SSH之后如何使用bash脚本使用私钥创建到远程计算机的隧道,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56376021/