Below is the process I am doing manually and I want to automate it in Python.
- I am opening SSH terminal to MySQL server using below command
cf ssh -L 63380:100.120.11.22:3380 appname
- Once this SSH is established I am doing ctrl+t to open a new terminal tab and run below command to create DB objects
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
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.
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.
Kindly let me know if any solution is available in Python for this. Env: mac, pcf.