Code to insert user into Tomcat config file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Code to insert user into Tomcat config file
# 1  
Old 05-04-2011
Code to insert user into Tomcat config file

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:
Code:
[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:
Code:
[root@ntt2 ~]$ tomcat-passwd Welcome0
b56e0b4ea4962283bee762525c2d490f

What I'd like to be able to do is something of the form:
Code:
[root@ntt2 ~]$ tomcat-insert-user z696969 Welcome0 admin

or even:
Code:
[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 Smilie
# 2  
Old 05-04-2011
You can use sed:
Code:
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:
Code:
tomcat-insert-user z696969 b56e0b4ea4962283bee762525c2d490f admin

That is left as an exercise for you.
# 3  
Old 05-04-2011
Code:
sed '$d' infile > outfile
pword=$(tomcat-passwd $2)
echo '  <user username="'$1'" password="'$pword'" roles="'$3'" />' >> outfile
echo '</tomcat-users>' >> outfile

This User Gave Thanks to fpmurphy For This Post:
# 4  
Old 05-04-2011
Quote:
Originally Posted by fpmurphy
Code:
sed '$d' infile > outfile
pword=$(tomcat-passwd $2)
echo '  <user username="'$1'" password="'$pword'" roles="'$3'" />' >> outfile
echo '</tomcat-users>' >> outfile

Excellent, I understand all of this code, thanks a plenty!

Last edited by dan-e; 05-04-2011 at 02:16 AM.. Reason: Question solved...
# 5  
Old 05-04-2011
if your sed support -i option, no need temp file genereated by below code.

Code:
user=$1
pword=$(tomcat-passwd $2)
roles=$3
sed -i '$ s/\(.*\)/  <user username="'$user'" password="'$pword'" roles="'$roles'" \/> \n\1/' input.txt

# 6  
Old 05-04-2011
Quote:
Originally Posted by rdcwayx
if your sed support -i option, no need temp file genereated by below code.

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 ----------

Code:
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.

Last edited by kevintse; 05-04-2011 at 03:32 AM..
# 7  
Old 05-05-2011
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...
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Setting config database user and password using sed

Hello everybody, I need to modify 200 files using a patern matching, I would like to do it with sed but it's not working with the following syntax: sed -e 's/DATABASE_PASSWORD.*oldpass/DATABASE_PASSWORD__', 'newpass/g' config.php need to find: define("__DATABASE_PASSWORD__", ... (6 Replies)
Discussion started by: dco
6 Replies

2. Shell Programming and Scripting

Insert a user input string after matched string in file

i am having file like this #!/bin/bash read -p 'Username: ' uservar match='<color="red" />' text='this is only a test so please be patient <color="red" />' echo "$text" | sed "s/$match/&$uservar\g" so desireble output what i want is if user type MARIA this is only a test so please... (13 Replies)
Discussion started by: tomislav91
13 Replies

3. UNIX for Beginners Questions & Answers

Possible to insert a few lines of code into a file at a specific point?

Hi Folks - How would I go about inserting a chunk of lines (3) into a specific portion of a file? The background is I have a script (non shell) that it executed daily, however on Sundays, I uncomment a section of code so that piece can be run as well. So I was hoping to write a piece of... (9 Replies)
Discussion started by: SIMMS7400
9 Replies

4. Solaris

Tomcat..Unable to deploy application remotely in tomcat

Hi, We have upgrade tomcat from 5.0.20 to 7.0.33 and made changes to server.xml file according to newer version.. how ever the upgrade went fine and now i am unable to deploy application remotely.. it is giving 403 access denied error.. we have seperate appbase directory mentioned in server.xml..... (0 Replies)
Discussion started by: phani4u
0 Replies

5. UNIX for Advanced & Expert Users

postfix config: how to relay mails for only one user of a certain domain

Hello there, First of all I tell you that this is my first postfix installation so please be patient... I have following scenario: fetchmail --> postfix --> amavis-new --> postfix --> exchange 2010. Everything -except exchange ;-)- runs on an opensuse 12.1 box. Now, I have a list of... (0 Replies)
Discussion started by: lpacor
0 Replies

6. SuSE

How to config root kde same as user?

Eclipse looks completely different when run under root compared to my user. It's like kde wasn't setup for root upon installation. I'm running Suse 9.3 Pro. How do I configure root kde so that eclipse looks the same when run as user? (3 Replies)
Discussion started by: shwick2
3 Replies

7. Shell Programming and Scripting

Shell script that will compare two config files and produce 2 outputs 1)actual config file 2)report

Hi I am new to shell scripting. There is a requirement to write a shell script to meet follwing needs.Prompt reply shall be highly appreciated. script that will compare two config files and produce 2 outputs - actual config file and a report indicating changes made. OS :Susi linux ver 10.3. ... (4 Replies)
Discussion started by: muraliinfy04
4 Replies

8. Shell Programming and Scripting

parsing config file to create new config files

Hi, I want to use a config file as the base file and parse over the values of country and city parameters in the config file and generate separate config files as explained below. I will be using the config file as mentioned below: (config.txt) country:a,b city:1,2 type:b1... (1 Reply)
Discussion started by: clazzic
1 Replies

9. Solaris

Tomcat Error-HTTP Status code 500

when I typed path http://localhost:8080/MyFirst/HelloWorld in web-browser ,it came up with error HTTP Status 500 - type Exception report message description The server encountered an internal error () that prevented it from fulfilling this request. exception ... (0 Replies)
Discussion started by: srinivas2828
0 Replies

10. UNIX for Advanced & Expert Users

any reason for a user without a homedir - security/config/application?

Hi, Can I just quick pick everyone brain here about the following: There is a security audit going on at the company I work for and one of the things that needed to be resolved was that there were a lot of users who don't have a home directory. As this is a fairly large environment of over... (5 Replies)
Discussion started by: Solarius
5 Replies
Login or Register to Ask a Question