我想使用python paramiko在远程计算机上调用应用程序;此应用程序等待您输入特定字符串然后成功。如何通过SSH连接发送请求的字符串?
应用程序调用如下:
./app --init
This option will delete the database and recreate all files.
OLD DATA (IF ANY) WILL BE LOST!
This option must be used just on one of your servers.
Running it on any one of the servers will delete the other server's data, too.
If you are sure, type the following sentence and press ENTER:
I KNOW THAT THIS OPTION DESTROYS EVERYTHING; DO IT ANYWAY.
然后我应该输入I KNOW THAT THIS OPTION DESTROYS EVERYTHING; DO IT ANYWAY.
并按ENTER
继续。
我有这段代码连接到远程主机:
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(ip, username=usrname, password=passwd, compress=True)
我试过self.sshlink.exec_command('cd app_addr;./app --init')
;它什么都没做。
答案 0 :(得分:0)
我在this question中的一个未接受的答案中找到了解决方案。
这是我最终尝试并成功的原因:
cmd = 'cd app_address;./app --init'
answer = 'I KNOW THAT THIS OPTION DESTROYS EVERYTHING; DO IT ANYWAY.'
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(hostname=ip, username=username, password=password)
ssh_stdin, ssh_stdout, ssh_stderr = ssh.exec_command(cmd)
ssh_stdin.write(answer + '\n')
ssh_stdin.flush()
time.sleep(5)
ssh_stdout.read()