Carried out using the python ssh linux server and execute the command

1, the installation module paramiko

    pip install paramiko

   Ssh connection timeout may be modified, the windows path: the installation path Python \ Python36 \ Lib \ site-packages \ paramiko \ transport.py, modify self.banner_timeout = 60 (ssh timeout set to 60 seconds)

2, performed by using ssh python

Import paramiko, the getpass   # the getpass hidden code 

DEF ssh_connect (password): 
    host_ip = ' 192.168.0.150 ' 
    USER_NAME = ' the root ' 
    host_port = ' 22 is ' 

    # command to be executed 
    sed_command = " Sed -i' S / 123 / ABC / G '/root/test/test.txt " 
    ls_command = " LS / the root / Test / " 

    # Note: when performing a plurality of commands in sequence, separated by semicolons between command 
    command = sed_command + " ; "+ Ls_command 

    # SSH remote connections
    = paramiko.SSHClient SSH ()    # create sshclient 
    ssh.set_missing_host_key_policy (paramiko.AutoAddPolicy ())   # specify when the other host does not have this machine public key situation should be how to do, AutoAddPolicy expressed in other host keys automatically saved under the machine 
    ssh.connect (host_ip, host_port, user_name, password) 

    # execute commands and get the results 
    stdin, stdout, stderr = ssh.exec_command (the command) 
    OUT = stdout.readlines () 
    ERR = stderr.readlines () 
    
    ssh.close () 

    return OUT, ERR 



IF  the __name__ == ' __main__ ' : 
    pwd = getpass.getpass ( " enter password: ")
    result = ssh_connect(pwd)
    print(result)

Guess you like

Origin www.cnblogs.com/longBlogs/p/10993815.html