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 |
Ok here is my partial script
Code:
ifconfig ppp0 | grep inet >> ppp0add Code:
inet addr:218.111.178.xxx P-t-P:219.93.218.177 Mask:255.255.255.255 |
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' 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. |
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 Here is the rundns.conf file which I want to update. Code:
<xml version="1.0"> |
I am stuck now. I don't know how to replace the IP address in the rundns.conf file.
|
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 |
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 |
You made changes while I was posting an answer. :)
Quote:
Edit: Quote:
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:
Code:
...stuff... Code:
#!/bin/bash Bonus points if you can reduce the entire script to one line. Semicolons don't count! Cheers! John |
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:
# Code:
cp -f $confile $scratch 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 |
Looks good! :)
|
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 |
All times are GMT -5. The time now is 03:26 AM. |