Skip to content

Commit

Permalink
v0.1.9
Browse files Browse the repository at this point in the history
  • Loading branch information
ji-podhead committed Nov 10, 2024
1 parent 1e907a9 commit 0446009
Show file tree
Hide file tree
Showing 16 changed files with 207 additions and 104 deletions.
64 changes: 64 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# Verschlüsselte Daten
*.key
*.pem
*.pfx
*.crt
*.certbot

# Kompilierte Dateien
__pycache__
*.py[cod]
*$py.class

# Log-Dateien
*.log

# Temporäre Dateien
*.tmp
*.swp
*.swo

# IDE-Spezifische Dateien
.vscode/
.idea/

# Python-Verpackungen
dist/
build/
*.egg-info

# Virtual Environment
venv/
env/
ENV/

# Lokale Entwicklungsskripte
*.pyc
__init__.py*
*.so
*.dylib
*.dll

# Test-Ausgaben
test_*.out
*.test

# Build-Dateien
*.exe
*.exe~
*.bin
*.class

# Verschlüsselte Dateien
*.gpg
*.asc

# Archivierte Dateien
*.tar.gz
*.tar.bz2
*.tar.xz
*.zip

# Docker-Dateien
.DS_Store
Thumbs.db
52 changes: 47 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,60 @@
# opnsense-helper
assign lan interfaces, create vlans, vlan-interfaces and setup dhcp in a single script.


## install
## pip
```bash
pip install opnsense-helper
```


## usage
- before you create Vlan-Interfaces you need to add them first using the add_vlan method
- you can also add them via config file but this requires a reboot and it's not yet implemented
- rn i try to find out how to reconfigure the vlans using the backend, but this is on todo list

```python
from opnsense_helper.classes import Opnsense_Helper

filepath = '/home/ji/confignew.xml'
output="./config.xml"
conf_path="/conf/config.xml"
vlans=[
{'if': 'vtnet1', 'tag': '1', 'pcp': '0', 'proto': None, 'descr': 'vlan1', 'vlanif': 'vlan0.1'},
{'if': 'vtnet1', 'tag': '2', 'pcp': '0', 'proto': None, 'descr': 'vlan2', 'vlanif': 'vlan0.2'}
]
dhcp={
"opt2": {'enable': '1', 'ddnsdomainalgorithm': 'hmac-md', "range":{'from': '200.0.3.10', '_to': '200.0.3.100'}},
"opt3": {'enable': '1', 'ddnsdomainalgorithm': 'hmac-md', "range":{'from': '200.0.4.10', '_to': '200.0.4.100'}}
}
interfaces={
"opt1": {'descr': 'router', 'enable': '1', 'ipaddr': None, 'subnet': None, 'type': None, 'virtual': None, 'spoofmac': '00:00:00:00:02:01',"interface":"vtnet1"},
"opt2": {'descr': 'vlan1', 'enable': '1', 'ipaddr': '200.0.3.1', 'subnet': '24', 'type': None, 'virtual': None, 'spoofmac': '00:00:00:00:00:01',"interface":"vlan0.1"},
"opt3": {'descr': 'vlan2', 'enable': '1', 'ipaddr': '200.0.4.1', 'subnet': '24', 'type': None, 'virtual': None, 'spoofmac': '00:00:00:00:00:02', "interface":"vlan0.2"}
}
ssh_auth={
"user":"root",
"passw":"opnsense",
}
api_auth={
# ONLY NEED WHEN YOU USE THE API
"api_key" :'ejl4fIU9yfNk+gaQmPk/rqIa15f1yX1snIKgcIEl2QNoJwhbekraWIE0ANRYceh9hey5IFGzlf3da4yJ',
"api_secret" : '5JVVGoatPbaAA+FozLDQY92/T6sRlmKD1+aRNl/YI8KA9/0TNiTDboLveqvd9FU8wFeDo3D3DY5wrUtF',
"ssl": True,
"verify": False
}
host= "192.168.1.103"
def test():
helper=Opnsense_Helper(host=host,ssh_auth=ssh_auth,api_auth=api_auth,filepath=output, verbose=False)
helper.add_vlans(vlans)
#helper.set_vlans(vlans)
helper.get_conf(conf_path)
helper.initialize()
helper.add_Items("interfaces",interfaces)
helper.add_Items("dhcpd",dhcp)
helper.save(output)
helper.put_file(output,conf_path)
helper.close_con()

if __name__ == "__main__":
test()
```

