#!/usr/bin/sh #---------------------------------------------------------------------------------- # /usr/bin/lprng_power-access - access allnet device, like ALL3075 from lprng-filter # # Copyright (c) 2010-2024 The Eisfair Team, team(at)eisfair(dot)org # # Creation: 2010-06-12 jed # Last Update: $Id$ # # Usage: lprng_power-access [--quiet|-q] --instance|-i --on[-status] # - switch device on # lprng_power-access [--quiet|-q] --instance|-i --on-autooff # - switch device on and automatically off # lprng_power-access [--quiet|-q] --instance|-i --off[-status] # - switch device off # lprng_power-access [--quiet|-q] --instance|-i --toggle[-status] # - switch device off # lprng_power-access [--quiet|-q] --instance|-i --status # - print device status # lprng_power-access [--quiet|-q] --help - show this help # # Hint: Instead of the instance number (--instance ) the queue # name can be given by using "--queue|-qq ". # # Pre-conditions (will automatically be done by the lprng_power.sh configuration script): # - A configuration file '/etc/lprng_power-access.conf' needs to be created and the # required parameters need to be set in the following order: # !!!!!!!!... # - User 'lp' needs to be added to /etc/at.allow to allow switching on/off of # printers. # - A symbolic link, e.g. '/usr/bin/lprng_power-1-off', needs to be created which # points to '/usr/bin/lprng_power-access' to enable switching of by running an # at-command. The middle part of the link needs to show the correct instance # number. In the example the first instance number has been used. # - An additional command needs to be added to the script '/var/install/bin/lprng-filter' # to allow automatic switching-on of a printer. Since lprng v1.16.2 the 3rd-party # interface of the lprng-filter script will automatically be used so that this # modification is no longer required. # # ... # echo "$druckdatum $queue $user@$client $jobgroesse KByte" >> /var/spool/lprng/log.lprng # + # + if [ -f /usr/bin/lprng_power-access ] # + then # + /usr/bin/lprng_power-access -queue ${queue} -on-autooff # + fi # + # exit 0 # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. #---------------------------------------------------------------------------------- # read eislib etc. . /var/install/include/eislib . /var/install/include/jedlib # debug mode: true/false #debug=true if ${debug:-false} then exec 2>/tmp/$(basename ${0})-trace$$.log set -x ask_debug=true export ask_debug fi #------------------------------------------------------------------------------ # my own echo #------------------------------------------------------------------------------ ownecho () { local _oe_switch local _oe_outstr _oe_switch='' _oe_outstr='' while [ $# -gt 1 ] do case $1 in --std ) _oe_switch="${_oe_switch} $1" shift _oe_outstr="`echo "$*" | sed -r 's/^(-)?-std //g'`" ;; --info ) _oe_switch="${_oe_switch} $1" shift _oe_outstr="`echo "$*" | sed -r 's/^(-)?-info //g'`" ;; --warn ) _oe_switch="${_oe_switch} $1" shift _oe_outstr="`echo "$*" | sed -r 's/^(-)?-warn //g'`" ;; --error ) _oe_switch="${_oe_switch} $1" shift _oe_outstr="`echo "$*" | sed -r 's/^(-)?-error //g'`" ;; -n ) _oe_switch="${_oe_switch} $1" shift _oe_outstr="`echo "$*" | sed -r 's/^-n //g'`" ;; * ) _oe_switch='' _oe_outstr="$*" break ;; esac done if [ ${force_quiet_run} -eq 0 ] then # be verbose mecho ${_oe_switch} "${_oe_outstr}" fi } #---------------------------------------------------------------------------------- # check device status # # output: 0 - device status: off # 1 - device status: on #---------------------------------------------------------------------------------- is_power_off () { local _ipo_ret _ipo_ret=0 # write command to file if [ -n "${url_status_filter}" ] then echo "curl ${options} -m ${curl_timeout} -s \"${proto}://${server}${url_status}\" | ${url_status_filter}" > ${tmp_exec_file} else echo "curl ${options} -m ${curl_timeout} -s \"${proto}://${server}${url_status}\"" > ${tmp_exec_file} fi # exec command _ipo_ret=`/usr/bin/sh ${tmp_exec_file}` # if [ -z "${_ipo_ret}" ] # then # mecho --error "communication error with device '${server}'!" # fi return ${_ipo_ret} } #---------------------------------------------------------------------------------- # set device status # # input: on - switch device off # off - switch device on #---------------------------------------------------------------------------------- set_power_status () { local _sps_ret _sps_ret=1 case $1 in on ) echo "curl ${options} -m ${curl_timeout} -s \"${proto}://${server}${url_on}\"" > ${tmp_exec_file} # execute command /usr/bin/sh ${tmp_exec_file} 2>&1 >/dev/null _sps_ret=$? ;; off ) echo "curl ${options} -m ${curl_timeout} -s \"${proto}://${server}${url_off}\"" > ${tmp_exec_file} # exec command /usr/bin/sh ${tmp_exec_file} 2>&1 >/dev/null _sps_ret=$? ;; * ) _sps_ret=99 ;; esac return ${_sps_ret} } #---------------------------------------------------------------------------------- # print device status #---------------------------------------------------------------------------------- print_status () { ownecho -n "Status of device ${instance} (${queue}): " if is_power_off then errval=0 ownecho --warn "off" else errval=1 ownecho --info "on" fi } #================================================================================== # main #================================================================================== module_name='lprng_power' pgmname=`basename $0` debug_cmd_line="$*" run_user=`whoami` run_time=`date +"%F %T.%N"` queue='' instance='' proto='http' server='' port='80' user='' pass='' action='' options='' force_quiet_run=0 errval=0 noentry=1 curl_timeout=3 # timeout in seconds lp_user='lp' config_file=/etc/${module_name}-access.conf # configuration file if [ ! -f ${config_file} ] then ownecho --error "configuration file '${config_file}' not found!" exit 1 else # check if configuration has been disabled grep -q "^disabled!" ${config_file} if [ $? -eq 0 ] then action='disabled' fi fi if [ -z "${action}" ] then echo "${pgmname}" | grep -q "^${module_name}-access" if [ $? -eq 0 ] then # manual mode while [ $# -gt 0 ] do case $1 in -*instance|-i ) instance="$2" shift; shift # !!!!!!!!... grep -E -q "^[-_a-zA-Z0-9]+!${instance}!" ${config_file} if [ $? -eq 0 ] then # entry exists, read queue name noentry=0 queue=`grep -E "^[-_a-zA-Z0-9]+!${instance}!" ${config_file} | cut -d'!' -f1` fi ;; -*on|-*on-status|-*on-autooff|-*off|-*off-status|-*toggle|-*toggle-status|-*status ) action=$1 shift ;; -*queue|-qq ) queue="$2" shift; shift # !!!!!!!!... grep -q "^${queue}!" ${config_file} if [ $? -eq 0 ] then # entry exists, read instance number instance=`grep "^${queue}!" ${config_file} | cut -d'!' -f2` if is_numeric "${instance}" then noentry=0 fi fi ;; -*quiet|-q ) force_quiet_run=1 shift ;; -*help|-h|-?|/? ) # show help mecho "Usage:" mecho " ${pgmname} [--quiet] -instance --on - switch device on" mecho " ${pgmname} [--quiet] -instance --on-autooff - switch device on and automatically off" mecho " ${pgmname} [--quiet] -queue --on-autooff - switch device on and automatically off" mecho " ${pgmname} [--quiet] -instance --off - switch device off" mecho " ${pgmname} [--quiet] -instance --status - print device status" mecho " ${pgmname} --help - show this help" exit 1 ;; * ) break ;; esac done else # automatic mode - lprng_power-1-off action="-`echo ${pgmname} | cut -d- -f3`" instance=`echo ${pgmname} | cut -d- -f2` # ::::: grep -E -q "^[-_a-zA-Z0-9]+!${instance}!" ${config_file} if [ $? -eq 0 ] then # entry exists, read queue name noentry=0 queue=`grep -E "^[-_a-zA-Z0-9]+!${instance}!" ${config_file} | cut -d'!' -f1` fi force_quiet_run=1 fi case ${action} in -*on|-*on-status|-*on-autooff|-*off|-*off-status|-*toggle|-*toggle-status|-*status ) ;; * ) ownecho --error "parameter 'action' contains an invalid value!" ;; esac if [ -z "${instance}" ] then ownecho --error "parameter 'instance' not set!" fi if [ -z "${queue}" ] then ownecho --error "parameter 'queue' not set!" fi fi if [ ${noentry} -eq 0 ] then # parameters exist, go on ... # 1 2 3 4 5 6 7 8 9 10 11 12 # !!!!!!!:!!! proto=`grep "^${queue}!" ${config_file} | cut -d'!' -f3` server=`grep "^${queue}!" ${config_file} | cut -d'!' -f4` port=`grep "^${queue}!" ${config_file} | cut -d'!' -f5` user=`grep "^${queue}!" ${config_file} | cut -d'!' -f6` pass=`grep "^${queue}!" ${config_file} | cut -d'!' -f7` timeout=`grep "^${queue}!" ${config_file} | cut -d'!' -f8` url_on=`grep "^${queue}!" ${config_file} | cut -d'!' -f9` url_off=`grep "^${queue}!" ${config_file} | cut -d'!' -f10` url_status=`grep "^${queue}!" ${config_file} | cut -d'!' -f11` url_status_filter=`grep "^${queue}!" ${config_file} | cut -d'!' -f12-` # check parameters if [ -z "${proto}" ] then # default tcp-port proto='http' fi if [ -z "${server}" ] then ownecho --error "parameter 'server' not set!" exit 1 fi atjob_id_file=/var/run/${module_name}-${instance}.id atjob_run_cmd="/usr/bin/${module_name}-${instance}-off" atjob_tmp_file=/tmp/${module_name}-${instance}-tmp.$$ tmp_exec_file=/tmp/${module_name}-${instance}-exec.$$ tmp_pw_file=/tmp/${module_name}-${instance}-pw.$$ if [ -z "${port}" ] then # default tcp-port port='80' fi if [ -n "${user}" -a -n "${pass}" ] then # authentication # options="-u ${user}:${pass}" # write authentication credentials to a file echo "machine ${server} login ${user} password ${pass}" > ${tmp_pw_file} options="--netrc-file ${tmp_pw_file}" fi if [ -z "${timeout}" ] then # default timeout in minutes timeout='10' fi case ${action} in -*on|-*on-status|-*on-autooff ) # switch device on if is_power_off then # current status: off set_power_status 'on' if is_power_off then /usr/bin/logger -p "local0.err" -t "${module_name}-access" "${server} - switch-on error." else /usr/bin/logger -p "local0.info" -t "${module_name}-access" "${server} - switched on." fi fi atjob_nbr='' if [ -f ${atjob_id_file} ] then # previous job found, delete it atjob_nbr=`cat ${atjob_id_file}` if [ -n "${atjob_nbr}" ] then # delete previous job atrm ${atjob_nbr} fi # delete id file rm -f ${atjob_id_file} fi case "${action}" in -*on ) if [ -n "${atjob_nbr}" ] then /usr/bin/logger -p "local0.err" -t "${module_name}-access" "${server} - at-job removed." fi ;; -*on-status ) print_status if [ -n "${atjob_nbr}" ] then /usr/bin/logger -p "local0.err" -t "${module_name}-access" "${server} - at-job removed." fi ;; -*on-autooff ) # create new job if [ "${run_user}" = "${lp_user}" ] then # run as 'lp' user, don't switch user to execute script ... echo ${atjob_run_cmd} | at now +${timeout} minutes > /dev/null 2> ${atjob_tmp_file} else # run as non-'lp' user, switch user ... su ${lp_user} -c "echo ${atjob_run_cmd} | at now +${timeout} minutes" > /dev/null 2> ${atjob_tmp_file} fi atjob_nbr=`sed -e '/^job .* at/!d' -e 's/^job //' -e 's/ at.*$//' ${atjob_tmp_file}` rm -f ${atjob_tmp_file} # save job number echo "${atjob_nbr}" > ${atjob_id_file} chown ${lp_user} ${atjob_id_file} chmod 0666 ${atjob_id_file} /usr/bin/logger -p "local0.err" -t "${module_name}-access" "${server} - at-job ${atjob_run_cmd} +${timeout} min added." ;; esac ;; -*off|-*off-status ) # switch device off if ! is_power_off then # current status: on set_power_status 'off' if ! is_power_off then /usr/bin/logger -p "local0.err" -t "${module_name}-access" "${server} - switch-off error." else if [ "${pgmname}" = "${module_name}-access" ] then /usr/bin/logger -p "local0.info" -t "${module_name}-access" "${server} - switched off." else /usr/bin/logger -p "local0.info" -t "${module_name}-access" "${server} - switched off automatically." fi fi fi case "${action}" in -*off-status ) print_status ;; * ) ;; esac if [ -f ${atjob_id_file} ] then # delete id file rm -f ${atjob_id_file} fi ;; -*toggle|-*toggle-status ) # toggle device status if is_power_off then # current status: off set_power_status 'on' if is_power_off then /usr/bin/logger -p "local0.err" -t "${module_name}-access" "${server} - switch-on error." else /usr/bin/logger -p "local0.info" -t "${module_name}-access" "${server} - switched on." fi else # current status: on set_power_status 'off' if ! is_power_off then /usr/bin/logger -p "local0.err" -t "${module_name}-access" "${server} - switch-off error." else if [ "${pgmname}" = "${module_name}-access" ] then /usr/bin/logger -p "local0.info" -t "${module_name}-access" "${server} - switched off." fi fi fi case "${action}" in -*toggle-status ) print_status ;; * ) ;; esac ;; * ) # print status print_status ;; esac rm -f ${tmp_exec_file} ${tmp_pw_file} fi exit ${errval} #================================================================================== # end #==================================================================================