python paramiko 使用

  1. 【区别】exec-command 和 invoke-shell
# 0.参考
https://stackoverflow.com/questions/55762006/what-is-the-difference-between-exec-command-and-send-with-invoke-shell-on-para


# 1. invoke-shell
The shell will then present a command prompt and wait for client/user to type the commands. 
The purpose of the shell channel is to implement an interactive shell session.


# 2. exec-command相当于putty执行
ssh user@host command
  1. ssh【exec_command】自动交互
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 + '
')
ssh_stdin.flush()
time.sleep(5)
ssh_stdout.read()
原文地址:https://www.cnblogs.com/amize/p/15108241.html