### contribute
- clone, or fork `[email protected]:the-pod-shop/opnsense-helper.git`
Expand Down
48 changes: 33 additions & 15 deletions examples/add_vlans.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,42 @@
from opnsense_helper.classes import Opnsense_Helper

filepath = '/home/ji/confignew.xml'
output="/home/ji/.ansible/collections/ansible_collections/ji_podhead/podnet/plugins/x.xml"
output="./config.xml"
conf_path="/conf/config.xml"
vlan3 ={'parentinterface': 'vtnet1', 'tag': '3', 'pcp': '0', 'proto': None, 'descr': 'vlan3', 'vlanif': 'vlan0.3',"attr":{"uuid":"cb503df8-821d-4acd-86ba-66b35e4df17n"}}
vlan4 ={'parentinterface': 'vtnet1', 'tag': '4', 'pcp': '0', 'proto': None, 'descr': 'vlan4', 'vlanif': 'vlan0.4',"attr":{"uuid":"cb503df8-821d-4acd-86ba-66b35e4s7c"}}
opt3_dhcp= {'enable': '1', 'ddnsdomainalgorithm': 'hmac-md', "range":{'from': '200.0.3.10', '_to': '200.0.3.100'},"attr":None}
opt4_dhcp= {'enable': '1', 'ddnsdomainalgorithm': 'hmac-md', "range":{'from': '200.0.4.10', '_to': '200.0.4.100'},"attr":None}
opt3_interface= {'descr': 'vlan3', 'enable': '1', 'ipaddr': '200.0.3.1', 'subnet': '24', 'type': None, 'virtual': None, 'spoofmac': '00:00:00:00:00:03', 'attr': {}}
opt4_interface= {'descr': 'vlan4', 'enable': '1', 'ipaddr': '200.0.3.1', 'subnet': '24', 'type': None, 'virtual': None, 'spoofmac': '00:00:00:00:00:04', 'attr': {}}
vlans=[
{'if': 'vtnet1', 'tag': '1', 'pcp': '0', 'proto': None, 'descr': 'vlan1', 'vlanif': 'vlan0.1'},
{'if': 'vtnet1', 'tag': '2', 'pcp': '0', 'proto': None, 'descr': 'vlan2', 'vlanif': 'vlan0.2'}
]
dhcp={
"opt2": {'enable': '1', 'ddnsdomainalgorithm': 'hmac-md', "range":{'from': '200.0.3.10', '_to': '200.0.3.100'}},
"opt3": {'enable': '1', 'ddnsdomainalgorithm': 'hmac-md', "range":{'from': '200.0.4.10', '_to': '200.0.4.100'}}
}
interfaces={
"opt1": {'descr': 'router', 'enable': '1', 'ipaddr': None, 'subnet': None, 'type': None, 'virtual': None, 'spoofmac': '00:00:00:00:02:01',"interface":"vtnet1"},
"opt2": {'descr': 'vlan1', 'enable': '1', 'ipaddr': '200.0.3.1', 'subnet': '24', 'type': None, 'virtual': None, 'spoofmac': '00:00:00:00:00:01',"interface":"vlan0.1"},
"opt3": {'descr': 'vlan2', 'enable': '1', 'ipaddr': '200.0.4.1', 'subnet': '24', 'type': None, 'virtual': None, 'spoofmac': '00:00:00:00:00:02', "interface":"vlan0.2"}
}

