OESF | ELSI | pdaXrom | OpenZaurus | Zaurus Themes | Community Links | Ibiblio

IPB

Welcome Guest ( Log In | Register )

 
Reply to this topicStart new topic
> Scripting Question(2 Actually)
bam
post Aug 13 2005, 10:04 PM
Post #1





Group: Members
Posts: 1,213
Joined: 9-June 05
From: Gobi Desert, Mongolia
Member No.: 7,306



ive got a script, it uses ntpdate, but I want to run it as not root. So how in the script do I switch to root user, run ntpdate, let it complete and switch back?

Second question, I have a list of servers how would I use one(say out of a *.conf file) and if it doesnt return anything[server not there], go to the next one in the "list".
Go to the top of the page
 
+Quote Post
Stubear
post Aug 14 2005, 06:41 AM
Post #2





Group: Members
Posts: 1,164
Joined: 17-December 03
From: Melbourne, AUSTRALIA
Member No.: 1,219



There are 2 ways (actually there are more but these are the easy ways) to allow a normal user to run a script as root
1) make the script suid (chmod u+s script_name.sh)
2) call sudo or su within the script (su -c ntpdate or sudo ntpdate)
2a) sudo the script, ie it runs as root, but doesn't need to be set suid
Read up on the chmod, su or sudo to understand what they do and the dangers of using them

Stu
Go to the top of the page
 
+Quote Post
bam
post Aug 14 2005, 08:12 AM
Post #3





Group: Members
Posts: 1,213
Joined: 9-June 05
From: Gobi Desert, Mongolia
Member No.: 7,306



found this script but getting error msg's about root doesnt own qpe/qtpalmtop data directory, any ideas, it does what I mentioned above, except with these errors.

CODE
#!/bin/sh

#
# TimeAdjust.sh by Makio Tsukamoto <walrus@digit.que.ne.jp>
# Version : 1.3.0
#
# Time adjust by ntpdate.
# Usage : timeadjust.sh [-c] [NTPDATE PATH] [TIME SERVER]
#

# --- Default Setting ---
NTPDATE=/usr/bin/ntpdate
ICONPICT=/home/QtPalmtop/pics144/timeadjust.png
EXECMODE=do

# --- Check user "zaurus" existance ---
USER=`cut -d : -f 1,3,4 /etc/passwd | grep zaurus:500:500`
if [ "$USER" = "" ]; then
    USER=root:0:0
fi
USERHOME=`cut -d : -f 1,3,4,6 /etc/passwd | grep $USER | cut -d : -f 4`
CONFFILE=${USERHOME}/Settings/timeadjust.conf
LOGFILE=${USERHOME}/Documents/TimeAdjust.txt
TMPFILE=/tmp/TimeAdjust.txt
USER=`echo $USER | cut -d : -f 1`


# ---- Main Process ----
case "$EXECMODE" in
    'do')
 # --- read and rotate NTP server list ---
       TIMESERVER=`grep -v "#" -i $CONFFILE | head -1 | cut -f1`
     (
   grep -v "$TIMESERVER" $CONFFILE
   grep "$TIMESERVER" $CONFFILE
     ) >$TMPFILE
     mv $TMPFILE $CONFFILE
     chown $USER $CONFFILE
 
 #--- Time Adjust ---
 qcop QPE/TrayIconCtl "add(QString,QString)" timeadjust $ICONPICT
 qcop QPE/TrayIconCtl "removeWith(QString,int)" timeadjust $$
 (
     echo "--- TimeAdjust ---"
     echo "NTPDATE    : $NTPDATE"
     echo "USER       : $USER"
     echo "LOGFILE    : $LOGFILE"
     echo "TIMESERVER : $TIMESERVER"
     echo "START      : "`date`
 ) >>$LOGFILE
 $NTPDATE $TIMESERVER >>$LOGFILE 2>&1
 echo "FINISH     : "`date` >>$LOGFILE
 qcop QPE/TrayIconCtl "remove(QString)" timeadjust

 # ---- rotate log file ----
 tail -20 $LOGFILE>$TMPFILE
 mv $TMPFILE $LOGFILE
 chown $USER $LOGFILE
    ;;
    'clear')
 # --- Process clear ----
 if [ "$EXECMODE" = "clear" ]; then
     killall ntpdate
     qcop QPE/TrayIconCtl "remove(QString)" timeadjust    
 fi
    ;;
