For last months I needed to maintain a number of heterogeneous servers for mi work, I need to do some usually actions, like update a config file, restart a service, create local users etc.
For this purposes there are a lot of applications, like dsh (or full csm), pysh, shmux and many others (only need to perform a search in google using phrase “distributed shell”). Unfortunately for me, I want a easy-to-parse solution, because I’ve a big (really big) number of servers, and I want a single cut-based/awk parsing, and also I need to do some actions as other users (like root, for example) via sudo. Althought many of the existants solutions offers me a subset of this features, I cannot found a complete solution. So I decided to create one
You can find the code, and some packages in the dtools development site. I was use this solution in production environment from months with excelent results, and you can feel free to use.
Of course, its free (of freedom) software, distributed under MIT license.
Enjoy and remember: feedback are welcome
Shawn 5:27 pm on 18 February 2008 Permalink |
Uh-oh:
eval: line 2: syntax error near unexpected token `}’
eval: line 2: `}’
Hopefully this is just a typo in the code above as I’d be super happy to get this to work and I’m just not quite good enough to figure out what is wrong.
Thanks for posting this!
ajdiaz 6:22 pm on 18 February 2008 Permalink |
Mmm, the code works for me
There are my ‘myfile.ini’:
[~]$ cat myfile.ini
[sec2]
var2=XXX
And the result:
[~]$ ./ini.sh
XXX
Maybe you need bash >= 3.0?, I’m using:
[~]$ echo ${BASH_VERSION}
3.2.17(1)-release
If you want, send me the result of the command:
$ bash -x ini.sh
And I try to help you
Thanks for your comments!!
Greg 1:58 pm on 3 March 2008 Permalink |
This script also doesn’t work if the ini file is not fully standard, like samba’s smb.conf. In that, there are some white spaces on both sides of the equation sign.
ajdiaz 10:50 pm on 8 March 2008 Permalink |
Sure, the script do not work fine with non stardard files, becaus the script parse file variables as bash variables and spaces are not allowed in bash variables. But, you are free to replace spaces with another character.
Tomas 2:24 pm on 29 April 2008 Permalink |
This works great!
Do you have any functionality for updating the functions array dynamically without re-parsing the ini-file?
Example: Add new section or add/update field within a section.
I’m considering trying to code this, but it would of course save me lots of time if it already exists
ajdiaz 3:34 pm on 1 May 2008 Permalink |
Sorry, I don’t code any functions to update the array. Really this hack do not use an array. We read the ini and create a couple of variables with the same name as variable in the file, and change the section creating a function called the section name. So if you need to add a new section, you might do:
cfg.section.MYNEWSECTION ()
{
MYNEWVAR1=”MYNEWVALUE1″
# any other variable
}
Now I’m working in a dump function to write the ini from specified functions.
Emanuele 3:16 pm on 8 May 2008 Permalink |
Hi,
I’ve only copy the function in my bash script and I receive the following error:
‘cfg.parser’:not a valid identifier
someone know why?
BASH_VERSION -> 3.1.17(1)
ajdiaz 5:04 pm on 10 May 2008 Permalink |
Yes, probably your bash is running with POSIX option (check the value of $HELLOPTS), in this case the dot (.) is not a valid character for function name.
If you really require a POSIX shell compatibility, you can replace cfg.parser and cfg.sections by cfg_parser and cfg_sections, the underline is always a valid character
Emanuele 10:00 am on 12 May 2008 Permalink |
Thanks! now I try!
Henning 2:37 pm on 20 June 2008 Permalink |
The source listing is somehow garbled. There’s something wrong with quotes (backticks?). The show up as a single character (” resp. “=
ajdiaz 2:48 pm on 20 June 2008 Permalink |
Sure, the wordpress format is not very readable with selected theme, use the pastebin code in http://pastebin.com/f61ef4979 instead
Stephen 10:13 pm on 27 June 2008 Permalink |
The following will parse out a file that has spaces between the ‘=’ as well as both ; and # comments. Just thought I would share it.
function cfg.parser ()
{
IFS=$’\n’ && ini=( $(<$1) ) # convert to line-array
ini=( ${ini[*]//;*/} ) # remove comments ‘;’
ini=( ${ini[*]//\#*/} ) # remove comments ‘#’
ini=( ${ini[*]/\ =\ /=} ) # remove anything with a space around ‘ = ‘
ini=( ${ini[*]/#[/\}$'\n'cfg.section.} ) # set section prefix
ini=( ${ini[*]/%]/ \(} ) # convert text2function (1)
ini=( ${ini[*]/=/=\( } ) # convert item to array
ini=( ${ini[*]/%/ \)} ) # close array parenthesis
ini=( ${ini[*]/%\( \)/\(\) \{} ) # convert text2function (2)
ini=( ${ini[*]/%\} \)/\}} ) # remove extra parenthesis
ini=( ${ini[*]/#\ */} ) # remove blank lines
ini=( ${ini[*]/#\ */} ) # remove blank lines with tabs
ini[0]=” # remove first element
ini[${#ini[*]} + 1]=’}’ # add the last brace
#printf “%s\n” ${ini[*]}
eval “$(echo “${ini[*]}”)” # eval the result
}
Lars 7:31 pm on 1 September 2008 Permalink |
Thanks for your script!
Is there any chance to get the available variables of a section?
For example: if I know that there’s a section [sec2], how can I print out the content of this section?
I think I need something like this:
for var in cfg.section.sec2; do
print “variable: %s\n” $var;
print “value: %s\n” $var[*];
done
but that doesn’t work for me.
ajdiaz 1:01 pm on 2 September 2008 Permalink |
declare -f cfg.section.sec2 | grep ‘=’ must be work Lars
frank 9:31 pm on 16 October 2008 Permalink |
Just a quick question… At one point I had this working, however, now all but the first section has been replaced by the number ‘1′. It’s replace right after this line:
IFS=$’\n’ && ini=( $(<$1) )
I’m using bash 3.0. Any thoughts?
Thanks for the help.