ssh_auth={
"user":"root",
"passw":"opnsense",
}
api_auth={
# ONLY NEED WHEN YOU USE THE API
"api_key" :'ejl4fIU9yfNk+gaQmPk/rqIa15f1yX1snIKgcIEl2QNoJwhbekraWIE0ANRYceh9hey5IFGzlf3da4yJ',
"api_secret" : '5JVVGoatPbaAA+FozLDQY92/T6sRlmKD1+aRNl/YI8KA9/0TNiTDboLveqvd9FU8wFeDo3D3DY5wrUtF',
"ssl": True,
"verify": False
}
host= "192.168.1.103"
def test():
helper=Opnsense_Helper(filepath,True,"root","opnsense","192.168.1.103")
helper.get_file(conf_path, output)
helper=Opnsense_Helper(host=host,ssh_auth=ssh_auth,api_auth=api_auth,filepath=output, verbose=False)
helper.add_vlans(vlans)
#helper.set_vlans(vlans)
helper.get_conf(conf_path)
helper.initialize()
helper.objects["dhcpd"]["opt3"]=opt3_dhcp
helper.objects["dhcpd"]["opt4"]=opt4_dhcp
helper.objects["vlans"]["vlan3"]=vlan3
helper.objects["vlans"]["vlan4"]=vlan4
helper.objects["interfaces"]["opt3"]=opt3_interface
helper.objects["interfaces"]["opt4"]=opt4_interface
helper.add_Items("interfaces",interfaces)
helper.add_Items("dhcpd",dhcp)
helper.save(output)
helper.put_file(output,conf_path)
helper.close_con()
Expand Down
120 changes: 52 additions & 68 deletions python/README.md
Original file line number Diff line number Diff line change
@@ -1,83 +1,62 @@
# opnsense-helper
assign lan interfaces, create vlans, vlan-interfaces and setup dhcp in a single script.


## install
## pip
```bash
pip install opnsense-helper
```

## usage

### python

- import and create object
```python
from opnsense_helper.opnsense_helper import OpennsenseHelper
helper=OpennsenseHelper(filepath,True,"root","opnsense","192.168.1.103")
```
- add some vars we need later
- before you create Vlan-Interfaces you need to add them first using the add_vlan method
- rn i try to find out how to reconfigure the vlans using the backend, but this is on todo list

