Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/339.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ssh/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在python中通过SSH连接自动应答提示_Python_Ssh_Prompt_Paramiko - Fatal编程技术网

如何在python中通过SSH连接自动应答提示

如何在python中通过SSH连接自动应答提示,python,ssh,prompt,paramiko,Python,Ssh,Prompt,Paramiko,我想使用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. Run

我想使用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.
然后我应该输入
我知道这个选项会破坏一切;无论如何都要这样做。
然后按
ENTER
继续

我有以下代码连接到远程主机:

ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(ip, username=usrname, password=passwd, compress=True)

我尝试了self.sshlink.exec_命令('cd-app\u-addr;/app-init');它没有执行任何操作。

我在中的一个未被接受的答案中找到了解决方案

这就是我最终尝试并成功的地方:

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()

我在年的一份联合国认可的答案中找到了解决办法

这就是我最终尝试并成功的地方:

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()

如果您询问如何以编程方式执行此操作,那么如果重定向标准输入不起作用,您可以使用。您需要查看Expect是否作为您的操作系统的软件包提供,不管是什么。您如何使用paramiko,以及哪些不起作用?@SergeBallesta,请查看我的编辑!如果您询问如何以编程方式执行此操作,那么如果重定向标准输入不起作用,您可以使用。您需要查看Expect是否作为您的操作系统的软件包提供,不管是什么。您如何使用paramiko,以及哪些不起作用?@SergeBallesta,请查看我的编辑!