Created
April 17, 2023 08:32
-
-
Save juliocbc/d798a7a34434af48608b07f2778e236c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
# Copyright (C) 2019 Cloudfence - Julio Camargo | |
# All rights reserved. | |
# | |
# Redistribution and use in source and binary forms, with or without | |
# modification, are permitted provided that the following conditions are met: | |
# | |
# 1. Redistributions of source code must retain the above copyright notice, | |
# this list of conditions and the following disclaimer. | |
# 2. Redistributions in binary form must reproduce the above copyright | |
# notice, this list of conditions and the following disclaimer in the | |
# documentation and/or other materials provided with the distribution. | |
# THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, | |
# INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY | |
# AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE | |
# AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, | |
# OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | |
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | |
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
# POSSIBILITY OF SUCH DAMAGE. | |
# -------------------------------------------------------------------------------------- | |
# https://github.com/cloudfence | |
# Convert XML from pfSense to OPNsense format | |
tput setaf 1 | |
echo " | |
USE AT OWN OWN RISK! | |
TESTS IN LAB BEFORE USE IN PRODUCTION ENVIRONMENT IS HIGHLY RECOMMENDED! | |
YOU WERE WARNED!! | |
" | |
tput sgr0 | |
read | |
XML="$1" | |
XMLLINT=$(which xmllint) | |
CHK=$( echo $? ) | |
if [ $CHK -ne 0 ];then | |
echo "xmllint is needed to run this script, you must install it and try again" | |
fi | |
DATE=$(date +%Y%m%d) | |
OPN_XML="/tmp/config-OPN-$DATE.xml" | |
change_ifaces() | |
{ | |
TMP_XML="/tmp/config.tmp" | |
IFACE_DRIVER=$(egrep "<if>" $XML | grep -v vlan | cut -d"<" -f2 | cut -d">" -f2 | sed 's/[0-9]//g' | sort | uniq) | |
tput setaf 3 | |
echo | |
echo "This script will change it sequentially e.g.: em1 -> igb1, em2 -> igb2..." | |
echo | |
tput sgr0 | |
echo | |
echo "Network Interface Driver found:" | |
tput setaf 6 | |
echo "$IFACE_DRIVER" | |
tput sgr0 | |
echo "Type new Network Interface Driver:" | |
tput setaf 6 | |
read NEW_IFACE_DRIVER | |
tput sgr0 | |
if [ -z "$NEW_IFACE_DRIVER" ];then | |
echo "You must type a valid network driver" | |
else | |
cat $XML | sed 's/\<if\>'"$IFACE_DRIVER"'/\<if\>'"$NEW_IFACE_DRIVER"'/g' > $TMP_XML | |
IFACE_CHANGED="1" | |
break | |
fi | |
} | |
# Sections to import | |
# not using array to be compatible with all shells | |
SECTIONS="system interfaces staticroutes dhcpd dnsmasq snmpd syslog nat filter ipsec alias revision rrd widgets sysctl gateways openvpn cert ca virtualip vlans" | |
echo "The following Config sections will be imported:" | |
for SECTION in $SECTIONS; | |
do | |
tput setaf 6 | |
echo "$SECTION" | tr a-z A-Z | |
tput sgr0 | |
done | |
echo "Press Start to... I mean: Press ENTER to continue" | |
read | |
echo "Network interfaces are:" | |
echo "$IFACES" | |
echo "Do you want to change the interfaces?" | |
echo "Tip: If the destination system have another network chipset, you should answer Yes here:" | |
select OPT in "Yes" "No" "Not sure" | |
do | |
case $OPT in | |
"Yes") | |
change_ifaces | |
;; | |
"No") | |
echo "OK! Lets continue" | |
break | |
;; | |
"Not sure") | |
tput setaf 3 | |
echo "Let me try to help:" | |
echo "Log in OPNsense WebGUI and go to:" | |
echo "Interfaces: Assignments" | |
echo "The list of your physical network interfaces should be listed there" | |
echo "This script will change it sequentially e.g.: em1 -> igb1, em2 -> igb2..." | |
tput sgr0 | |
;; | |
*) | |
echo "invalid option " | |
;; | |
esac | |
done | |
cat << EOF > $OPN_XML | |
<?xml version="1.0"?> | |
<opnsense> | |
EOF | |
for SECTION in $SECTIONS | |
do | |
if [ ! -z "$IFACE_CHANGED" ];then | |
$XMLLINT --nocdata --xpath "//$SECTION" $TMP_XML >> $OPN_XML | |
echo >> $OPN_XML | |
else | |
$XMLLINT --nocdata --xpath "//$SECTION" $XML >> $OPN_XML | |
echo >> $OPN_XML | |
fi | |
done | |
echo "</opnsense>" >> $OPN_XML | |
echo "Saved new file: $OPN_XML" | |
rm -f $TMP_XML | |
i replaced XML="$1" with the locatiion of xml and then it works, now to test if import works :)
Hey @hjaltioj, did this work out for you?
i replaced XML="$1" with the locatiion of xml and then it works, now to test if import works :)
It should work with: pf2OPN-config.sh <config.xml>
Hey, yeah, but i had to edit alot in the xml. But it was helpful for my migration :)
This script is 4 years old, needs an update and review. But I´m glad that was helpful!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi.
Do this still work? or how do i use this script?
it starts, and then just outputs this:
Libxml project home page: https://gitlab.gnome.org/GNOME/libxml2
Usage : /usr/local/bin/xmllint [options] XMLfiles ...
Parse the XML files and output the result of the parsing...............
.....................