```python
from opnsense_helper.classes import Opnsense_Helper

filepath = '/home/ji/confignew.xml'
output="/home/ji/.ansible/collections/ansible_collections/ji_podhead/podnet/plugins/x.xml"
output="./config.xml"
conf_path="/conf/config.xml"

# the variables of the things we will modif
vlan3 ={'parentinterface': 'vtnet1', 'tag': '3', 'pcp': '0', 'proto': None, 'descr': 'vlan3', 'vlanif': 'vlan0.3',"attr":{"uuid":"cb503df8-821d-4acd-86ba-66b35e4df17n"}}
vlan4 ={'parentinterface': 'vtnet1', 'tag': '4', 'pcp': '0', 'proto': None, 'descr': 'vlan4', 'vlanif': 'vlan0.4',"attr":{"uuid":"cb503df8-821d-4acd-86ba-66b35e4s7c"}}
opt3_dhcp= {'enable': '1', 'ddnsdomainalgorithm': 'hmac-md', "range":{'from': '200.0.3.10', '_to': '200.0.3.100'},"attr":None}
opt4_dhcp= {'enable': '1', 'ddnsdomainalgorithm': 'hmac-md', "range":{'from': '200.0.4.10', '_to': '200.0.4.100'},"attr":None}
opt3_interface= {'descr': 'vlan3', 'enable': '1', 'ipaddr': '200.0.3.1', 'subnet': '24', 'type': None, 'virtual': None, 'spoofmac': '00:00:00:00:00:03', 'attr': {}}
opt4_interface= {'descr': 'vlan4', 'enable': '1', 'ipaddr': '200.0.3.1', 'subnet': '24', 'type': None, 'virtual': None, 'spoofmac': '00:00:00:00:00:04', 'attr': {}}

```
- get the current conf file
- initilaize the values and scan for object
- dumb the object to xml
. put the file back to the ssh-host
- close ssh-connection
```python
helper.get_file(conf_path, output)
helper.initialize()
helper.objects["dhcpd"]["opt3"]=opt3_dhcp
helper.objects["dhcpd"]["opt4"]=opt4_dhcp
helper.objects["vlans"]["vlan3"]=vlan3
helper.objects["vlans"]["vlan4"]=vlan4
helper.objects["interfaces"]["opt3"]=opt3_interface
helper.objects["interfaces"]["opt4"]=opt4_interface
helper.save(output)
helper.put_file(output,conf_path)
helper.close_con()
vlans=[
{'if': 'vtnet1', 'tag': '1', 'pcp': '0', 'proto': None, 'descr': 'vlan1', 'vlanif': 'vlan0.1'},
{'if': 'vtnet1', 'tag': '2', 'pcp': '0', 'proto': None, 'descr': 'vlan2', 'vlanif': 'vlan0.2'}
]
dhcp={
"opt2": {'enable': '1', 'ddnsdomainalgorithm': 'hmac-md', "range":{'from': '200.0.3.10', '_to': '200.0.3.100'}},
"opt3": {'enable': '1', 'ddnsdomainalgorithm': 'hmac-md', "range":{'from': '200.0.4.10', '_to': '200.0.4.100'}}
}
interfaces={
"opt1": {'descr': 'router', 'enable': '1', 'ipaddr': None, 'subnet': None, 'type': None, 'virtual': None, 'spoofmac': '00:00:00:00:02:01',"interface":"vtnet1"},
"opt2": {'descr': 'vlan1', 'enable': '1', 'ipaddr': '200.0.3.1', 'subnet': '24', 'type': None, 'virtual': None, 'spoofmac': '00:00:00:00:00:01',"interface":"vlan0.1"},
"opt3": {'descr': 'vlan2', 'enable': '1', 'ipaddr': '200.0.4.1', 'subnet': '24', 'type': None, 'virtual': None, 'spoofmac': '00:00:00:00:00:02', "interface":"vlan0.2"}
}
ssh_auth={
"user":"root",
"passw":"opnsense",
}
api_auth={
# ONLY NEED WHEN YOU USE THE API
"api_key" :'ejl4fIU9yfNk+gaQmPk/rqIa15f1yX1snIKgcIEl2QNoJwhbekraWIE0ANRYceh9hey5IFGzlf3da4yJ',
"api_secret" : '5JVVGoatPbaAA+FozLDQY92/T6sRlmKD1+aRNl/YI8KA9/0TNiTDboLveqvd9FU8wFeDo3D3DY5wrUtF',
"ssl": True,
"verify": False
}
host= "192.168.1.103"
def test():
helper=Opnsense_Helper(host=host,ssh_auth=ssh_auth,api_auth=api_auth,filepath=output, verbose=False)
helper.add_vlans(vlans)
#helper.set_vlans(vlans)
helper.get_conf(conf_path)
helper.initialize()
helper.add_Items("interfaces",interfaces)
helper.add_Items("dhcpd",dhcp)
helper.save(output)
helper.put_file(output,conf_path)
helper.close_con()

if __name__ == "__main__":
test()
```

### adding vlans

| tpe | name |
| --- | --- |
| NIC | veth1 |
| Interface | router |
| Vlan | vlan0.1 |
| VlanInterface | vlan1 |


- no redundant assigntments allowed
- reset to default config before reassigning
- roll back to snapshot
- old vlans and interfaces must explicitl get deleted and the config cant provide this
- however config gives freedom over runtime changes without entire cloudinit
- keep in mind that violating given rules can cause a broken vm

