Solved

Shell script as user with root access needed

Posted on 2012-03-26
6
358 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Problem logging tar errors 11 35
Ubuntu 16.04 AMD video card driver issues 20 112
Issue to mail 11 40
linux boot fsck problem 3 44
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…
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
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…

760 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now