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 |
| 6 | import libyang as ly |
| 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 | |
| 16 | # |
| 17 | # get know where to connect |
| 18 | # |
| 19 | host = input("hostname: ") |
| 20 | try: |
| 21 | port = int(input("port : ")) |
| 22 | except: |
| 23 | port = 0; |
| 24 | user = input("username: ") |
| 25 | |
| 26 | # |
| 27 | # set SSH settings |
| 28 | # |
| 29 | if user: |
| 30 | ssh = nc.SSH(username=user) |
| 31 | else: |
| 32 | ssh = nc.SSH() |
| 33 | ssh.setAuthInteractiveClb(interactive_auth) |
| 34 | ssh.setAuthPasswordClb(password_auth) |
| 35 | |
| 36 | # |
| 37 | # create NETCONF session to the server |
| 38 | # |
| 39 | try: |
| 40 | session = nc.Session(host, port, ssh) |
| 41 | except Exception as e: |
| 42 | print(e) |
| 43 | sys.exit(1) |
| 44 | |
| 45 | # perform <get> and print result |
| 46 | try: |
| 47 | data = session.rpcGet() |
| 48 | except 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 | |
| 55 | print(data.print_mem(ly.LYD_XML, ly.LYP_FORMAT | ly.LYP_WITHSIBLINGS)) |