### copy the config file to your machine manually
```bash
scp [email protected]:/conf/config.xml /home/user/confignew.xml && chmod +x /home/user/confignew.xml && chown user:user /home/user/confignew.xml
```
### replace the original file manually
```bash
scp /home/user/config.xml [email protected]:/conf/config.xml
```
## contribute
### python
### contribute
- clone, or fork `[email protected]:the-pod-shop/opnsense-helper.git`
- build when made changes
- make sure to use the right user
Expand All @@ -87,6 +66,7 @@ python setup.py bdist_wheel \
&& pip install --upgrade . \
&& python3 -m pip install --upgrade build #--force
```
- you can also use the build.sh script
- create pull request

## motivation
Expand All @@ -96,4 +76,8 @@ python setup.py bdist_wheel \
- but /conf/config.xml has the answer.
- however for phisical interfaces its the god damn conf.rc
- my opnsense runs in a vm, so it really doesnt matter for me
- i just add the interfaces via libvirt and all i need to do is to enable them, given the /conf/config.xml method
- i just add the interfaces via libvirt and all i need to do is to enable them, given the /conf/config.xml method
xml has the answer.
- however for phisical interfaces its the god damn conf.rc
- my opnsense runs in a vm, so it really doesnt matter for me
- i just add the interfaces via libvirt and all i need to do is to enable them, given the /conf/config.xml method
10 changes: 7 additions & 3 deletions python/build/lib/opnsense_helper/classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ def save(self,output):
"""
update_xml_file(self.objects["dhcpd"],self.root,"dhcpd")
update_xml_file(self.objects["interfaces"],self.root,"interfaces")
update_xml_file(self.objects["vlans"],self.root,"vlans")
#update_xml_file(self.objects["vlans"],self.root,"vlans")
with open(output, 'w') as f:
f.write(ET.tostring(self.root, encoding='unicode', method='xml'))
def get_all(self,element):
Expand Down Expand Up @@ -129,7 +129,10 @@ def get_all(self,element):

child.attr=key.attrib if key.attrib is not None else None
print(child.attr)
name = key.tag if element != "vlans" else child.descr
if element != "vlans":
name = key.tag
else:
name = child.descr
self.objects[element][name]=child.__dict__
print(f'''{key.tag} : {child.__dict__}
-------------------''')
Expand Down Expand Up @@ -202,7 +205,8 @@ def add_vlans(self,vlans):
payload={"vlan":value}
r=api_post(self,"interfaces/vlan_settings/addItem",payload)
print(r)

r=api_post(self,"interfaces/vlan_settings/reconfigure",{})
print(r)
# please use the get_backup function to avoid losing data

def get_conf(self,_from,_to=None):
Expand Down
12 changes: 1 addition & 11 deletions python/build/lib/opnsense_helper/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,7 @@
}

#curl -k -u $OPNS_KEY:$OPNS_SECRET https://$IPFW/api/diagnostics/interface/getinterfaceconfig
def get_child(root,element, id, keys):
elements=[]
for parent in root.findall(element):
child= {}
for y in keys:
child[y]=None
for x in parent.findall(id):
for key in keys:
child[key]=parseChild(x, key)
elements.append(child)
return elements

def parseChild(parent, tag):
result=parent.find(tag)

Expand Down
Binary file removed python/dist/opnsense_helper-0.1.1-py3-none-any.whl
Binary file not shown.
Binary file removed python/dist/opnsense_helper-0.1.1.tar.gz
Binary file not shown.
Binary file removed python/dist/opnsense_helper-0.1.2-py3-none-any.whl
Binary file not shown.
Binary file removed python/dist/opnsense_helper-0.1.3-py3-none-any.whl
Binary file not shown.
Binary file removed python/dist/opnsense_helper-0.1.4-py3-none-any.whl
Binary file not shown.
Binary file removed python/dist/opnsense_helper-0.1.5-py3-none-any.whl
Binary file not shown.
Binary file removed python/dist/opnsense_helper-0.1.7-py3-none-any.whl
Binary file not shown.
2 changes: 1 addition & 1 deletion python/opnsense_helper.egg-info/PKG-INFO
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Metadata-Version: 2.1
Name: opnsense_helper
Version: 0.1.7
Version: 0.1.9
Summary: assign lan interfaces, create vlans, vlan-interfaces and setup dhcp in a single script.
Home-page: https://github.com/the-pod-shop/opnsense-helper/
Author: ji-podhdead
Expand Down
1 change: 1 addition & 0 deletions python/opnsense_helper/README.md
Loading

0 comments on commit 0446009

Please sign in to comment.