dan-e
May 4, 2011, 12:36am
1
Hi all,
I'm sure someone out there has an 'easy' way to insert a new user into a Tomcat config file without using vi or otherwise editing the file. For the uninitiated, here is the layout of the config file:
[root@ntt2 bin]# cat /apps/tomcat/conf/tomcat-users.xml
<?xml version='1.0' encoding='utf-8'?>
<tomcat-users>
<role rolename="manager"/>
<role rolename="tomcat"/>
<role rolename="admin"/>
<role rolename="role1"/>
<user username="z888666" password="b56e0b4ea4962283bee762525c2d490f" roles="admin"/>
<user username="admin" password="21232f297a57a5a743894a0e4a801fc3" roles="tomcat,role1,manager,admin"/>
<user username="z777333" password="b56e0b4ea4962283bee762525c2d490f" roles="admin"/>
</tomcat-users>
The 'password' data is a hash generated from this command:
[root@ntt2 ~]$ tomcat-passwd Welcome0
b56e0b4ea4962283bee762525c2d490f
What I'd like to be able to do is something of the form:
[root@ntt2 ~]$ tomcat-insert-user z696969 Welcome0 admin
or even:
[root@ntt2 ~]$ tomcat-insert-user z696969 b56e0b4ea4962283bee762525c2d490f admin
Or even an interactive one that allows you to enter the password etc would be fine.
Does anyone out there have a script/command sequence that will do this for me or have the smarts to whip it up?
I tried my own attempt but it was a bit of a disaster and stripped all the "'s out of the file and killed Tomcat
You can use sed:
sed -i '/<\/tomcat-users>/ i\<user username="z777333" password="b56e0b4ea4962283bee762525c2d490f" roles="admin"/>' tomcat-users.xml
You need to write a small piece of Bash wrapping the code so you can use:
tomcat-insert-user z696969 b56e0b4ea4962283bee762525c2d490f admin
That is left as an exercise for you.
sed '$d' infile > outfile
pword=$(tomcat-passwd $2)
echo ' <user username="'$1'" password="'$pword'" roles="'$3'" />' >> outfile
echo '</tomcat-users>' >> outfile
1 Like
dan-e
May 4, 2011, 1:03am
4
Excellent, I understand all of this code, thanks a plenty!
if your sed support -i option, no need temp file genereated by below code.
user=$1
pword=$(tomcat-passwd $2)
roles=$3
sed -i '$ s/\(.*\)/ <user username="'$user'" password="'$pword'" roles="'$roles'" \/> \n\1/' input.txt
rdcwayx:
if your sed support -i option, no need temp file genereated by below code.
user=$1
pword=$(tomcat-passwd $2)
roles=$3
sed -i '$ s/\(.*\)/ <user username="'$user'" password="'$pword'" roles="'$roles'" \/> \n\1/' input.txt
I think Using the i(insert) command would be more straightforward in this case.
---------- Post updated at 01:16 AM ---------- Previous update was at 01:15 AM ----------
sed -i '$ i\<user username="'$user'" password="'$pword'" roles="'$roles'" />' input.txt
Note: For this command to work, make sure there're no blank lines at the end of the file.
dan-e
May 4, 2011, 11:13pm
7
OK, but in your code, which part dictates it to insert within the
<tomcat-users>
...
</tomcat-users>
section?
I am also looking to improve upon this to make a script that will allow me to specify a log comment that it will insert at the end of the XML file as an XML comment using the <!- ---> tags, so I need a sed command that doesn't care about these trailing comments at the bottom...
sed -i '$ i\<user username="'$user'" password="'$pword'" roles="'$roles'" />' input.txt
When $(last line) is used, </tomcat-users> is assumed to appear at the end of the file. and i that follows inserts the text right before </tomcat-users>(last line).