问题
I have this code below:
from pywinauto.application import Application
'''user-defined package user_input1'''
from user_input1 import Get_USER,Get_TOKEN
import time
server='host.domain.com'
cwd=r'C:\Program Files (x86)\PuTTY'
user = 'uname'
'''user = Get_USER()'''
password = Get_TOKEN()
app = Application().start(cmd_line='putty -ssh uname@host.domain.com')
putty = app.PuTTY
putty.wait('ready')
time.sleep(1)
putty.type_keys(password)
putty.type_keys("{ENTER}")
time.sleep(1)
putty.type_keys("export TMOUT=0")
putty.type_keys("{ENTER}")
This code works as expected. But when I change the line app = Application().start(cmd_line='putty -ssh uname@host.domain.com')
to app = Application().start(cmd_line="putty -ssh user@server")
i.e when I try to pass the variable names instead of the actual values, it is not able to make the connection because the user
and server
variables don't expand to their actual values.
How can I correctly pass the variables.
回答1:
The usual python string expansion actually worked in this case.
Here is my updated code:
from pywinauto.application import Application
'''user-defined package user_input1'''
from user_input1 import Get_USER,Get_TOKEN
import time
server='host.domain.com'
cwd=r'C:\Program Files (x86)\PuTTY'
user = Get_USER()
password = Get_TOKEN()
cmd = 'putty -ssh '+user+'@'+server
app = Application().start(cmd_line=cmd)
putty = app.PuTTY
putty.wait('ready')
time.sleep(1)
putty.type_keys(password)
putty.type_keys("{ENTER}")
time.sleep(1)
putty.type_keys("export TMOUT=0")
putty.type_keys("{ENTER}")
Works just as expected.
来源:https://stackoverflow.com/questions/64793920/how-to-pass-variable-in-pywinauto-application-startcmd-line