blob: cc8a96024edd4b8d77e9db60b3974a528aa86fff [file] [log] [blame]
Radek Krejcib0bf24c2018-02-07 16:40:27 +01001#!/usr/bin/python3
2
3import sys
4import os
5import getpass
Radek Krejcicd6cd602018-02-08 12:53:28 +01006import json
Radek Krejcieed7f872018-02-13 16:57:42 +01007import yang
Radek Krejcib0bf24c2018-02-07 16:40:27 +01008import netconf2 as nc
9
10def interactive_auth(name, instruct, prompt, data):
11 print(name)
12 return getpass.getpass(prompt)
13
14def password_auth(user, host, data):
15 return getpass.getpass((user if user else os.getlogin()) + '@' + host + ' password : ')
16
17def hostkey_check(hostname, state, keytype, hexa, priv):
18 return True
19
20#
21# get know where to connect
22#
23host = input("hostname: ")
24try:
25 port = int(input("port : "))
26except:
27 port = 0;
28user = input("username: ")
29
30#
31# set SSH settings
32#
33if user:
34 ssh = nc.SSH(username=user)
35else:
36 ssh = nc.SSH()
37ssh.setAuthInteractiveClb(interactive_auth)
38ssh.setAuthPasswordClb(password_auth)
39ssh.setAuthHostkeyCheckClb(hostkey_check)
40
41#
42# create NETCONF session to the server
43#
44try:
45 session = nc.Session(host, port, ssh)
46except Exception as e:
47 print(e)
48 sys.exit(1)
49
50# prepare config content as string or data tree
51tm = 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 Krejcieed7f872018-02-13 16:57:42 +010053config = yang.Data_Node(session.context, "/turing-machine:turing-machine/transition-function/delta[label='left summand']/input/state", "5", 0, 0)
Radek Krejcib0bf24c2018-02-07 16:40:27 +010054
55# perform <edit-config> and print result
56try:
57 session.rpcEditConfig(nc.DATASTORE_RUNNING, config)
58except 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))