The applet is as follows:
#!/usr/bin/env python3#-*-coding:utf-8-*-import paramikored_color= ' \033[1;31;48m ' #红, configure the terminal output Color blue_color= ' \033[1; 34;48m ' #蓝, configure the color of the terminal output res= ' \033[0m ' def ssh_pkey (host,username,pkey,port=22): #基于用户名密钥连接并执行命令 Try:private_key = Paramiko. Rsakey.from_private_key_file (Pkey) # create ssh object ssh = Paramiko. Sshclient () # allows connections to host Ssh.set_missing_host_key_policy (Paramiko) that are not in the Know_hosts file. Autoaddpolicy ()) # Connection Server Ssh.connect (Hostname=host, Port=port, Username=username,key_filename=pkey) # Execute command Print ("%s successfully logged on to server%s, enter the action command!") %s "% (Red_color,host,res)) while True:cmd=input ('%s Enter input operation command:%s '% (blue_color,res)) if cmd== ' Exit ': Print (%s execute%s command exits!) %s "% (red_color,cmd,res)) # Close Connection Ssh.close () Break Else: stdin, stdout, stderr = Ssh.exec_command (CMD) print ("%s Execute command%s return result:%s"% (red_color,cmd,res)) ForI in Stdout.read (). Splitlines (): Print (I.decode (' Utf-8 ')) except:print ("%sssh telnet failed, quit the program! \ n "% (red_color,res)) if __name__ = = ' __main__ ': host= ' 59.1.1.1 ' username= ' root ' ssh_key=r ' D:\Identity2048 ' P ORT = 6382 Ssh_pkey (host=host,username=username,pkey=ssh_key,port=port)
As follows: