1
我試圖安裝cronjob在親戚的機器上運行bash shell腳本。他們將運行安裝,我無法遠程訪問它(這是我的腳本是 - - 但這不是問題在這裏)。我使用kdialog來請求他們的root密碼,然後想用它來sudo各種命令。我的下面的代碼失敗了:a)揭示終端上的root p/w,以及b)未能將其管理到各種sudos。幫幫我?如何在我的bash安裝腳本中使用用戶提供的root密碼
#!/bin/bash
kdialog --password "Please enter your root password to install theCronScript.sh and set up cron"
# Sanity checks =========================================╕
if test -z "$BASH" ; then
printf "$SCRIPT:$LINENO: please run this script with the BASH shell\n">&2
exit 192
fi
#========================================================╛
# Global variables=======================================╕
PW="$?"
THISDIR="$(pwd)"
GETIPFILE='theCronScript.sh'
CRONPERIOD='/15 * * * * '
TARGETCRONDIR='/etc/cron.hourly'
#========================================================╛
echo "hi"
# txt file exists check =================================╕
echo "Checking:"
if [ ! -f "$THISDIR/$GETIPFILE" ]; then #there's no file to install
kdialog --msgbox "I cannot find $GETIPFILE to upload\nPlease check attachments in recent e-mails from Greg and download $GETIPFILE to $THISDIR"
exit
else
if [ -f "$TARGETCRONDIR/$GETIPFILE" ]; then #the target already exists
kdialog --title "Replace or Keep" --warningyesno "A similar file already exists.\n Do you want to replace it (recommended)?\n(The original file will be saved with a different name _OLD)"
if [ $? = 0 ]; then # rename, then replace the existing file
#echo $PW is probably unneccessary beyond the first use but just in case...
RNGETIPFILE=$GETIPFILE'_OLD'
echo $PW | sudo -S mv $TARGETCRONDIR/$GETIPFILE $TARGETCRONDIR/$RNGETIPFILE #rename original file
echo $PW | sudo -S cp $THISDIR/$GETIPFILE $TARGETCRONDIR/$GETIPFILE #copy new version in
echo $PW | sudo -S chmod +x $TARGETCRONDIR/$GETIPFILE #
echo $PW | sudo -S crontab -l > mycron #write out current crontab
echo $PW | sudo -S echo $CRONPERIOD $TARGETCRONDIR >> mycron #echo new cron into cron file
echo $PW | sudo -S crontab mycron #install new cron file
rm mycron
$PW="" #clear password variable once it's no longer required
else # Don't replace, exit
exit
fi
else # Nothing to replace. Just copy it in
echo $PW | sudo -S "cp $THISDIR/$GETIPFILE $TARGETCRONDIR/$GETIPFILE" #copy new version in
echo $PW | sudo -S chmod +x $TARGETCRONDIR/$GETIPFILE # make sure it's executable
echo $PW | sudo -S crontab -l > mycron #write out current crontab
echo $PW | sudo -S echo $CRONPERIOD $TARGETCRONDIR >> mycron #echo new cron into cron file
echo $PW | sudo -S crontab mycron #install new cron file
rm mycron
$PW="" #clear password variable once it's no longer required
fi
fi
exit 0
#========================================================╛
你爲什麼不乾脆指示他們來運行'sudo'腳本? – tripleee 2014-11-25 11:09:13
作爲最簡單(雖然不太吸引人)的解決方案,我可以做到這一點,並在終端上交互腳本。考慮到時間和技能,zeroconf將成爲我的首選路線。我只是擔心不得不添加另一個shell腳本(來封裝外部kdialog命令)。 – Greg 2014-11-25 22:18:27
你總是可以使用'ssh-askpass',它可以正常工作,但它看起來像是在尋求一個ssh密碼。你也可以讓腳本實際上在運行之前創建'kdialog'包裝器腳本(不需要額外的文件)。讓我知道你是否想要一個例子。 – zerodiff 2014-11-26 19:56:28