I want to call an application on a remote machine using python paramiko; this application waits till you enter a specific string and then succeeds. How can I send requested string over SSH connection to it?
The application call is like:
./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.
Then I should type I KNOW THAT THIS OPTION DESTROYS EVERYTHING; DO IT ANYWAY.
and press ENTER
to continue.
I have this code to connect to the remote host:
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(ip, username=usrname, password=passwd, compress=True)
I tried self.sshlink.exec_command('cd app_addr;./app --init')
; it performed nothing.
I found the solution in one of the un-accepted answers in this question.
This is what I've finally tried and succeeded:
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()