Radek Krejci | b0bf24c | 2018-02-07 16:40:27 +0100 | [diff] [blame] | 1 | #!/usr/bin/python3 |
| 2 | |
| 3 | import sys |
| 4 | import os |
| 5 | import getpass |
Radek Krejci | cd6cd60 | 2018-02-08 12:53:28 +0100 | [diff] [blame] | 6 | import json |
Radek Krejci | eed7f87 | 2018-02-13 16:57:42 +0100 | [diff] [blame] | 7 | import yang |
Radek Krejci | b0bf24c | 2018-02-07 16:40:27 +0100 | [diff] [blame] | 8 | import netconf2 as nc |
| 9 | |
| 10 | def interactive_auth(name, instruct, prompt, data): |
| 11 | print(name) |
| 12 | return getpass.getpass(prompt) |
| 13 | |
| 14 | def password_auth(user, host, data): |
| 15 | return getpass.getpass((user if user else os.getlogin()) + '@' + host + ' password : ') |
| 16 | |
| 17 | def hostkey_check(hostname, state, keytype, hexa, priv): |
| 18 | return True |
| 19 | |
| 20 | # |
| 21 | # get know where to connect |
| 22 | # |
| 23 | host = input("hostname: ") |
| 24 | try: |
| 25 | port = int(input("port : ")) |
| 26 | except: |
| 27 | port = 0; |
| 28 | user = input("username: ") |
| 29 | |
| 30 | # |
| 31 | # set SSH settings |
| 32 | # |
| 33 | if user: |
| 34 | ssh = nc.SSH(username=user) |
| 35 | else: |
| 36 | ssh = nc.SSH() |
| 37 | ssh.setAuthInteractiveClb(interactive_auth) |
| 38 | ssh.setAuthPasswordClb(password_auth) |
| 39 | ssh.setAuthHostkeyCheckClb(hostkey_check) |
| 40 | |
| 41 | # |
| 42 | # create NETCONF session to the server |
| 43 | # |
| 44 | try: |
| 45 | session = nc.Session(host, port, ssh) |
| 46 | except Exception as e: |
| 47 | print(e) |
| 48 | sys.exit(1) |
| 49 | |
| 50 | # prepare config content as string or data tree |
| 51 | tm = session.context.get_module("turing-machine") |
| 52 | # config = "<turing-machine xmlns=\"http://example.net/turing-machine\"><transition-function><delta><label>left summand</label><input><state>0</state></input></delta></transition-function></turing-machine>" |
Radek Krejci | eed7f87 | 2018-02-13 16:57:42 +0100 | [diff] [blame] | 53 | config = yang.Data_Node(session.context, "/turing-machine:turing-machine/transition-function/delta[label='left summand']/input/state", "5", 0, 0) |
Radek Krejci | b0bf24c | 2018-02-07 16:40:27 +0100 | [diff] [blame] | 54 | |
| 55 | # perform <edit-config> and print result |
| 56 | try: |
| 57 | session.rpcEditConfig(nc.DATASTORE_RUNNING, config) |
| 58 | except nc.ReplyError as e: |
| 59 | reply = {'success':False, 'error': []} |
| 60 | for err in e.args[0]: |
| 61 | reply['error'].append(json.loads(str(err))) |
| 62 | print(json.dumps(reply)) |
| 63 | sys.exit(1) |
| 64 | |
| 65 | # print(data.print_mem(ly.LYD_XML, ly.LYP_FORMAT | ly.LYP_WITHSIBLINGS)) |