#!/bin/bash function usage { echo -e "Usage: $0 [OPTIONS]" echo "OPTIONS: " echo -e " -f path to file that contains IP addresses of Cisco routers" echo -e " -g path to Expect script" echo -e " -i path to file with public RSA key" echo -e " -u name of unprivileged - level 0 - user" echo -e " -p password of unprivileged - level 0 - user" echo -e " -s enable secret password" echo -e " -x name of privileged - level 15 - user that will be created" echo -e " -z password of privileged -level 15 - user" echo -e " -h display help" } function read_arguments { while getopts "f:g:i:u:p:s:x:z:h" arg; do case "$arg" in f) ipfile="$OPTARG";; g) tclfile="$OPTARG";; i) rsafile="$OPTARG";; u) user0="$OPTARG";; p) pass0="$OPTARG";; s) secret="$OPTARG";; x) user15="$OPTARG";; z) pass15="$OPTARG";; h) usage exit;; esac done } function check_arguments { [ ! -f "$ipfile" ] && echo "Can't find a file with the list of routers' IP addresses, use $0 -h" && exit [ ! -f "$tclfile" ] && echo "Can't find tcl file, use $0 -h" && exit [ ! -f "$rsafile" ] && echo "Can't find file with private RSA key, use $0 -h" && exit [ -z "$user0" ] && echo "Enter name of unprivileged - level 0 - user, use $0 -h" && exit [ -z "$pass0" ] && echo "Enter password for unprivileged -level 0 - user, use $0 -h" && exit [ -z "$secret" ] && echo "Enter enable secret password, use $0 -h" && exit [ -z "$user15" ] && echo "Enter name of privileged - level 15 - user that I'm going to create, use $0 -h" && exit [ -z "$pass15" ] && echo "Enter password for privileged -level 15 - user, use $0 -h" && exit [ ! "$(type -p expect)" ] && echo "Utility 'expect' not found, install it" && exit } function run_expect { fingerprint=$(ssh-keygen -l -f "$rsafile" | cut -d " " -f2) fingerprint=${fingerprint//:/} #Remove char ":" from fingerprint for ip in $(cat "$ipfile"); do $(type -p expect) $tclfile $user0 $ip $pass0 $secret $fingerprint $user15 $pass15 done } curdir="$(dirname $0)" read_arguments $@ check_arguments run_expect