Python paramiko implements methods for SSH remote access

  • 2020-04-02 13:15:16
  • OfStack

After installing (link: #), take a look at the following example:


import paramiko
# Set up the ssh The remote host address and port to which to connect 
t=paramiko.Transport((ip,port))
# Set the login name and password 
t.connect(username=username,password=password)
# Open one after successful connection channel
chan=t.open_session()
# Set the session timeout 
chan.settimeout(session_timeout)
# Open remote terminal
chan.get_pty()
# The activation terminal
chan.invoke_shell()
 And then you can go through chan.send('command') and chan.recv(recv_buffer) To execute commands remotely and get feedback locally. 
 Such as: 
chan.send('pwd')
print chan.recv(65535)

The point is that some commands take so long to execute that an inappropriate receive might not come back with the desired result, you can use time.sleep() to wait, or you can use some conditional loops.
Such as:


str=chan.recv(recv_buffer)
while not str.endswith('#'):
    str=chan.recv(recv_buffer)


Related articles: