2017-07-19 130 views
0

我想運行一些打開另一個shell的命令,我可以輸入特定的命令。在那個shell中,我想在循環中運行另一個命令。 更具體的 - 在我的例子,它看起來像這樣:如何從python中的另一個shell執行命令?

> openssl s_client -connect 10.10.10.10:10000 ### this is the first command 
CONNECTED(00000003) ### some output 
(...) 
### now i'd like to type 'R' in a loop (R means renegotiate) 

什麼是做到這一點的最好方法是什麼?在此先感謝您的幫助

+1

我認爲你正在尋找子https://docs.python.org/2/library/subprocess.html – ginginsha

回答

0

與子進程,像這樣。

import subprocess 

cmd = 'ls -la' 
subprocess.call(cmd.split(), shell=False) 

所以你的情況會是這樣的:

import subprocess 

connect = 'openssl s_client -connect 10.10.10.10:10000' 
subprocess.call(connect.split(), shell=False) 

while (something): 
    subprocess.call('R', shell=False)