问题原文
原帖链接 · Osqui · CC BY-SA 4.0
I want to run these two commands in a interactive shell, one after the other (as root and with the "filter" table and the "input" chain loaded):
nft define lala=1.2.3.4
nft add rule ip filter input ip saddr \$lala accept
You can see I escape the "$" to avoid shell expansion. But I get this error message "Unkown identifier 'lala'". If I don't escape the "$", I get this error message instead: "Syntax error, unexepected accept". If I write \$$lala instead, I get this error message instead: "Syntax error, uneepcted accept, expecting string" and this wrong rule is shown: add rule ip filter input ip saddr $ accept.
So, what's the correct syntax to be able to use Nft variables inside a interactive shell. Doing the same inside a Nft script works well (without worrying about escaping).
Thanks a lot
已采纳解答
原帖链接 · A.B · CC BY-SA 4.0
Symbolic variables in use by the nft userspace command are resolved into their values only by this command. The nft command resolves the variable into its assigned value to assemble the final result. Then the nft commands uses the netlink API to communicate with the kernel and send it a new rule to add. The kernel never sees lala=1.2.3.4 nor has any concept of symbolic variable.
So if you split your nft command in two invocations what happens is:
The first
nftcommand defines a symbolic variable for its own later use. But does nothing else. It doesn't even have to communicate with the kernel since no ruleset change is asked. (Actually it does communicate, but to send "nothing", like with the commandnft ''). At the end of the command the definition of the symbol is lost, without having been used.A second invocation of the
nftcommand needs to resolve the$lalasymbol, but cannot find a definition:nftstops with an error.
So this must be all done in one nft invocation so the second statement still knows about the symbolic variable. There are multiple ways to do this. I left in examples below shell and nft's prompts which shouldn't be typed.
interactive
nft-i, --interactive
Read input from an interactive readline CLI. You can usequitto exit, or use the EOF marker, normally this is CTRL-D.# nft -i nft> define lala=1.2.3.4 nft> add rule ip filter input ip saddr $lala accept nft> quittwo commands in a single
nftinvocation.# nft 'define lala=1.2.3.4; add rule ip filter input ip saddr $lala accept'Note the additional
;needed because in this contextnftreceives one line of parameters, but they should be split into two logical commands. Alsonfthaving its own parser, it doesn't care if commands are received as a single command parameter or multiple separate parameters. Here everything was enclosed in a pair of''to avoid shell issues with both;and$characters.Then
nfthas an option to read from a file. When reading from a file, all the input is part of the samenftcommand context.-f, --file filename
Read input from filename. If filename is -, read from stdin.nft scripts must start
#!/usr/sbin/nft -fSo with a file named
test.nfthaving this content:define lala=1.2.3.4 add rule ip filter input ip saddr $lala acceptThis can then be used:
# nft -f test.nftThe variant below executes a script in the "nftables language". Having a
test.nftfile content as:#!/usr/sbin/nft -f define lala=1.2.3.4 add rule ip filter input ip saddr $lala acceptThen:
# chmod a+rx test.nft # ./test.nftThis would also have worked:
# nft -f - <<'EOF' > define lala=1.2.3.4 > add rule ip filter input ip saddr $lala accept > EOF
For each previous command, the resulting ruleset is the same (assuming table and chain were created before):
# nft list chain ip filter input
table ip filter {
chain input {
type filter hook input priority filter; policy accept;
ip saddr 1.2.3.4 accept
}
}
There won't be anymore any trace of $lala left: the kernel never received $lala but only 1.2.3.4 and that's what it gives back.
摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。
