Efface les comptes presents sur des consoles Itanium sauf "Admin" ou "Administrator"

From Tuxunix
Jump to: navigation, search

Objet

  • 2 scripts, un en bash (erazeCptIta.sh), et un en expect (comCheck-Ita.exp) :
  • 1 Fichier : "lstCptDeleteWhithNum.txt"

erazeCptIta.sh

#!/bin/sh
 
#
#@Name          erazeCptIta.sh
#@Fonction      Efface les comptes presents sur les consoles Itanium sauf Admin ou Administrator
#@Depends       AdmList.txt, comCheck-Ita.exp, ilomp.csv
#@Version       0.1
#
 
function initVar() {
 
expectScript="comCheck-Ita.exp"
fileListItaPhy="AdmList.txt" # fichier contenant la liste des consoles (nom DNS)
outFile="erazeCptIta.txt"
extractCptMdpFile="ilomp.csv" # fichier contenant la liste des mdp par défaut pour les consoles
fileListNumCptToDel="lstCptDeleteWhithNum.txt" # fichier contenant la liste des comptes à supprimer avec leurs num
 
 
[ ! -e ${fileListItaPhy} ] || [ ! -e ${expectScript} ] || [ ! -e ${extractCptMdpFile} ] || [ ! -e ${fileListNumCptToDel} ] && echo "no depends found !!"
[ ! -e ${outFile} ] && >${outFile}
 
} #initVar
 
function extractCpt() {
 
while read line; do
        #found user+pwd in file extractCptMdpFile
        [ $(grep ${line} ${extractCptMdpFile}) ] && {
        for i in $(cat ${extractCptMdpFile}); do
              cons=$(echo ${i} | cut -d"," -f1)
              if [ ${cons} == ${line} ]; then
                #extract user+mdp Admin
                user=$(echo ${i} | cut -d"," -f2)
                mdp=$(echo ${i%;} | cut -d"," -f3)
 
                #recup num user to delete
                for j in $(cat ${fileListNumCptToDel}); do
                     hostCons=$(echo ${j} | cut -d"," -f1)
                     if [ ${hostCons} == ${line} ]; then
                        numUser=$(echo ${j} | cut -d"," -f2)
                        userDel=$(echo ${j} | cut -d"," -f3)
                        #launch eraze cpt
                        echo "########## Host : ${line} UserAdmin : ${user} AdminPwd : ${mdp} - UserDel : ${userDel} NumCpttodel : ${numUser} ##########" >> ${outFile}
                        /usr/bin/expect ${expectScript} ${line} ${user} ${mdp} ${numUser} >> ${outFile}
                        [ "$?" != "0" ] && echo "Del user ${userDel} NumCpttodel : ${numUser} failed !!"  >> ${outFile} || echo "Del user ${userDel}, NumCpttodel : ${numUser} [Done]" >> ${outFile}
                        sleep 2
                     else
                        hostCons="null"
                        continue
                     fi
                done
                cons="null"
              else
                cons="null"
                        continue
                fi
        done
        } || {
              #Host not found
              echo "########## Host : ${line} - UNKNOW !!! ##########" >> ${outFile}
        }
done < ${fileListItaPhy}
 
} #extractCpt
 
 
function help() {
 
 echo -e ""
 echo -e "This programm extract user list for many Itanium console"
 echo -e "Depends necessary : AdmList.txt, comCheck-Ita.exp, ilomp.csv"
 echo -e "Usage: ${0##*/} [ -s|-h ] "
 echo -e "-s --start"
 echo -e "-h --help : Usage run"
 echo -e ""
 
} #help
 
 
#
#Main
#
 
[ ${#} == 0 ] && {
        echo "No arguments !"
        echo "Please, Usage: ${0##*/} --help"
        exit 1;
        }
 
while getopts ":sh-:" opt; do
        case ${opt} in
                s|start)
                        initVar
                extractCpt
                ;;
                h|help)
                        help
                ;;
                -) case ${OPTARG} in
                        help)
                                help
                        ;;
                        start)
                                initVar
                     extractCpt
                        ;;
                        *)
                                echo "Option illegale ${opt}, please use -h or --help" && exit 1
                        ;;
                   esac ;;
                *)
                        echo "Option illegale ${opt}, please use -h or --help" && exit 1
                ;;
        esac
done
  • Ex fichier ilomp.csv :
srv001-cons,Admin,password1;
srv002-cons,Admin,password2;
...

comCheck-Ita.exp

#!/usr/bin/expect
# debugging: 0=no 1=yes
#exp_internal 1
# spawn process output: 0=no 1=yes
#log_user 1
 
 
# input arguments
set host [lindex $argv 0]
set user [lindex $argv 1]
set passwd [lindex $argv 2]
set numUserToDel [lindex $argv 3]
 
# check for arguments
if {[llength $argv] != 4 } {
        puts "\n Usage: scriptname host user password \n"
        puts "\n Example: concheck-hpux.exp g7u1111 g7u1111 xxxxxxx xxxxx \n"
        puts "Note: the script works only if console allows SSH"
        exit 1
}
 
spawn ssh -o StrictHostKeyChecking=no ${user}@${host}
expect "password:"
send "${passwd}\r"
 
expect "(.*)MP> "
send "cm\r"
 
expect "(.*)CM> "
send "uc\r"
 
expect "(.*)Quit:"
send "D\r"
 
expect "(.*)Quit:"
send "${numUserToDel}\r"
 
expect "(.*)Y/\[N\]):"
send "Y\r"
 
expect "(.*)Quit:"
send "Q\r"
 
expect "(.*)CM>"
send "exit\r"
 
expect "(.*)MP>"
send "X\r"
 
exit 0

lstCptDeleteWhithNum.txt

  • Ce fichier est récupéré grace au script expect suivant : [1], puis parsé via le script parse_cptOthers.py
#> cat lstCptDeleteWhithNum.txt
srv001-cons,3,toto
srv002-cons,4,hpsi_ad
srv003-cons,3,titi
srv004-cons,3,tata
...

parse_cptOthers.py

import re
re_host = re.compile(".*Host : (\w+-cons).*")
#Commence a partir du compte num 2
re_user = re.compile(".*([2-9])+ - (\w+).*")
host = "no"
last_host = "no"
accounts = []
 
#print('"address","environment","os","username","LCK"')
f=open("erazeCptIta.txt", "r")
for l in f:
        m = re_host.match(l)
        if m != None:
              last_host = host
              host = m.group(1)
              for a in accounts:
                print ("%s,%s" % (last_host, a))
              accounts = []
        m = re_user.match(l)
        if m!= None:
              if m.group(2) != 'Admin':
                user = m.group(1,2)
                accounts.append(user)