Solved

Shell script as user with root access needed

Posted on 2012-03-26
6
362 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
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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

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

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Suggested Solutions

Title # Comments Views Activity
ifconfig 4 69
How to enable sync between two yum repo? 1 39
windows 7 starter missing password 21 89
centos commands 6 67
Over the last ten+ years I have seen Linux configuration tools come and go. In the early days there was the tried-and-true, all-powerful linuxconf that many thought would remain the one and only Linux configuration tool until the end of times. Well,…
If you have a server on collocation with the super-fast CPU, that doesn't mean that you get it running at full power. Here is a preamble. When doing inventory of Linux servers, that I'm administering, I've found that some of them are running on l…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
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…

791 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