How to use Bh_ssh server in Python

Source: Internet
Author: User

Import socketimport paramikoimport threadingimport syshost_key = paramiko. Rsakey (filename= ' Test_rsa.key ') class server  (Paramiko. Serverinterface):     def __init__ (self):         self.event = threading. Event ()     def check_channel_request (Self,kind,chanid):         if kind ==  ' Session ':             return paramiko. Open_succeeded        return paramiko. Open_failed_administraively_prohibited    def check_auth_password (Self,username, Password):        if  (username ==  ' Justin ')  and   (password ==  ' Lovesthepython '):             return parAmiko. Auth_successful        return paramiko. Auth_failedserver = sys.argv[1]ssh_port = int (sys.argv[2]) Try:    sock  = socket.socket (Socket.af_inet,socket. SOCK_STREAM)     sock.setsockopt (socket. Sol_socket,socket. so_reuseaddr,1)     sock.bind ((Server,ssh_port))     sock.listen (100)     print  ' [+] listening for connection ... '      Client,addr = sock.accept () except exception,e:    print  ' [-]  listen failed: ' +str (e)     sys.exit (1) print  ' [+] got a connection! ' Try:    bhsession = paramiko. Trasport (client)     bhsession.add_server_key (Host_key)     server  = server ()     try:     &Nbsp;  bhsession.start_server (Server=server)     except paramiko. sshexception,x:        print  ' [-] ssh negotiation  Failed. '     chan = bhsession.accept (    print  ' [+]  authenticated! '     PRINT CHAN.RECV (1024x768)     chan.send (' Welcome to bh_ SSH ')     while True:        try:             command = raw_input ("Enter command: "). Strip (' \ n ')             if command !=   ' exit ':                 Chan.send (command)                  prINT CHAN.RECV (1024x768) + ' \ n '             else:                 chan.send (' Exit ')                 print   ' exiting '                  bhsession.close ()                  raise exception (' exit ')         except  Keyboardinterrupt:            bhsession.close () except exception,e:    print  ' [-] caught exception: ' +str (e)      try:        bhsession.close ()      except:      &nBsp; pass    sys.exit (1) 


This article is from the "Focus on PHP" blog, please be sure to keep this source http://jingshanls.blog.51cto.com/3357095/1766002

How to use Bh_ssh server in Python

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.