#!/bin/bash # Company: PowerCraft Technology # Author: Copyright Jelle de Jong # Note: Please send me an email if you enhanced the script # Date: 2008-11-22 / 2008-12-07 / ... / 2009-02-15 / 2009-06-13 # 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 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, # MA 02110-1301, USA. TEXTDOMAINDIR=/usr/share/locale TEXTDOMAIN=pct-support-login SYSCONF="/etc/pct-support-scripts.conf" SYSDIR="/etc/pct-support-scripts.conf.d/" SECURITY="-o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -o NumberOfPasswordPrompts=13" version="0.1.6" author="Jelle de Jong " donation="http://www.tuxcrafter.net/pages/contact.html#paypal-donation" homepage="https://secure.powercraft.nl/svn/packages/trunk/source/pct-support-scripts/" licence="http://www.gnu.org/licenses/gpl-3.0.html" unset USERNAME unset SERVER unset PORT unset SCAN unset CONFIG function version() { echo $"Usage: $0 --help" echo $"Version: $version" echo $"Author: $author" echo $"Donation: $donation" echo $"Homepage: $homepage" echo $"Licence: $licence" } function usage() { echo "Program to establish a remote support session" echo echo "Usage: $0 [options]" echo echo "Options:" echo " -u, --user ... user" echo " -s, --server ... server" echo " -p, --port ... server port to connect to" echo " -n, --scan ... scanning for support screen session (yes, no)" echo " -c, --config ... name of id-string used in configuration file" echo " -h, --help, -? show this help information" echo " -v, --version show the version and author information" echo echo "Example:" echo " pct-support-login" echo " pct-support-login --user support01 --server powercraft.nl" echo " pct-support-login --scan yes" echo " pct-support-login -u support01 -s powercraft.nl -n" echo " pct-support-login --config config2" echo echo "Configuration:" echo " $SYSCONF" echo " $SYSDIR" echo echo " In the configuration files the default user, server and port" echo " can be set. Pleae look inside the files to get a better idea" echo " of how to configure them. The --config option can be used to" echo " select additional configurations. For example using" echo " --config config2 will use the section [config2]." echo echo " It is always possible to use the command options, or the" echo " configuration system to make profiles for the servers you" echo " want to contact for remote support, it is a generic system" echo " by design." echo echo " This program can be used to request a remote support ticket" echo " at PowerCraft Technology. You can find contact data on our" echo " main website. We give support from everywere to everywere." echo echo "Website:" echo " https://secure.powercraft.nl/svn/packages/" echo "Mailinglist:" echo " http://www.powercraft.nl/cgi-bin/mailman/listinfo/packages-commit" echo " http://www.powercraft.nl/cgi-bin/mailman/listinfo/packages-devel" } while [[ "$1" == -* ]]; do case "$1" in -h|--help|-\?) usage; exit 0;; -u|--user) USERNAME="$2"; shift; shift;; -s|--server) SERVER="$2"; shift; shift;; -p|--port) PORT="$2"; shift; shift;; -n|--scan) SCAN="$2"; shift; shift;; -c|--config) CONFIG="$2"; shift; shift;; -v|--version) version; exit 0;; --) shift; break;; -*) echo 1>&2 $"invalid option: $1"; usage; exit 1;; esac done function scan_configuration() { file="$1" unset options [ ! -z "$USERNAME" ] && options+=("USERNAME") [ ! -z "$SERVER" ] && options+=("SERVER") [ ! -z "$PORT" ] && options+=("PORT") [ ! -z "$SCAN" ] && options+=("SCAN") for value in "${options[@]}" do sed -n -i -e "/$value/!p" "$file" done } function get_configuration() { section="$1" if [ ! -z "$section" ] && [ ! -z "$CONFIG" ]; then section+="-$CONFIG" elif [ -z "$section" ] && [ ! -z "$CONFIG" ]; then section="$CONFIG" fi [ ! -d "$SYSDIR" ] && mkdir --parents --verbose "$SYSDIR" tempfile=$(mktemp -p "/tmp/" tempfile.XXXXXXXXXX) trap "rm -f $tempfile" 0 1 2 3 15 #~ trap "rm -f $tempfile" EXIT SIGHUP SIGINT SIGQUIT SIGTERM [ -z "$tempfile" ] && exit 1 files="$SYSCONF" files+=$'\n' files+=$(run-parts --list "$SYSDIR" ) if [ -n "$files" ]; then while read file; do set +e [ ! -r "$file" ] && continue sed -n '/^\['"$section"'\]$/,/^\[.*\]$/{//!p}' "$file" > "$tempfile" scan_configuration "$tempfile" . "$tempfile" set -e done < <(echo "$files") fi } if [ ! -z $CONFIG ]; then get_configuration else get_configuration pct-support-login fi if [ -z "$USERNAME" ]; then echo 1>&2 $"user not set, try adding --user support01, closing now" exit 1 fi if [ -z "$SERVER" ]; then echo 1>&2 $"server not set, try adding --server powercraft.nl, closing now" exit 1 fi if [ -z "$PORT" ]; then echo 1>&2 $"port not set, try adding --port 9999, closing now" exit 1 fi if [ -z $(which screen) ] || [ -z $(which ssh) ] || [ -z $(whereis -b sshd | cut -d : -f 2) ] then echo 1>&2 $"please install the openssh-client openssh-server screen packages" exit 1 fi if [ -z "$(pidof sshd)" ]; then echo $"trying to start the openssh-server..." sudo invoke-rc.d ssh start fi echo $"trying to initialize ssh tunnel to make remote support possible:" echo $"- enter the provided password from your administrator" echo $"- the password will not show up when you type the characters" echo $"- just type the password and press the [enter] key" ssh $SECURITY -q -f -N -R $PORT:127.0.0.1:22 $USERNAME@$SERVER if [ "$?" -ne 0 ]; then echo 1>&2 $"connection failed, please try again and contact your administrator" exit 1 else echo $"connection successful, please wait for your administrator to login" fi unset active maximum=600 count=0 while [ -z "$active" ] && [ "$count" -lt "$maximum" ] && [ "$SCAN" != "0" ] && [ "$SCAN" != "no" ] do count=$(($count+1)) if [ -z "$(ps aux | grep support | grep SCREEN | grep --invert-match $SERVER)" ]; then sleep 1 && echo -ne $"...scanning for support session, please standby... $count/$maximum \r" else active="yes" fi done if [ "$active" == "yes" ]; then echo $"trying to enter the screen session..." screen -x support echo $"support session ended, closing now..." fi echo $"this window will close after 30 seconds, please standby..." sleep 30s echo $"bye" exit 0