esac
Go to the top of the page
 
+Quote Post
inode0
post Aug 14 2005, 08:37 AM
Post #4





Group: Members
Posts: 97
Joined: 15-December 04
Member No.: 5,933



QUOTE(bam @ Aug 14 2005, 06:04 AM)
ive got a script, it uses ntpdate, but I want to run it as not root. So how in the script do I switch to root user, run ntpdate, let it complete and switch back?

How fancy do you need running ntpdate to be? Would just running it once to set your clock on each network connection be sufficient for your purposes?

John
Go to the top of the page
 
+Quote Post
bam
post Aug 14 2005, 08:56 AM
Post #5





Group: Members
Posts: 1,213
Joined: 9-June 05
From: Gobi Desert, Mongolia
Member No.: 7,306



yea, I guess so, just learning stuff thru this, btw I am using nethelper, it is supposed to detect a net connection(like eth0) but it doesnt when I place a script to run under Run_All or Run_ETH0 or Run_eth0, any ideas?

Also, figure I just need a ntpdate theat can be run under just zaurus user and not su, so I need to read on that.
Go to the top of the page
 
+Quote Post
inode0
post Aug 14 2005, 10:32 AM
Post #6





Group: Members
Posts: 97
Joined: 15-December 04
Member No.: 5,933



QUOTE(bam @ Aug 14 2005, 04:56 PM)
yea, I guess so, just learning stuff thru this, btw I am using nethelper, it is supposed to detect a net connection(like eth0) but it doesnt when I place a script to run under Run_All or Run_ETH0 or Run_eth0, any ideas?

Also, figure I just need a ntpdate theat can be run under just zaurus user and not su, so I need to read on that.

Well the nethelper stuff is out of my element but I've posted elsewhere how to configure internal and external wireless cards to run ntpdate when the interface is brought up.

ntpdate should run fine as zaurus with the exception of actually setting the time. I'd recommend not changing that behavior as it is there for a reason. On the zaurus I suppose it isn't so critical, but it is a bad habit to develop. Typing sudo ntpdate isn't that much harder and neither is using su to change your privilege level prior to running commands like ntpdate. I don't think a non-privileged user should really need to run ntpdate anyway. Keeping the clock in sync should be done by the system without any intervention from normal users.

But it sounds like an exercise from which you can learn some new tricks to add to your arsenal for future use.

John
Go to the top of the page
 
+Quote Post
Stubear
post Aug 14 2005, 04:42 PM
Post #7





Group: Members
Posts: 1,164
Joined: 17-December 03
From: Melbourne, AUSTRALIA
Member No.: 1,219



QUOTE(bam @ Aug 15 2005, 01:12 AM)
found this script but getting error msg's about root doesnt own qpe/qtpalmtop data directory, any ideas, it does what I mentioned above, except with these errors.
*


This script needs to be run as user zaurus or as root using "su -" rather than just "su"

Stu
Go to the top of the page
 
+Quote Post
bam
post Aug 14 2005, 06:58 PM
Post #8





Group: Members
Posts: 1,213
Joined: 9-June 05
From: Gobi Desert, Mongolia
Member No.: 7,306



ive tried to set up the script as follows, running it as zaurus

#!/bin/sh
su ntpdate 'server'
kill ntpdate

It wont update the time, why?


how would I make it so ntpdate doesnt care who runs it, nah better do this the right way, thru script. So what am I doing wrong? Point me to some good tutorials.
Go to the top of the page
 
+Quote Post
Stubear
post Aug 15 2005, 05:06 AM
Post #9





Group: Members
Posts: 1,164
Joined: 17-December 03
From: Melbourne, AUSTRALIA
Member No.: 1,219



You need to do
CODE
su -c ntpdate 'server'
so that su nows what the command is, better if you use sudo (if its installed by default)

Stu
Go to the top of the page
 
+Quote Post
bam
post Aug 15 2005, 10:11 AM
Post #10





Group: Members
Posts: 1,213
Joined: 9-June 05
From: Gobi Desert, Mongolia
Member No.: 7,306



ok sudo'd ntpdate, still "can't adjust date operation not permitted."....hmmm
Go to the top of the page
 
+Quote Post

Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 



RSS Lo-Fi Version Time is now: 19th December 2014 - 09:28 AM