Solved

Shell script as user with root access needed

Posted on 2012-03-26
6
368 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
Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

 
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

Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

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…
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
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…
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…
Suggested Courses

615 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