问题如下:
我有一个包含数据的 xml 文件,我正在寻找一小部分数据将其写入新文件:内容已根据请求缩短:
如果 type=dhcp-client,则代码片段:
<deviceconfig>
<system>
<type>
<dhcp-client>
<send-hostname>yes</send-hostname>
</dhcp-client>
</type>
<hostname>Firewall</hostname>
</system>
</deviceconfig>
如果 type=static 则片段
<deviceconfig>
<system>
<type>
<static/>
</type>
<hostname>Firewall</hostname>
<permitted-ip>
<entry name="192.168.0.0/24"/>
</permitted-ip>
<ip-address>192.168.0.2</ip-address>
<netmask>255.255.255.0</netmask>
<default-gateway>192.168.0.1</default-gateway>
</system>
<network>
<interface>
<ethernet>
<entry name="ethernet1/1">
<layer3>
<ip>
<entry name="192.168.0.5/24"/>
</ip>
</layer3>
</entry>
</ethernet>
</interface>
<virtual-router>
<entry name="default">
<routing-table>
<ip>
<static-route>
<entry name="default-route">
<nexthop>
<ip-address>192.168.0.1</ip-address>
</nexthop>
<interface>ethernet1/4</interface>
<destination>0.0.0.0/0</destination>
</entry>
</static-route>
</ip>
</routing-table>
</entry>
</virtual-router>
</network>
四个相关值在“系统”标签内是唯一的(或不存在),
<system></system>
像IP地址之类的东西可能会再次出现在系统之外的其他地方,
<system></system>
但我只检查系统内部的值,如果类型不是静态的,则不会出现,我会设置它到 dhcp 客户端
如果类型是 dhcp,这就是我需要的文件结果:
type=dhcp-client
如果类型是静态的,这就是我在文件中需要的结果:
type=static
ip-address=192.168.0.2
default-gateway=192.168.0.1
netmask=255.255.255.0
我不确定如何有效地完成此任务并将其集成到现有的PHP文件(所以要么使用 exec 工作,要么最好只使用 php)。
我还只能使用 ubuntu 服务器系统上默认安装的工具,并且无法使用其他软件包。
PS:这实际上是整个/完整的用例,除了这两个示例之外,我不需要生成其他输出。感谢您的帮助或指点:)
答案1
假设您无法访问 XML 感知工具,并且您的输入文件如您所示的那样简单且规则,这会根据您发布的示例输入生成预期的输出:
$ cat tst.awk
BEGIN { FS="[[:space:]]*[<>][[:space:]]*"; OFS="=" }
$2 == "system" { inBlock=1 }
inBlock { f[$2] = $3 }
$2 == "/system" { inBlock=0 }
END {
if ("ip-address" in f) {
print "type", "static"
print "ip-address", f["ip-address"]
print "default-gateway", f["default-gateway"]
print "netmask", f["netmask"]
}
else {
print "type", "dhcp-client"
}
}
。
$ awk -f tst.awk absentFile
type=dhcp-client
。
$ awk -f tst.awk presentFile
type=static
ip-address=192.168.0.2
default-gateway=192.168.0.1
netmask=255.255.255.0
以上是在这些输入文件上运行的:
$ tail -n +1 absentFile presentFile
==> absentFile <==
<deviceconfig>
<system>
<type>
<dhcp-client>
<send-hostname>yes</send-hostname>
</dhcp-client>
</type>
<hostname>Firewall</hostname>
</system>
</deviceconfig>
==> presentFile <==
<deviceconfig>
<system>
<type>
<static/>
</type>
<hostname>Firewall</hostname>
<permitted-ip>
<entry name="192.168.0.0/24"/>
</permitted-ip>
<ip-address>192.168.0.2</ip-address>
<netmask>255.255.255.0</netmask>
<default-gateway>192.168.0.1</default-gateway>
</system>
<network>
<interface>
<ethernet>
<entry name="ethernet1/1">
<layer3>
<ip>
<entry name="192.168.0.5/24"/>
</ip>
</layer3>
</entry>
</ethernet>
</interface>
<virtual-router>
<entry name="default">
<routing-table>
<ip>
<static-route>
<entry name="default-route">
<nexthop>
<ip-address>192.168.0.1</ip-address>
</nexthop>
<interface>ethernet1/4</interface>
<destination>0.0.0.0/0</destination>
</entry>
</static-route>
</ip>
</routing-table>
</entry>
</virtual-router>
</network>
答案2
好吧,我自己找到了答案......
实际上,在 php 中比没有它简单得多......
不过我确实花了很多小时才完成^^
#load the file as simplexml object and then switch into system
#https://www.w3schools.com/php/func_simplexml_load_file.asp
$xml=simplexml_load_file('./myfile') or die("Error: Cannot create object");
$xml=$xml->system
#put the whole string(s) into a variable, getname gets the name of the object itself if it exists
#https://www.w3schools.com/php/func_simplexml_getname.asp
$output='type=' . $xml -> type -> static -> getName() . $xml -> type -> {'dhcp-client'} -> getName() . "\nip-address=" . $xml -> {'ip-address'} . "\ndefault-gateway=" . $xml -> {'default-gateway'} . "\nnetmask=" . $xml -> netmask;
#write the output into a file
#https://www.w3schools.com/php/func_filesystem_file_put_contents.asp
file_put_contents('./myoutputfile', $output );
这为我提供了第一个片段的以下输出(如果最后三行没有给出值,则可以,否则我可以先检查它们是否存在):
type=dhcp-client
ip-address=
default-gateway=
netmask=
第二个片段的输出:
type=static
ip-address=192.168.0.2
default-gateway=192.168.0.1
netmask=255.255.255.0
感谢大家的帮助:)
答案3
您可以使用以下脚本ip-parse.sh
:
#!/bin/bash
#https://stackoverflow.com/questions/22221277/bash-grep-between-two-lines-with-specified-string
#https://www.cyberciti.biz/faq/bash-remove-whitespace-from-string/
#https://stackoverflow.com/questions/1251999/how-can-i-replace-a-newline-n-using-sed
sed -n '/\<system\>/,/system\>/p' ~/Desktop/x-test.xml | sed -e 's/^[ \t]*//' > ~/Desktop/x-system.xml
sed ':a;N;$!ba;s/\n/ /g' ~/Desktop/x-system.xml > /tmp/xml-one-line.xml
#[]test to see if the "system" section ...
#... has the word hostname occuring before the word ip-address
#https://stackoverflow.com/questions/33265650/grep-for-a-string-in-a-specific-order
if [ -n "$(grep hostname.*ip-address /tmp/xml-one-line.xml)" ]; then
echo "File contains hostname and ip-address, in that order."
else
echo "type=dhcp-client" ; echo "type=dhcp-client" > ~/Desktop/network-config.txt ; exit
fi
#http://www.compciv.org/topics/bash/variables-and-substitution/
ipaddress="$(grep ip-address ~/Desktop/x-system.xml | sed 's/<ip-address>//g; s/<\/ip-address>//g')"
defaultgateway="$(grep default-gateway ~/Desktop/x-system.xml | sed 's/<default-gateway>//g; s/<\/default-gateway>//g')"
netmask="$(grep netmask ~/Desktop/x-system.xml | sed 's/<netmask>//g; s/<\/netmask>//g')"
echo "type=static" > ~/Desktop/network-config.txt
echo "ip-address=$ipaddress" >> ~/Desktop/network-config.txt
echo "default-gateway=$defaultgateway" >> ~/Desktop/network-config.txt
echo "netmask=$netmask" >> ~/Desktop/network-config.txt
Application example:
paul@mxg6:~/Desktop$ ./ip-parse.sh
File contains hostname and ip-address, in that order.
paul@mxg6:~/Desktop$ cat network-config.txt
type=static
ip-address=192.168.0.2
default-gateway=192.168.0.1
netmask=255.255.255.0
如果您不需要检查主机名是否在 ip 地址之前,并且您想使用变量而不是中间文件,请尝试以下操作:
#!/bin/bash
xsystemxml="$(sed -n '/\<system\>/,/system\>/p' ~/Desktop/x-test.xml \
| sed -e 's/^[ \t]*//')"
if [ -n "$(echo $xsystemxml | grep ip-address)" ]; then
echo "System section contains ip-address."
else
echo "type=dhcp-client"
echo "type=dhcp-client" > ~/Desktop/network-config.txt
exit
fi
ipaddress="$(echo "$xsystemxml" | grep "ip-address" \
| sed 's/<ip-address>//g; s/<\/ip-address>//g')"
defaultgateway="$(echo "$xsystemxml" | grep "default-gateway" \
| sed 's/<default-gateway>//g; s/<\/default-gateway>//g')"
netmask="$(echo "$xsystemxml" | grep "netmask" \
| sed 's/<netmask>//g; s/<\/netmask>//g')"
echo "type=static" > ~/Desktop/network-config.txt
echo "ip-address=$ipaddress" >> ~/Desktop/network-config.txt
echo "default-gateway=$defaultgateway" >> ~/Desktop/network-config.txt
echo "netmask=$netmask" >> ~/Desktop/network-config.txt
答案4
以下答案来自这里https://stackoverflow.com/questions/893585/how-to-parse-xml-in-bash,我做了一个简单的脚本
#!/bin/bash
read_dom () {
local IFS=\>
read -d \< ENTITY CONTENT
}
found=0
while read_dom; do
if [[ $ENTITY = "ip-address" ]] && [[ $last_tag = "/hostname" ]] || [[ $ENTITY = "netmask" ]] || [[ $ENTITY = "default-gateway" ]]; then
if [[ $found = 0 ]]; then
echo "type=static"
fi
echo "$ENTITY=$CONTENT"
found="1"
fi
last_tag=$ENTITY
done
if [[ $found = 0 ]]; then
echo "type=dhcp-client"
fi
如果你给你的脚本命名parse.sh
,你就可以这样称呼它
parse.sh < input.xml > output.txt