Solved

Shell script as user with root access needed

Posted on 2012-03-26
6
366 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 19

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Little introduction about CP: CP is a command on linux that use to copy files and folder from one location to another location. Example usage of CP as follow: cp /myfoder /pathto/destination/folder/ cp abc.tar.gz /pathto/destination/folder/ab…
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 several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
Suggested Courses

751 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