blob: b69cfbbe3589484dfcc877f7689c81329fbbf2ba [file] [log] [blame]
Radek Krejcib5ac0602017-10-17 10:35:03 +02001#!/usr/bin/python3
2
3import sys
4import os
5import getpass
Radek Krejcieed7f872018-02-13 16:57:42 +01006import yang
Radek Krejcib5ac0602017-10-17 10:35:03 +02007import 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
Radek Krejcib0bf24c2018-02-07 16:40:27 +010016def hostkey_check(hostname, state, keytype, hexa, priv):
17 return True
18
Radek Krejcib5ac0602017-10-17 10:35:03 +020019#
20# get know where to connect
21#
22host = input("hostname: ")
23try:
24 port = int(input("port : "))
25except:
26 port = 0;
27user = input("username: ")
28
29#
30# set SSH settings
31#
32if user:
33 ssh = nc.SSH(username=user)
34else:
35 ssh = nc.SSH()
36ssh.setAuthInteractiveClb(interactive_auth)
37ssh.setAuthPasswordClb(password_auth)
Radek Krejcib0bf24c2018-02-07 16:40:27 +010038ssh.setAuthHostkeyCheckClb(hostkey_check)
Radek Krejcib5ac0602017-10-17 10:35:03 +020039
40#
41# create NETCONF session to the server
42#
43try:
44 session = nc.Session(host, port, ssh)
45except Exception as e:
46 print(e)
47 sys.exit(1)
48
49# perform <get> and print result
50try:
51 data = session.rpcGet()
52except nc.ReplyError as e:
53 reply = {'success':False, 'error': []}
54 for err in e.args[0]:
55 reply['error'].append(json.loads(str(err)))
56 print(json.dumps(reply))
57 sys.exit(1)
58
Radek Krejcieed7f872018-02-13 16:57:42 +010059print(data.print_mem(yang.LYD_XML, yang.LYP_FORMAT | yang.LYP_WITHSIBLINGS))