Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Shell script as user with root access needed

Posted on 2012-03-26
6
Medium Priority
?
371 Views
Last Modified: 2012-03-31
I need to run a script to change permision to 766 on a file and change another file to a new version of the file. the issue is the script is being called by a java code as a normal user, however the 2 files have root only permission and cant be edited. what I need to do is be able to su -c | chmod 766 /opt/xxx/xxx/file.xml
and mv /etc/my.cnf  /opt/xxx/saved/
mv /opt/my.cnf /etc/

the 3 lines have to be executed as root and we can pass the root password as an incripted value in a variable, how can I create this bash script is my big question. I tried the below but as you can tell just by looking at it it does not work.

#!/bin/bash
PASSWORD="test"

echo $PASSWORD>su root -c chmod 766 /opt/ibm/javapos/jpos.xml

any ideas would be apriciated.
Thanks,
Atorex
0
Comment
Question by:atorex
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 29

Expert Comment

by:MikeOM_DBA
ID: 37766973
You need to use "sudo" (ask your sysadmin to set it up):
sudo -c "chmod 766 /opt/ibm/javapos/jpos.xml"

Open in new window

:p
0
 
LVL 31

Expert Comment

by:farzanj
ID: 37767046
As MikeOM_DBA said you either need to set up sudo as it appears you have the root password, or second option would be to enable ssh certificates and execute this command through ssh on localhost.  Third way is to use expect.  A regular script would not work because it cannot interact with a terminal (entering password).
0
 

Accepted Solution

by:
atorex earned 0 total points
ID: 37767056
I have the access to change the sudoers file however I need to make this work on 1800 systems and changing the sudoers on all of them is another project. I wqas actualy running my command out of order thats why it didnt work, the below will work and encrypted password from java code

echo $PASSWORD | su -c "chmod 766 /opt/ibm/javapos/jpos.xml" root
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 31

Expert Comment

by:farzanj
ID: 37767063
Then the option you have is to use expect.

http://en.wikipedia.org/wiki/Expect
0
 
LVL 20

Expert Comment

by:simon3270
ID: 37767569
Using expect is more portable:


echo Enter root password:
read RPW

expect <<EOF
spawn su - root -c "chmod 766 /opt/ibm/javapos/jpos.xml"
expect "sword"
send "${RPW}\r"
expect EOF
EOF
0
 

Author Closing Comment

by:atorex
ID: 37790331
echo $PASSWORD | su -c "chmod 766 /opt/ibm/javapos/jpos.xml" root

I'm using a java application to do most of whats needed and adding these to that process utalizing java encription to decrypt the password, I have tested the above and works.

Thanks for the imput.
0

Featured Post

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Over the years I've spent many an hour playing on hardened, DMZ'd servers, with only a sub-set of the usual GNU toy's to keep me company; frequently I've needed to save and send log or data extracts from these server back to my PC, or to others, and…
Setting up Secure Ubuntu server on VMware 1.      Insert the Ubuntu Server distribution CD or attach the ISO of the CD which is in the “Datastore”. Note that it is important to install the x64 edition on servers, not the X86 editions. 2.      Power on th…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Suggested Courses

718 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question