码迷,mamicode.com
首页 > 编程语言 > 详细

python

时间:2016-05-13 15:38:20      阅读:248      评论:0      收藏:0      [点我收藏+]

标签:password   python   justin   return   import   

import socket
import paramiko
import threading
import sys

host_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_FATLED_ADMINISTRATIVELY_PROHIBITED
    def check_auth_password(self,username,password):
        if(username == ‘justin‘) and (password == ‘lovesthepython‘):
            return paramiko.AUTH_SUCCESSFUL
        return paramiko.AUTH_FAILED

server = 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.Transport(client)
    bhSession.add_server_key(host_key)
    server = Server()
    try:
        bhSession.start_server(server=server)
    except paramiko.SSHExecption,x:
        print ‘[-] SSH negotiation failed.‘
    chan = bhSession.accept(20)
    print ‘[+] Authenticated!‘
    print chan.recv(1024)
    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(1024)+‘\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:
        pass
    sys.exit(1)


本文出自 “专注php” 博客,请务必保留此出处http://jingshanls.blog.51cto.com/3357095/1772849

python

标签:password   python   justin   return   import   

原文地址:http://jingshanls.blog.51cto.com/3357095/1772849

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!