Using the Paramiko Interactive applet

Source: Internet
Author: User
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:

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.