1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
|
# importing modules
import warnings
warnings.filterwarnings("ignore", category=RuntimeWarning)
import os
# adding pyvyos to sys.path
# sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__))))
from dotenv import load_dotenv
import pprint
import random
import string
# importing pyvyos modules
from pyvyos.device import VyDevice
# getting env variables
load_dotenv()
hostname = os.getenv("VYDEVICE_HOSTNAME")
apikey = os.getenv("VYDEVICE_APIKEY")
port = os.getenv("VYDEVICE_PORT")
protocol = os.getenv("VYDEVICE_PROTOCOL")
verify = os.getenv("VYDEVICE_VERIFY_SSL")
if verify == "False":
verify = False
else:
verify = True
# running example
if __name__ == "__main__":
# preparing connection to vyos device
device = VyDevice(
hostname=hostname,
apikey=apikey,
port=port,
protocol=protocol,
verify=verify,
timeout=60,
)
response = device.retrieve_show_config(["system"])
pprint.pprint(response)
response = device.configure_set(
path=["interfaces", "dummy", "dum1", "address", "192.168.56.100/24"]
)
pprint.pprint(response)
response = device.retrieve_return_values(
path=["interfaces", "dummy", "dum1", "address"]
)
pprint.pprint(response)
response = device.configure_delete(path=["interfaces", "dummy", "dum1"])
pprint.pprint(response)
randstring = "".join(
random.choice(string.ascii_letters + string.digits) for _ in range(20)
)
keyrand = f"/tmp/key_{randstring}"
response = device.generate(path=["ssh", "client-key", keyrand])
pprint.pprint(response)
response = device.show(path=["system", "image"])
pprint.pprint(response)
response = device.reset(path=["conntrack-sync", "internal-cache"])
pprint.pprint(response)
response = device.config_file_save(file="/config/test300.config")
pprint.pprint(response)
response = device.config_file_load(file="/config/test300.config")
pprint.pprint(response)
response = device.configure_multiple_op(
op_path=[
{
"op": "set",
"path": ["interfaces", "dummy", "dum1", "address", "192.168.56.100/24"],
},
{
"op": "delete",
"path": ["interfaces", "dummy", "dum1"],
},
]
)
pprint.pprint(response)
# print("### Generating ssh key ###")
# randstring = ''.join(random.choice(string.ascii_letters + string.digits) for _ in range(20))
# keyrand = f'/tmp/key_{randstring}'
# response = device.generate(path=["ssh", "client-key", keyrand])
# pprint.pprint(response)
# response = device.retrieve_return_values(path=["interfaces", "ethernet", "eth0", "address"])
# pprint.pprint(response)
# response = device.reset(path=["conntrack-sync", "internal-cache"])
# pprint.pprint(response)
# response = device.reboot(path=["now"])
# pprint.pprint(response)
# response = device.shutdown(path=["now"])
# pprint.pprint(response)
# response = device.image_add(url="https://github.com/vyos/vyos-rolling-nightly-builds/releases/download/1.5-rolling-202312130023/vyos-1.5-rolling-202312130023-amd64.iso")
# pprint.pprint(response)
# response = device.image_delete(name="foo")
# pprint.pprint(response)
|