blob: 5fa0bc49d1c7f696ef2f46c4f5b18805c12af892 [file] [log] [blame]
Radek Krejcib5ac0602017-10-17 10:35:03 +02001#!/usr/bin/python3
2
3import sys
4import os
5import getpass
6import libyang as ly
7import netconf2 as nc
8
9def interactive_auth(name, instruct, prompt, data):
10 print(name)
11 return getpass.getpass(prompt)
12
13def password_auth(user, host, data):
14 return getpass.getpass((user if user else os.getlogin()) + '@' + host + ' password : ')
15
16#
17# get know where to connect
18#
19host = input("hostname: ")
20try:
21 port = int(input("port : "))
22except:
23 port = 0;
24user = input("username: ")
25
26#
27# set SSH settings
28#
29if user:
30 ssh = nc.SSH(username=user)
31else:
32 ssh = nc.SSH()
33ssh.setAuthInteractiveClb(interactive_auth)
34ssh.setAuthPasswordClb(password_auth)
35
36#
37# create NETCONF session to the server
38#
39try:
40 session = nc.Session(host, port, ssh)
41except Exception as e:
42 print(e)
43 sys.exit(1)
44
45# perform <get> and print result
46try:
47 data = session.rpcGet()
48except nc.ReplyError as e:
49 reply = {'success':False, 'error': []}
50 for err in e.args[0]:
51 reply['error'].append(json.loads(str(err)))
52 print(json.dumps(reply))
53 sys.exit(1)
54
55print(data.print_mem(ly.LYD_XML, ly.LYP_FORMAT | ly.LYP_WITHSIBLINGS))