The "delete-node" script is provided in firmware 2.8.1 to help with deleting nodes from the command line. The "delete-node"script takes one argument, the node name you want to delete (e.g. "config.users.user1", or "config.sdt.hosts.host1").
So delete-node is a general script for deleting any node you desire (users, groups, hosts, UPS's etc) from the command line. The script deletes the specified node and shuffles the remainder of the node values.
For example if we have five users configured and we use the script to delete user 3, then the last user (user 5) will take the place of user 3.
This creates an obvious complication as this script does NOT check for any other dependencies that the node being deleted may have had. So you are responsible for making sure that any references and dependencies connected to the deleted node are removed or corrected in the configuration (by running config -s or config -d). The script also does not run any of the configurators. Before you manually run a configurator, it is strongly recommended that you correct any dependency issues after deleting a node.
NOTE: To add/modify/delete users from the command line use the user-add, user-mod or user-del scripts instead (as described in faq 347).
The script treats all nodes the same. The syntax to run the script is as follows:
# /etc/scripts/delete-node {node name}
so to remove user 3:
# /etc/scripts/delete-node config.users.user3
To remove all users in one command:
# /etc/scripts/delete-node config.users
For more general information on custom scripts refer faq 255.
The delete-node script
#!/bin/bash
#set -x
#User must provide the node to be removed. e.g. "config.users.user1"
# Usage: delete-node {full node path}
if [ $# != 1 ]
then
echo "Wrong number of arguments"
echo "Usage: delnode {full '.' delimited node path}"
exit 2
fi
# test for spaces
TEMP=`echo "$1" | sed 's/.* .*/N/'`
if [ "$TEMP" = "N" ]
then
echo "Wrong input format"
echo "Usage: delnode {full '.' delimited node path}"
exit 2
fi
# testing if node exists
TEMP=`config -g config | grep "$1"`
if [ -z "$TEMP" ]
then
echo "Node $1 not found"
exit 0
fi
# LASTFIELD is the last field in the node path e.g. "user1"
# ROOTNODE is the upper level of the node e.g. "config.users"
# NUMBER is the integer value extracted from LASTFIELD e.g. "1"
# TOTALNODE is the node name for the total e.g. "config.users.total"
# TOTAL is the value of the total number of items before deleting e.g. "3"
# NEWTOTAL is the modified total i.e. TOTAL-1
# CHECKTOTAL checks if TOTAL is the actual total items in .xml
LASTFIELD=${1##*.}
ROOTNODE=${1%.*}
NUMBER=`echo $LASTFIELD | sed 's/^[a-zA-Z]*//g'`
TOTALNODE=`echo ${1%.*} | sed 's/\(.*\)/\1.total/'`
TOTAL=`config -g $TOTALNODE | sed 's/.* //'`
NEWTOTAL=$[ $TOTAL -1 ]
TOTALPRESENT=true
#set -x
# Test if a total value exists
# NOTE: this fix is not fail proof. But works for most things which have
# one line per numbered item.
if [ -z "$TOTAL" ]
then
TOTALPRESENT=false
TOTAL=`config -g $ROOTNODE \
| grep -c config`
fi
# Make backup copy of config file
cp /etc/config/config.xml /etc/config/config0.bak
echo "backup of /etc/config/config.xml saved in /etc/config/config0.bak"
if [ -z $NUMBER ] # test whether a singular node is being \
#deleted e.g. config.sdt.hosts
then
echo "deleting $1"
config -d "$1"
#set +x
exit 0
elif [ "$NUMBER" = "$TOTAL" ] # Test if last item is being deleted
then
# Deleting node
echo "Deleting $1"
config -d "$1"
# Modifying item total.
if [ "$TOTALPRESENT" = true ]
then
config -s "$TOTALNODE=$NEWTOTAL"
fi
exit 0
# more than one item exists
elif [ "$NUMBER" -lt "$TOTAL" ]
then
# Modify the config list by shifting the last item into the
# gap produced from delelting
echo "Deleting $1"
LASTFIELDTEXT=`echo $LASTFIELD | sed 's/[0-9]//g'`
CHECKTOTAL=`config -g $ROOTNODE.$LASTFIELDTEXT$TOTAL`
if [ -z "$CHECKTOTAL" ]
then
echo "WARNING: "$TOTALNODE" greater than number of items"
fi
#set -x
# Delete the node now
DELETECOMMAND=
config -d "$ROOTNODE.$LASTFIELDTEXT$NUMBER"
fifo_name=/var/tmp/$(basename $0).$$
mkfifo $fifo_name
config -g "$ROOTNODE.$LASTFIELDTEXT$TOTAL" > $fifo_name &
while read -r LINE
do
DELETECOMMAND="$DELETECOMMAND -s \
`echo "$LINE" | sed -e s/"$LASTFIELDTEXT$TOTAL\
/$LASTFIELDTEXT$NUMBER"/ \
-e 's/ /=/'`"
done < $fifo_name
rm -f $fifo_name
DELETECOMMAND="config $DELETECOMMAND" $DELETECOMMAND
#set +x
# deleting last item
config -d "$ROOTNODE.$LASTFIELDTEXT$TOTAL"
# Modifying item total.
if [ "$TOTALPRESENT" = true ]
then
config -s "$TOTALNODE=$NEWTOTAL"
fi
#set +x
exit 0
else
echo "error: item being deleted has an index greater than total items. Increase the total count variable."
#set +x
exit 0
fi
Comments
0 comments
Article is closed for comments.