Author Topic: Scripting Question(2 Actually)  (Read 4233 times)

bam

  • Hero Member
  • *****
  • Posts: 1213
    • View Profile
    • http://thegrinder.ws
Scripting Question(2 Actually)
« on: August 14, 2005, 02:04:28 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?

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".
SL-C3100 current: Stock/Tetsu 18h
Socket BT CF Card
Linksys WCF-12 802.11b/Cheapie USB Ethernet

The Grinder

Stubear

  • Hero Member
  • *****
  • Posts: 1164
    • View Profile
    • http://
Scripting Question(2 Actually)
« Reply #1 on: August 14, 2005, 10:41:35 am »
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
SL-C1000, Hand converted to English with Japanese Input
Running X apps via X/Qt
iRiver USB host cable; Diatec P-Cord usb power cable (extendable); Acro's Reel Cable USB (A to A, B, Mini-B,  & Mini-B 8pin); GreenHouse 1Gb PicoDrive+; 2x256Mb Hagiwara SD cards; 128Mb Transcend CF card; 512Mb PQI CF card; AmbiCom WL1100C-CF 11B WLAN card

bam

  • Hero Member
  • *****
  • Posts: 1213
    • View Profile
    • http://thegrinder.ws
Scripting Question(2 Actually)
« Reply #2 on: August 14, 2005, 12:12:22 pm »
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: [Select]
#!/bin/sh

#
# TimeAdjust.sh by Makio Tsukamoto
# 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
SL-C3100 current: Stock/Tetsu 18h
Socket BT CF Card
Linksys WCF-12 802.11b/Cheapie USB Ethernet

The Grinder

inode0

  • Jr. Member
  • **
  • Posts: 97
    • View Profile
Scripting Question(2 Actually)
« Reply #3 on: August 14, 2005, 12:37:34 pm »
Quote
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

bam

  • Hero Member
  • *****
  • Posts: 1213
    • View Profile
    • http://thegrinder.ws
Scripting Question(2 Actually)
« Reply #4 on: August 14, 2005, 12:56:33 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.
SL-C3100 current: Stock/Tetsu 18h
Socket BT CF Card
Linksys WCF-12 802.11b/Cheapie USB Ethernet

The Grinder

inode0

  • Jr. Member
  • **
  • Posts: 97
    • View Profile
Scripting Question(2 Actually)
« Reply #5 on: August 14, 2005, 02:32:25 pm »
Quote
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

Stubear

  • Hero Member
  • *****
  • Posts: 1164
    • View Profile
    • http://
Scripting Question(2 Actually)
« Reply #6 on: August 14, 2005, 08:42:09 pm »
Quote
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.
[div align=\"right\"][a href=\"index.php?act=findpost&pid=91808\"][{POST_SNAPBACK}][/a][/div]

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

Stu
SL-C1000, Hand converted to English with Japanese Input
Running X apps via X/Qt
iRiver USB host cable; Diatec P-Cord usb power cable (extendable); Acro's Reel Cable USB (A to A, B, Mini-B,  & Mini-B 8pin); GreenHouse 1Gb PicoDrive+; 2x256Mb Hagiwara SD cards; 128Mb Transcend CF card; 512Mb PQI CF card; AmbiCom WL1100C-CF 11B WLAN card

bam

  • Hero Member
  • *****
  • Posts: 1213
    • View Profile
    • http://thegrinder.ws
Scripting Question(2 Actually)
« Reply #7 on: August 14, 2005, 10:58:51 pm »
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.
« Last Edit: August 14, 2005, 11:01:02 pm by bam »
SL-C3100 current: Stock/Tetsu 18h
Socket BT CF Card
Linksys WCF-12 802.11b/Cheapie USB Ethernet

The Grinder

Stubear

  • Hero Member
  • *****
  • Posts: 1164
    • View Profile
    • http://
Scripting Question(2 Actually)
« Reply #8 on: August 15, 2005, 09:06:03 am »
You need to do
Code: [Select]
su -c ntpdate 'server' so that su nows what the command is, better if you use sudo (if its installed by default)

Stu
SL-C1000, Hand converted to English with Japanese Input
Running X apps via X/Qt
iRiver USB host cable; Diatec P-Cord usb power cable (extendable); Acro's Reel Cable USB (A to A, B, Mini-B,  & Mini-B 8pin); GreenHouse 1Gb PicoDrive+; 2x256Mb Hagiwara SD cards; 128Mb Transcend CF card; 512Mb PQI CF card; AmbiCom WL1100C-CF 11B WLAN card

bam

  • Hero Member
  • *****
  • Posts: 1213
    • View Profile
    • http://thegrinder.ws
Scripting Question(2 Actually)
« Reply #9 on: August 15, 2005, 02:11:11 pm »
ok sudo'd ntpdate, still "can't adjust date operation not permitted."....hmmm
SL-C3100 current: Stock/Tetsu 18h
Socket BT CF Card
Linksys WCF-12 802.11b/Cheapie USB Ethernet

The Grinder