After paramiko is installed, see the following example:
Copy codeThe Code is as follows:
Import paramiko
# Set the remote host address and port for ssh connection
T = paramiko. Transport (ip, port ))
# Set logon name and password
T. connect (username = username, password = password)
# Open a channel after successful connection
Chan = t. open_session ()
# Set Session Timeout
Chan. settimeout (session_timeout)
# Enable remote terminal
Chan. get_pty ()
# Activating terminal
Chan. invoke_shell ()
Then, you can remotely execute commands and obtain local feedback through chan. send ('COMMAND ') and chan. recv (recv_buffer.
For example:
Chan. send ('pwd ')
Print chan. recv (65535)
The point is that some commands are executed for a long time, and the desired result may not be obtained when the returned result is inappropriate. You can use time. sleep () to wait, or use some conditional loops.
For example:
Copy codeThe Code is as follows:
Str = chan. recv (recv_buffer)
While not str. endswith ('#'):
Str = chan. recv (recv_buffer)