python linux interact with

# Coding. 8 = UTF- 
Import paramiko
 from Time Import SLEEP 

Transport = paramiko.Transport (( ' 192.168.1.58 ' , 22 is ))
 Print (Transport)       # Returns <paramiko.Transport at 0x5745ed0 (unconnected)> # At this time, you can see Transport is not connected 
transport.connect (username = ' the root ' , password = ' 123456 ' )
 Print (Transport)   # returns <paramiko.Transport at 0x5745ed0 (cipher aes128- ctr, 128 bits) (active; 0 open channel (s) ) after> # calls connect, then you can see Transport connection is successful, but active 0 indicates no channel open the
= Channel transport.open_session ()
 Print (Channel)       # Returns <paramiko.Channel 0 (open) window = 0 -> <paramiko.Transport at 0x5745ed0 (cipher aes128-ctr, 128 bits) (active; 1 open channel (s) ) >> # after the call open_session, active is 1, indicating open channel window = 0 but this passage also means that we can not receive data 
channel.get_pty ()      # activate the terminal, so that you can log on to the end, and we will use similar xshell to log the same. If you do not use this command you can only get back significantly after the command issued, will not display something other than echo such as "[root @ localhost ~] # " , etc. 
channel.invoke_shell ()
 Print (Channel)       # returns <0 paramiko.Channel (Open) = 2097152 window -> <paramiko.Transport AT 0x5745ed0 (CTR-cipher aes128, 128 bits) (Active; Open Channel. 1 (S)) >> 
                            # call invoke_shell () after, window! = 0 means that our channel is already available
SLEEP (2) # is displayed at one time to finish all echo, if not wait two seconds, acquired echo may be incomplete. 
= channel.recv_ready channelStatus1 ()       # Check whether there is the IO data path, if the False if not, the data can be used to determine whether the full echo 
Print (channelStatus1)      # This will return Treue 
backMsg = channel.recv (65535) .decode ( ' UTF-8 ' )       # get to the login information for #### stressed that only activates the terminal in order to obtain 
Print (backMsg) 
channelStatus2 = channel.recv_ready ()
 Print (channelStatus2) 
channel.send ( ' LS \ the n- ' )    #\ n corresponds to the case by Qiaoxia Enter # print (channel) command returns <paramiko.Channel 0 (open) window = 2097149 in-buffer = 67 -> <paramiko.Transport at 0x5745ed0 (cipher aes128-ctr, 128 bits) (Active; Open Channel. 1 (S)) >> 
Print (Channel)   # in this case a plurality of in-buffer = 67 67B represents the content of the echo 
SLEEP (2)     # is displayed at one time to finish all the echo if you do not wait two seconds, acquired echo may be incomplete. 
= channelStatus3 channel.recv_ready ()
 Print (channelStatus3) 
A = channel.recv (65535)      # a recv read in-buffer content, content size 65535 indicates pre-read, if the value is less than the in-buffer value, will be read incomplete 
Print (a)     # acquired content read 
Print (a.decode ( ' UTF-. 8 ' ))     #b recv value acquired character type is displayed, to display to the terminal of the linux pattern needs to be transcoded into. If we just want to get the results returned by the command can comment channel.get_pty () Code 
Print (Channel)   # read after, in view channel, found no in-buffer value 
channel.close () 
transport.close ()

 

Guess you like

Origin www.cnblogs.com/testlearn/p/11775848.html