LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This 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


Reply
  Search this Thread
Old 01-05-2005, 02:58 AM   #1
carboncopy
Senior Member
 
Registered: Jan 2003
Location: Malaysia
Posts: 1,210
Blog Entries: 4

Rep: Reputation: 45
bash script help me.


Hi!

I need a very simple shell scripts which does the following:

1. take the ppp0 IP address
2. The IP address will replace the old IP address in a conf file (xml format). <ip>new IPadd here</ip>

I hope this is clear enough. Thanks
 
Old 01-05-2005, 03:08 AM   #2
carboncopy
Senior Member
 
Registered: Jan 2003
Location: Malaysia
Posts: 1,210

Original Poster
Blog Entries: 4

Rep: Reputation: 45
Ok here is my partial script
Code:
ifconfig ppp0 | grep inet >> ppp0add
And I get one line:
Code:
        inet addr:218.111.178.xxx P-t-P:219.93.218.177   Mask:255.255.255.255
Now, need to throw away unwanted info and replace the IP between the markers.
 
Old 01-05-2005, 03:20 AM   #3
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
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.

Last edited by jschiwal; 01-05-2005 at 03:25 AM.
 
Old 01-05-2005, 03:28 AM   #4
carboncopy
Senior Member
 
Registered: Jan 2003
Location: Malaysia
Posts: 1,210

Original Poster
Blog Entries: 4

Rep: Reputation: 45
Thanks jschiwal

I managed to do something simillar using grep and awk.

here is my code:
Code:
ifconfig ppp0 | awk '{print $2}' | grep addr | awk 'BEGIN { FS=":" } { print $2 }' >> ppp0_latestIP
Will read up the sed manual.

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.
 
Old 01-05-2005, 04:20 AM   #5
carboncopy
Senior Member
 
Registered: Jan 2003
Location: Malaysia
Posts: 1,210

Original Poster
Blog Entries: 4

Rep: Reputation: 45
I am stuck now. I don't know how to replace the IP address in the rundns.conf file.
 
Old 01-05-2005, 07:58 AM   #6
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 61
Something like this may get you started .......

Edit: that smiley face isn't supposed to be there. it should be like this without spaces....
[ : punct : ]]

Code:
#!/bin/bash
addr=`/sbin/ifconfig eth0 |grep -o 'addr:[[:alnum:][:punct:]]*' |cut -d: -f2`
echo Your ip address is $addr
num="<ip>127.0.0.1</ip>"
line=`cat file.txt | grep -n $num | cut -d: -f1`
cat file.txt | sed -e ''$line'd'  | sed -e ''$line'i\'"  <ip>$addr</ip>"'' > file1.txt

Last edited by homey; 01-05-2005 at 08:00 AM.
 
Old 01-05-2005, 08:43 PM   #7
carboncopy
Senior Member
 
Registered: Jan 2003
Location: Malaysia
Posts: 1,210

Original Poster
Blog Entries: 4

Rep: Reputation: 45
Thanks!


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.
 
Old 01-05-2005, 09:09 PM   #8
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 61
You made changes while I was posting an answer.
Quote:
confile=/etc/rundns/rundns.conf
scratch=/etc/rundns/rundns.conf.updater
mv $confile $scratch
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.

Last edited by homey; 01-05-2005 at 09:19 PM.
 
Old 01-05-2005, 09:17 PM   #9
janic
LQ Newbie
 
Registered: Jan 2005
Distribution: SuSE, RHEL, FC on x86 and PPC64
Posts: 6

Rep: Reputation: 0
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:
Code:
...stuff...
<ip>IP_ADDR_REPLACE_ME</ip>
...more stuff...
and would simplify the script to look like:

Code:
#!/bin/bash
addr=`/sbin/ifconfig ppp0 |grep -o 'addr:[[:alnum:][:punct:]]*' |cut -d: -f2`
echo Your IP address is $addr
num=`grep -e \<ip /etc/rundns/rundns.conf.updater | awk '{ print $1 }'`
echo Your previous IP address was $num
cat /etc/rundns/rundns.conf.updater.tmpl | sed -e "s/IP_ADDR_REPLACE_ME/$addr/" > /etc/rundns/rundns.conf
I am a big believer in simplicity, so I would even go as far as nuking the two lines needed to show your previous address too ;)

Bonus points if you can reduce the entire script to one line. Semicolons don't count!

Cheers!
John
 
Old 01-05-2005, 09:41 PM   #10
carboncopy
Senior Member
 
Registered: Jan 2003
Location: Malaysia
Posts: 1,210

Original Poster
Blog Entries: 4

Rep: Reputation: 45
Thanks for all the feedbacks.

I am really a newbie in bash scripting

Thanks for the template idea, janic.

Homey:
So I can replace
Code:
#
# 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.
Code:
#!/bin/bash
addr=`/sbin/ifconfig ppp0 |grep -o 'addr:[[:alnum:][:punct:]]*' |cut -d: -f2`
cat /etc/rundns/rundns.conf.tmpl | sed -e "s/IP_ADDR_REPLACE_ME/$addr/" > /etc/rundns/rundns.conf

Last edited by carboncopy; 01-05-2005 at 09:51 PM.
 
Old 01-05-2005, 10:04 PM   #11
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 61
Looks good!
 
Old 01-06-2005, 01:38 AM   #12
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
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
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash script Linh Programming 4 04-21-2004 05:19 PM
send automatic input to a script called by another script in bash programming jorgecab Programming 2 04-01-2004 12:20 AM
bash script - incrementing a filename in a script tslinux Programming 10 08-05-2003 11:58 PM
bash script brian0918 Programming 7 06-12-2003 06:06 PM
bash script prob: how can i tell the script that a 'dd' has finished? Frustin Linux - General 2 04-02-2003 05:34 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 08:03 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration