示例#1
0
def setup_droplet():
    # Create a droplet
    do_key = configparse('DigitalOcean')['digitalocean_api_key']
    do = DO(do_key)
    #do.create_droplet()

    for droplet in do.get_all_droplets(tag_name="vpn-server"):
        ip = droplet.ip_address
    
    print("Waiting for setup of VPN")
    #sleep(35)

    # SSH接続
    path = configparse('SSH')['ssh_secretkey_path']
    
    print(path)
    ssh = SSH(ip,"root",path)
    ssh.connect

    ssh.excute_command("wget https://raw.githubusercontent.com/s151003/openvpn-install/master/openvpn-install.sh && chmod 777 openvpn-install.sh && bash openvpn-install.sh")
    ssh.close()
示例#2
0
class WebSSH(WebsocketConsumer):
    message = {'status': 0, 'message': None}
    """
    status:
        0: ssh 连接正常, websocket 正常
        1: 发生未知错误, 关闭 ssh 和 websocket 连接
    message:
        status 为 1 时, message 为具体的错误信息
        status 为 0 时, message 为 ssh 返回的数据, 前端页面将获取 ssh 返回的数据并写入终端页面
    """

    # status = {'code': 1001, 'error': ''}

    def connect(self):
        try:
            self.accept()
            query_string = self.scope['query_string']
            connect_argv = QueryDict(query_string=query_string,
                                     encoding='utf-8')
            server_id = connect_argv.get('server_id')
            user = connect_argv.get('user')
            width = connect_argv.get('width')
            height = connect_argv.get('height')

            width = int(width)
            height = int(height)

            connect_info = get_object_or_404(models.Server, id=server_id)

            host = connect_info.alias
            port = connect_info.ssh_port
            # auth = ''
            # pkey = ''

            # connect_info.delete()

            if user == str("root"):
                #     # password = base64.b64decode(pwd).decode('utf-8')
                password = connect_info.ssh_user_root_password
            else:
                password = connect_info.ssh_user_other_password

            self.ssh = SSH(websocker=self, message=self.message)
            # self.ssh = SSH(websocker=self)

            self.ssh.connect(host=host,
                             user=user,
                             password=password,
                             port=port,
                             pty_width=width,
                             pty_height=height)

            # if auth == 'key':
            #     pkey = pkey
            #     obj = StringIO()
            #     obj.write(pkey)
            #     obj.flush()
            #     obj.seek(0)
            #     self.pkey = obj
            #
            #     self.ssh.connect(
            #         host=host,
            #         user=user,
            #         password=password,
            #         pkey=self.pkey,
            #         port=port,
            #         pty_width=width,
            #         pty_height=height
            #     )
            # else:
            #     self.ssh.connect(
            #         host=host,
            #         user=user,
            #         password=password,
            #         port=port,
            #         pty_width=width,
            #         pty_height=height
            #     )
        except Exception as e:
            self.message['status'] = 1
            self.message['message'] = str(e)
            message = json.dumps(self.message)
            self.send(message)
            self.close()

    def disconnect(self, close_code):
        try:
            self.ssh.close()
        except:
            pass

    def receive(self, text_data=None, bytes_data=None):
        data = json.loads(text_data)
        if type(data) == dict:
            status = data['status']
            if status == 0:
                data = data['data']
                self.ssh.shell(data)
            else:
                cols = data['cols']
                rows = data['rows']
                self.ssh.resize_pty(cols=cols, rows=rows)