I am running a command to call pscp.exe to transfer some files between hosts. I am using the subprocess module to execute the pscp.exe. Here is the code sample:
command = 'pscp -i {} {}@{}:/files/{} {}'\
.format(privkey, username, server, file_remote_name, file_local_name)
log(logger, "Command to run: {}".format(command), logging.DEBUG)
ret_code = subprocess.call(args=command, shell=False)
if ret_code == 0:
return True
else:
return False
The thing is at the first run, pscp.exe prompts to accept the host key. I read through the putty documentation but did not find any option to automatically accepts the key. So at first run I would have to manually press the 'y' key. This is not possible in my prod environment as it is fully managed by Saltstack and I do not want any human interaction. So how can I modify the program to send the 'y' key press to the pscp process?
PS: I do know that in another question people suggested to prepend "echo y | " in front of the pscp command. But it does not seem to work with the subprocess.call() function. All am I using it wrong?