This article mainly introduces how to implement ssh remote access through the pythonparamiko module. for details, refer to the following example after installing paramiko:
The 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:
The code is as follows:
Str = chan. recv (recv_buffer)
While not str. endswith ('#'):
Str = chan. recv (recv_buffer)