コード例 #1
0
ファイル: connections.py プロジェクト: jeson1981/Netopeer2GUI
def connect():
	session = auth.lookup(request.headers.get('lgui-Authorization', None))
	user = session['user']
	path = os.path.join(INVENTORY, user.username)

	data = request.get_json()
	if 'id' in data:
		# stored device
		device = devices_get(data['id'], user.username)
	elif 'device' in data:
		# one-time connect, the device is specified in request
		device = data['device']
	else:
		raise NetopeerException('Invalid connection request.')

	if not device:
		raise NetopeerException('Unknown device to connect to request.')

	nc.setSearchpath(path)
	nc.setSchemaCallback(getschema, session)

	if 'password' in device:
		ssh = nc.SSH(device['username'], password = device['password'])
	else:
		ssh = nc.SSH(device['username'])
		ssh.setAuthPasswordClb(auth_password, session['session_id'])
		ssh.setAuthInteractiveClb(auth_interactive, session['session_id'])

	ssh.setAuthHostkeyCheckClb(hostkey_check, {'session': session, 'device' : device})
	try:
		ncs = nc.Session(device['hostname'], device['port'], ssh)
	except Exception as e:
		nc.setSchemaCallback(None)
		return(json.dumps({'success': False, 'error-msg': str(e)}))
	nc.setSchemaCallback(None)

	if not user.username in sessions:
		sessions[user.username] = {}

	# use key (as hostname:port:session-id) to store the created NETCONF session
	key = ncs.host + ":" + str(ncs.port) + ":" + ncs.id
	sessions[user.username][key] = {}
	sessions[user.username][key]['session'] = ncs

	# update inventory's list of schemas
	schemas_update(session)

	return(json.dumps({'success': True, 'session-key': key}))
コード例 #2
0
def connect_device():
    global sessions
    session = auth.lookup(request.headers.get('lgui-Authorization', None))
    username = str(session['user'].username)
    data = request.get_json()

    nc.setSchemaCallback(get_schema, session)
    site_root = os.path.realpath(os.path.dirname(__file__))
    path = os.path.join(site_root, 'userfiles', username)
    if not os.path.exists(path):
        os.makedirs(path)
    nc.setSearchpath(path)
    if 'password' in data and data['password'] != '':
        ssh = nc.SSH(data['username'], password=data['password'])
    else:
        ssh = nc.SSH(data['username'])
        ssh.setAuthPasswordClb(auth_password, session['session_id'])
        ssh.setAuthInteractiveClb(func=auth_interactive,
                                  priv={
                                      'id': session['session_id'],
                                      'device': data
                                  })

    ssh.setAuthHostkeyCheckClb(hostkey_check, {
        'session': session,
        'device': data
    })

    try:
        ncs = nc.Session(data['hostname'], int(data['port']), ssh)
    except Exception as e:
        nc.setSchemaCallback(None)
        return json.dumps({'success': False, 'code': 500, 'message': str(e)})
    nc.setSchemaCallback(None)

    if username not in sessions:
        sessions[username] = {}

    # use key (as hostname:port:session-id) to store the created NETCONF session
    key = ncs.host + ":" + str(ncs.port) + ":" + ncs.id
    sessions[username][key] = {}
    sessions[username][key]['session'] = ncs

    # update inventory's list of schemas
    # schemas_update(session)

    return json.dumps({'success': True, 'session-key': key})
コード例 #3
0
def connect():
    session = auth.lookup(request.headers.get('Authorization', None))
    user = session['user']
    path = os.path.join(INVENTORY, user.username)

    data = request.get_json()
    if 'id' in data:
        # stored device
        device = devices_get(data['id'], user.username)
    elif 'device' in data:
        # one-time connect, the device is specified in request
        device = data['device']
    else:
        raise NetopeerException('Invalid connection request.')

    if not device:
        raise NetopeerException('Unknown device to connect to request.')

    nc.setSearchpath(path)

    ssh = nc.SSH(device['username'], password=device['password'])
    ssh.setAuthHostkeyCheckClb(hostkey_check)
    try:
        session = nc.Session(device['hostname'], device['port'], ssh)
    except Exception as e:
        return (json.dumps({'success': False, 'error-msg': str(e)}))

    if not user.username in sessions:
        sessions[user.username] = {}

    # use key (as hostname:port:session-id) to store the created NETCONF session
    key = session.host + ":" + str(session.port) + ":" + session.id
    sessions[user.username][key] = {}
    sessions[user.username][key]['session'] = session

    return (json.dumps({'success': True, 'session-key': key}))
コード例 #4
0
#
# get know where to connect
#
host = input("hostname: ")
try:
    port = int(input("port    : "))
except:
    port = 0
user = input("username: ")

#
# set SSH settings
#
if user:
    ssh = nc.SSH(username=user)
else:
    ssh = nc.SSH()
ssh.setAuthInteractiveClb(interactive_auth)
ssh.setAuthPasswordClb(password_auth)
ssh.setAuthHostkeyCheckClb(hostkey_check)

#
# create NETCONF session to the server
#
try:
    session = nc.Session(host, port, ssh)
except Exception as e:
    print(e)
    sys.exit(1)