【问题标题】:How to write data to database to a remote from another remote in another newtork thru SSH tunnel in bash script?如何通过bash脚本中的SSH隧道将数据从另一个网络中的另一个远程远程写入数据库?
【发布时间】:2022-01-28 12:03:05
【问题描述】:

我有三个遥控器,PC_A 有 python 代码,而 PC_B 在不同的网络有数据库,PC_C 也有数据库,与 PC_A 有相同的网络

我能够在脚本中将数据从 PC_A 加载到 PC_C,如下所示: 在 PC_A 中,

#!/bin/sh
# echo "i am in bin testing"
/home/user/env/bin/python3.8 /home/user/load_to_db.py -e IP_OF_PC_C  

为了将数据加载到 PC_B,我在 PC_A 和 PC_B 之间打开了一个 SSH 隧道, 如果我执行以下操作,我能够将数据加载到 PC_B:

ssh -L 9200:127.0.0.1:9200 -L 5601:127.0.0.1:5601 user@IP_PC_B -p 30020

其中 30020 是 SSH 端口,5601/9200 是 ElasticSearch 和 Kibana 端口 然后运行(在 PC_A 中)脚本:

#!/bin/sh
# echo "i am in bin testing"
/home/user/env/bin/python3.8 /home/user/load_to_db.py -e localhost

如果我在中写下以下内容,我会失败

#!/bin/sh
ssh user@PC_B -p 30020
/home/user/env/bin/python3.8 /home/user/load_to_db.py -e PC_B

我想知道如何将以上内容编写到脚本中,以使用 SSH 将数据传输到 PC_B 谢谢

【问题讨论】:

  • 你的第一个例子中IP_OF_PC_C 错了吗?上面那行说你转移到B。
  • 最后一个例子有错吗,它执行 python 应该是 ssh 的参数?一般来说,在脚本中启动交互式 ssh 会话没有任何意义。
  • @ceving,是的,感谢您的指出。对于第二个问题,我不明白,因为我的目标是使用 crontab 将一些数据写入 PC_B,我想知道如何在一个脚本中执行 SSH 和 python 脚本

标签: database bash ssh ssh-tunnel


【解决方案1】:

我在“在后台使用本地端口转发”和“杀死 ssh 进程”中搜索了一下,我做了一些测试,下面是我需要的

ssh -N -f -L 9200:127.0.0.1:9200 user@IP_OF_PC_B -p 30020 
do python code in PC_A
kill $(ps aux | grep ssh | grep 9200 | awk '{print $2}') 

【讨论】:

    猜你喜欢
    • 2019-07-15
    • 1970-01-01
    • 2012-08-08
    • 1970-01-01
    • 2017-12-26
    • 1970-01-01
    • 2012-09-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多