Linux - SoftwareThis forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Extracting the parts you want is often done with grep or sed. Here I extracted the ip address from the 'ifconfig' command on my computer. You would use ppp0 instead of eth0.
Code:
ifconfig | sed -n '/^eth0/,/^$/s/inet addr:\([[:digit:]][[:digit:]]*\.[[:digit:]][[:digit:]]*\.[[:digit:]][[:digit:]]*.[[:digit:]][[:digit:]]* \).*$/\1/p'
using backticks ( `sed ...` ), or the $() syntax, $( sed ... ), you could assign the IP address to a variable to be used in the next command, or you could include this command within backticks in the command that alters the XML file.
The sed command could be one one line, or you could put the part between the single quotes in a sed script and call the command like this: ifconfig | sed -n -f sedscript
Please read the sed man page for more information.
I think you would need to provide a larger segment from the XML file you want to change before we could see what a sed or awk command would look like.
More complex configuration file changes are often done in Linux using Perl scripts.
Here is the rundns.conf file which I want to update.
Code:
<xml version="1.0">
<!-- NOTE that all the values are case sensitive here! It means
that "YES" is NOT the same thing as "yes". -->
<rundns version="0.53">
<!-- system is "dyndns", "statdns" or "custom" -->
<system>dyndns</system>
<!-- You need to put your DynDNS username
and password here so that the client
is authorized to update your settings. -->
<user>test</user>
<password>test</password>
<!-- Your dyndns virtual host -->
<host>test.dyndns.org</host>
<!-- put your current REAL ip-number belowe -->
<ip>127.0.0.1</ip>
<!-- timestamp. (You don't need to change this). -->
<timestamp>0</timestamp>
<!-- wildcard = "ON" or "OFF" or "NOCHG" -->
<wildcard>OFF</wildcard>
<!-- MX specifies a Mail eXchanger for use with the hostname being modified.
Leave it as is, if you don't need one. -->
<mx></mx>
<!-- Request that the MX in the previous parameter be set up as a backup MX.
Valid values are YES and NO, leave as is, if you didn't specify MX above. -->
<backmx></backmx>
<!-- The offline-feature is only available to credited users. -->
<offline>NO</offline>
<!-- Do not change the server belowe, leave it as is. -->
<server>members.dyndns.org</server>
<!-- Give the log file's name, so that you may read it if something goes wrong.
Make sure you have write permissions to it's directory.
You may use /dev/null of course if you dont want logs,
But in that case a better idea is to set the value NULL -->
<logfile>/tmp/rundns.log</logfile>
</rundns>
</xml>
Last edited by carboncopy; 01-05-2005 at 03:33 AM.
Now I have automatic dynamic dns updater using rundns(with ssl). I know I can use updatedd as well. But, it doesn't have ssl support.
I modified the script homey gave. Minor adjustments only.
Code:
#!/bin/bash
#
# This script is to be used with rundns.
# rundns is available from http://rundns.dyndns.org
#
#
# rundns configuration file
confile=/etc/rundns/rundns.conf
# The scratch file just for this updater script
scratch=/etc/rundns/rundns.conf.updater
#
# Interface which the domain is to be updated on DNS server. Example : eth0,eth1,ppp0, etc.
netint=ppp0
#
#
echo Dynamic Domain Name Configuration File Updater.
echo
# Finding out what is your IP address. Thanks to homey (linuxquestions.org).
addr=`/sbin/ifconfig $netint |grep -o 'addr:[[:alnum:][:punct:]]*' |cut -d: -f2`
echo Your IP address is $addr
#
# Remove previous scratch file if it exist.
if [[ -e $scratch ]]
then
rm $scratch
else
# This is not really necessary.
echo "$scratch does not exist"
fi
#
# The current (un-updated) configuration file is turn in to scratch file.
mv $confile $scratch
#
# Getting your previous IP address according to the config file. Code by homey (linuxquestions.org)
num=`grep -e \<ip $scratch | awk '{ print $1 }'`
echo Your previous IP address was $num
#
# Getting the line which is to be updated. Code by homey (linuxquestions.org)
line=`cat $scratch | grep -n $num | cut -d: -f1`
#
# Update with new IP and write to Configuration file. Code by homey (linuxquestions.org)
cat $scratch | sed -e ''$line'd' | sed -e ''$line'i'" <ip>$addr</ip>"'' > $confile
Last edited by carboncopy; 01-05-2005 at 09:17 PM.
I wonder if you should use cp instead of mv as rundns.conf will no longer exist if you move it to a new name like rundns.conf.updater
Edit:
Quote:
if [[ -e $scratch ]]
then
rm $scratch
else
echo "$scratch does not exist"
fi
mv $confile $scratch
I think you could do with out the if statement and removing $scratch for two reasons.
1. It will get overwritten with a cp or mv statement.
2. If you remove it, then mv $confile $scratch will fail as you are trying to access something which you already removed.
hmmmm, if you want to make the script even more bulletproof, try having a "template" config file with a "magic" string to replace. That way the file could look like:
#
# Remove previous scratch file if it exist.
if [[ -e $scratch ]]
then
rm $scratch
else
# This is not really necessary.
echo "$scratch does not exist"
fi
#
# The current (un-updated) configuration file is turn in to scratch file.
mv $confile $scratch
with
Code:
cp -f $confile $scratch
That is, by not using the template.
The template config file would eliminate the need for that. :)
Ok.. the whole thing can be compress into the following:Thanks to janic.
I've added to my earlier example. It is now a two-liner. I added '| sed 's/ //g' to strip off leading and trailing spaces from the ip address before assigning it to the $ans variable.
The substitution is performed by the last line. Note the '-i' option. This allows inline editing without having to save to a temporary file. You may want to remove it at first so the output prints to the screen, just to verify that it works. (Also, the name of the file needs to be changed)
The ifconfig command that this script uses must be run as root. Well you need to be root to modify your xml file anyway.
Code:
#!/bin/bash
# get address from 'ifconfig', strip off leading/trailing spaces, and assign to $ans
ans=$(ifconfig | sed -n '/^ppp0/,/^$/s/inet addr:\([[:digit:]][[:digit:]]*\.[[:digit:]][[:digit
:]]*\.[[:digit:]][[:digit:]]*.[[:digit:]][[:digit:]]* \).*$/\1/p'| sed 's/ //g')
# replace line in xmlfile with REAL ip number
sed -i '/put your current REAL ip-number/ { n
s/^.*$/ \<ip\>'${ans}'\<\/ip\>/
}' xmlfile
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.