[Omaha.pm] Net::SSH::Perl and sudo or su

Tony Simons webmaster at tvortex.net
Thu Dec 6 13:35:01 PST 2007


Ok, but it's really long... But... this is what I have.  And it's a
little rough but it works.

#!/bin/bash
author="Tony Simons"
altauthor=""
ver=0
rev=1
mod="2007.10.12"
HOS=0;
ASS=1;
SER=2;
MAK=3;
MOD=4;
RAC=5;
FAC=6;
UNI=7;
SSHCMD="/usr/bin/ssh -t"
SSHEXEC=${SSHCMD}
DEBUG=0
FPCHECK=1
ERRORED=0
ERRCODE=99
#-------------------------------------------------
# Functions
#-------------------------------------------------
function usage {
    echo "Version ${ver}.${rev}, original by ${author} ${altauthor} on
${mod}"
    echo -e "\nUsage: $0 <options>
Options:
\t -h <host> {for ssh purposes}
\t -a <asset number>
\t -s <serial number>
\t -k <make>
\t -m <model>
\t -f <facility>
\t -r <rack#>
\t -u <U's>
\t -p Turn off fingerprinting
NOTE: this help message shows when no options are supplied\n
another NOTE: if your running this outside of the script every value
MUST be in double quotes

Error Codes:
99:    No options were supplied.
2:    Sudo Failed
3:    SinFP not found
4:    SinFP failed
8:    NOT A UNIX BOX

"
}
if [ $# -lt 1 ]; then
    usage
    exit 99
fi
#-------------------------------------------------
# Sudo Checking
#-------------------------------------------------
echo "Checking sudo status (Enter Password if asked)..."
if [ `sudo test; echo $?` -gt 1 ]; then
    echo "This script uses SinFP which requires Root privileges please
run the script again and enter your password."
    exit 2
fi
echo -e "Local Sudo PASSED!\nLooking for sinfp.pl..."
#-------------------------------------------------
# SinFP Checking
#-------------------------------------------------
SINFP=`which sinfp.pl`
if [ ! -f "$SINFP" ]; then
    echo "This script uses SinFP for fingerprinting purposes which helps
locate certain scripts"
    echo -e "********SinFP is installed through cpan*********
# cpan Net::SinFP
If it fails with IOSOMETHING Interface not found...
# perl -MCPAN -e shell
> force install Net::Pcap
Try the cpan line again."
    exit 61
fi
echo "Found $SINFP!"
#-------------------------------------------------
# Variable Assignments
#-------------------------------------------------
while getopts "dpa:f:h:k:m:r:s:u:" optname
do
    case ${optname} in
        a)SNFILE[$ASS]=${OPTARG};;
        f)SNFILE[$FAC]=${OPTARG};;
        h)SNFILE[$HOS]=${OPTARG};;
        k)SNFILE[$MAK]=${OPTARG};;
        m)SNFILE[$MOD]=${OPTARG};;
        r)SNFILE[$RAC]=${OPTARG};;
        s)SNFILE[$SER]=${OPTARG};;
        u)SNFILE[$UNI]=${OPTARG};;
        p)FPCHECK=0;;
        d)DEBUG=1
          echo "DEBUG ON; NOTHING WILL BE SENT"
          ;;
    esac
done
for w in `seq 1 2`; do
    if [ "${SNFILE[${w}]}" == "" ]; then
        ERRORED=1
        ERRCODE=${w}
    fi
done
if [ $ERRORED -eq 1 ]; then
    echo "${SNFILE[${HOS}]} Missing Variables: ${ERRCODE}"
    echo "${SNFILE[${HOS}]} Missing Variables: ${ERRCODE}" >>
SNPushMissingContent-`date +%Y%m%d`
    exit
fi
if [ ${DEBUG} -eq 0 ]; then
STR="ASSET=\\\"${SNFILE[${ASS}]}\\\"
\\\nSERIAL=\\\"${SNFILE[${SER}]}\\\" \\\nMAKE=\\\"${SNFILE[${MAK}]}\\\"
\\\nMODEL=\\\"${SNFILE[${MOD}]}\\\"
\\\nFACILITY=\\\"${SNFILE[${FAC}]}\\\"
\\\nRACK=\\\"${SNFILE[${RAC}]}\\\" \\\nUNITS=\\\"${SNFILE[${UNI}]}\\\""
else
STR="ASSET=${SNFILE[${ASS}]} \nSERIAL=${SNFILE[${SER}]}
\nMAKE=${SNFILE[${MAK}]} \nMODEL=${SNFILE[${MOD}]}
\nFACILITY=${SNFILE[${FAC}]} \nRACK=${SNFILE[${RAC}]}
\nUNITS=${SNFILE[${UNI}]}"
fi
#-------------------------------------------------
# Guessing the OS
#-------------------------------------------------
#if [ ${FPCHECK} -eq 1 ]; then
    echo "Detecting the OS of ${SNFILE[${HOST}]} with SinFP..."
    case `sudo sinfp.pl -a -i${SNFILE[${HOS}]} -p 22 | grep IPv4 | awk
'{print $4}' | head -n1` in
     "Linux:")
         #echo "Has Linux Fingerprint!"
        SUDO=/usr/bin/sudo
        ;;
     "SunOS:")
         #echo "Has Solaris Fingerprint!"
        SUDO=/usr/local/bin/sudo
        ;;
     *)
         echo "No Unix Fingerprint Found...doing one more check!"
        if [ "`sudo sinfp.pl -i${SNFILE[${HOS}]} -p 22 | grep IPv4 | awk
'{print $2}' | head -n1`" == "unknown" ]; then
            SUDO=/usr/bin/sudo
        else
            exit 6
        fi
    ;;
esac
#else
#    echo "Fingerprint Disabled!"
#fi
#-------------------------------------------------
# Making the connections
#-------------------------------------------------
if [ ${DEBUG} -eq 0 ]; then
    $SSHCMD ${SNFILE[${HOS}]} "echo -e $STR > /tmp/${SNFILE[${HOS}]}.sn
;$SUDO mv /tmp/${SNFILE[${HOS}]}.sn /etc/serialnumber; $SUDO chmod 660
/etc/serialnumber"
    RESULT=$?
    if [ ${RESULT} -gt 0 ]; then
        exit 22
    fi
else
    echo "This is what would be sent to ${SNFILE[${HOS}]}!"
    echo -e $STR
fi
exit 0



More information about the Omaha-pm mailing list