LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   bash script help me. (https://www.linuxquestions.org/questions/linux-software-2/bash-script-help-me-274037/)

carboncopy 01-05-2005 02:58 AM

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

carboncopy 01-05-2005 03:08 AM

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.

jschiwal 01-05-2005 03:20 AM

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.

carboncopy 01-05-2005 03:28 AM

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>


carboncopy 01-05-2005 04:20 AM

I am stuck now. I don't know how to replace the IP address in the rundns.conf file.

homey 01-05-2005 07:58 AM

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


carboncopy 01-05-2005 08:43 PM

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


homey 01-05-2005 09:09 PM

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.

janic 01-05-2005 09:17 PM

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

carboncopy 01-05-2005 09:41 PM

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


homey 01-05-2005 10:04 PM

Looks good! :)

jschiwal 01-06-2005 01:38 AM

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



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