Radek Krejci | b5ac060 | 2017-10-17 10:35:03 +0200 | [diff] [blame] | 1 | #!/usr/bin/python3 |
| 2 | |
| 3 | import sys |
| 4 | import os |
| 5 | import getpass |
Radek Krejci | eed7f87 | 2018-02-13 16:57:42 +0100 | [diff] [blame] | 6 | import yang |
Radek Krejci | b5ac060 | 2017-10-17 10:35:03 +0200 | [diff] [blame] | 7 | import netconf2 as nc |
| 8 | |
| 9 | def interactive_auth(name, instruct, prompt, data): |
| 10 | print(name) |
| 11 | return getpass.getpass(prompt) |
| 12 | |
| 13 | def password_auth(user, host, data): |
| 14 | return getpass.getpass((user if user else os.getlogin()) + '@' + host + ' password : ') |
| 15 | |
Radek Krejci | b0bf24c | 2018-02-07 16:40:27 +0100 | [diff] [blame] | 16 | def hostkey_check(hostname, state, keytype, hexa, priv): |
| 17 | return True |
| 18 | |
Radek Krejci | b5ac060 | 2017-10-17 10:35:03 +0200 | [diff] [blame] | 19 | # |
| 20 | # get know where to connect |
| 21 | # |
| 22 | host = input("hostname: ") |
| 23 | try: |
| 24 | port = int(input("port : ")) |
| 25 | except: |
| 26 | port = 0; |
| 27 | user = input("username: ") |
| 28 | |
| 29 | # |
| 30 | # set SSH settings |
| 31 | # |
| 32 | if user: |
| 33 | ssh = nc.SSH(username=user) |
| 34 | else: |
| 35 | ssh = nc.SSH() |
| 36 | ssh.setAuthInteractiveClb(interactive_auth) |
| 37 | ssh.setAuthPasswordClb(password_auth) |
Radek Krejci | b0bf24c | 2018-02-07 16:40:27 +0100 | [diff] [blame] | 38 | ssh.setAuthHostkeyCheckClb(hostkey_check) |
Radek Krejci | b5ac060 | 2017-10-17 10:35:03 +0200 | [diff] [blame] | 39 | |
| 40 | # |
| 41 | # create NETCONF session to the server |
| 42 | # |
| 43 | try: |
| 44 | session = nc.Session(host, port, ssh) |
| 45 | except Exception as e: |
| 46 | print(e) |
| 47 | sys.exit(1) |
| 48 | |
| 49 | # perform <get> and print result |
| 50 | try: |
| 51 | data = session.rpcGet() |
| 52 | except 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 Krejci | eed7f87 | 2018-02-13 16:57:42 +0100 | [diff] [blame] | 59 | print(data.print_mem(yang.LYD_XML, yang.LYP_FORMAT | yang.LYP_WITHSIBLINGS)) |