简体   繁体   English

在Python中的不同终端中打开SSH会话并运行命令

[英]Opening SSH session and running commands in different terminal in Python

Below is the process I am doing manually and I want to automate it in Python. 以下是我正在手动执行的过程,并且我想在Python中使其自动化。

  1. I am opening SSH terminal to MySQL server using below command 我正在使用以下命令将SSH终端打开到MySQL服务器
cf ssh -L 63380:100.120.11.22:3380 appname
  1. Once this SSH is established I am doing ctrl + t to open a new terminal tab and run below command to create DB objects 建立此SSH后,我将按ctrl + t打开新的终端选项卡,然后运行以下命令来创建数据库对象
mysql -u myuser -h 0 -pmypassword -D mydbname -P 3380 < init_db/mysql/schema.ddl

Now I have written Python code for step 1 like below 现在,我已经为步骤1编写了Python代码,如下所示

process = subprocess.Popen("cf ssh -L 63380:100.120.11.22:3380 appname",shell = True)

It opens SSH terminal but when ran second step using below code it does not connect to opened SSH terminal it says not able to connect to MySQL server. 它打开SSH终端,但是使用下面的代码运行第二步时,它没有连接到打开的SSH终端,它说无法连接到MySQL服务器。

pro = subprocess.Popen("mysql -u myuser -h 0 -pmypassword -D mydbname -P 3380 < init_db/mysql/schema.ddl", stdout=subprocess.PIPE,shell=True, preexec_fn=os.setsid) 

I have searched and tried other options like writing step 2 in different program and calling from first program after SSH is established but of no use. 我搜索并尝试了其他选项,例如在不同的程序中编写第2步,并在建立SSH后从第一个程序调用,但没有用。

Kindly let me know if any solution is available in Python for this. 请让我知道Python是否提供任何解决方案。 Env: mac, pcf. 信封:mac,pcf。

It looks to me like this command: 在我看来,该命令如下:

cf ssh -L 63380:100.120.11.22:3380 appname

ssh's to your CloudFoundry application server and forwards any connection on your local machine's port 63380 to the remote port 3380. So it seems like your mysql connection should be: ssh到您的CloudFoundry应用程序服务器,并将本地计算机端口63380上的所有连接转发到远程端口3380。因此,看来您的mysql连接应该是:

mysql -u myuser -h localhost -pmypassword -D mydbname -P 63380 < init_db/mysql/schema.ddl

where init_db/mysql/schema.ddl is on your local machine. 本地计算机上的init_db/mysql/schema.ddl

I'm not completely sure if this will work with CloudFoundry, but with ssh you can run a remote command like this: 我不确定这是否可以与CloudFoundry一起使用,但是使用ssh可以运行如下所示的远程命令:

ssh username@<host or ip> <command>
#for example:
ssh myname@example.com ls -al

So you might try putting ~/init_db/mysql/schema.dll on the server and use this command: 因此,您可以尝试在服务器上放置~/init_db/mysql/schema.dll并使用以下命令:

cf ssh appname mysql -u myuser -pmypassword -Dmydbname -P 3380 < ~/init_db/mysql/schema.dll

You should be able to drop that command into python's subprocess.Popen() . 您应该能够将该命令放入python的subprocess.Popen()

I found way of avoiding the ssh to mysql. 我找到了避免ssh到mysql的方法。 There is a cf mysql plugin by which we can connect to cf mysql services and execute the queries. 有一个cf mysql插件,通过它我们可以连接到cf mysql服务并执行查询。 More details can be found at: https://github.com/andreasf/cf-mysql-plugin 可以在以下位置找到更多详细信息: https : //github.com/andreasf/cf-mysql-plugin

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM