Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- max=20
- prefix=$1
- unique=$$-$(date +%s | base64)
- askpass=./sshpass-${unique}
- run_command() {
- DISPLAY=nothing:0 SSH_ASKPASS=${askpass} setsid ssh -oStrictHostKeyChecking=no -t ${superuser}@$1 "$2" 2> /dev/null
- }
- escape() {
- sed -e 's/\//\\\//g'
- }
- trim() {
- tr -d $'\r' | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//'
- }
- user_regex="^(${prefix}[^:]*)"
- users=($(grep -Eo "$user_regex" /etc/shadow))
- if [ ${#users[@]} -gt ${max} ]; then
- echo >&2 "Over ${max} users with prefix '${prefix}', please narrow down by using longer prefix."
- echo >&2 "Example: $0 inf5063-g"
- exit 1
- fi
- read -p "Found ${#users[@]} users starting with prefix '${prefix}', continue [y/N]? " yn
- case $yn in
- [Yy]*) ;;
- *) exit 1;;
- esac
- read -p "Superuser username: " superuser
- if [ -z "${superuser}" ]; then
- echo >&2 "Username can not be empty"
- exit 1
- fi
- read -p "Superuser password: " -s superpass
- echo
- if [ -z "${superuser}" ] || [ -z "${superpass}" ]; then
- echo >&2 "Username and password can not be empty"
- exit 1
- fi
- echo "echo ${superpass}" > ${askpass}
- chmod +x ${askpass}
- hosts=()
- while true; do
- read -p "Hostname [empty quits]: " hostname
- case ${hostname} in
- "") break;;
- *) ;;
- esac
- if ! realhost=$(run_command "${hostname}" "hostname"); then
- echo >&2 "Hostname '${hostname}' is unreachable or username+password is wrong"
- else
- hosts+=" ${hostname}"
- if ! mountpoint=$(run_command "${hostname}" "echo ${superpass} | sudo -S grep -E '/mnt/home' /etc/fstab"); then
- run_command "${hostname}" "echo ${superpass} | sudo -S echo \"128.39.36.109:/home /mnt/home\tnfs\tdefaults\t0\t0\" >> /etc/fstab"
- fi
- fi
- done
- if [ ${#hosts[@]} -lt 1 ]; then
- echo >&2 "No hosts specified, aborting"
- exit 2
- fi
- for host in ${hosts[@]}; do
- echo "Applying changes on ${host}..."
- for user in ${users[@]}; do
- user_regex="^${user}:*"
- passwd=$(grep -E "${user_regex}" /etc/passwd | sed -e "s,/home/${user},/mnt/home/${user},g" | trim)
- shadow=$(grep -E "${user_regex}" /etc/shadow)
- group=$(grep -E "${user_regex}" /etc/group)
- echo "${user}"
- remote_passwd=`run_command "${host}" "echo ${superpass} | sudo -S grep -E \"${user_regex}\" /etc/passwd 2> /dev/null"`
- remote_passwd=$(echo "${remote_passwd}" | trim)
- if [ -z "${remote_passwd}" ]; then
- run_command "${host}" "echo ${superpass} | sudo -S sh -c 'echo \"${passwd}\" >> /etc/passwd' 2> /dev/null"
- else
- a=$(echo "${remote_passwd}" | escape)
- b=$(echo "${passwd}" | escape)
- run_command "${host}" "echo ${superpass} | sudo -S sed -i 's/${a}/${b}/' /etc/passwd 2> /dev/null"
- fi
- remote_shadow=`run_command "${host}" "echo ${superpass} | sudo -S grep -E \"${user_regex}\" /etc/shadow 2> /dev/null"`
- remote_shadow=$(echo "${remote_shadow}" | trim)
- if [ -z "${remote_shadow}" ]; then
- run_command "${host}" "echo ${superpass} | sudo -S sh -c 'echo \"${shadow}\" >> /etc/shadow' 2> /dev/null"
- else
- a=$(echo "${remote_shadow}" | escape)
- b=$(echo "${shadow}" | escape)
- run_command "${host}" "echo ${superpass} | sudo -S sed -i 's/${a}/${b}/' /etc/shadow 2> /dev/null"
- fi
- remote_group=`run_command "${host}" "echo ${superpass} | sudo -S grep -E \"${user_regex}\" /etc/group 2> /dev/null"`
- remote_group=$(echo "${remote_group}" | trim)
- if [ -z "${remote_group}" ]; then
- run_command "${host}" "echo ${superpass} | sudo -S sh -c 'echo \"${group}\" >> /etc/group' 2> /dev/null"
- else
- a=$(echo "${remote_group}" | escape)
- b=$(echo "${group}" | escape)
- run_command "${host}" "echo ${superpass} | sudo -S sed -i 's/${a}/${b}/' /etc/group 2> /dev/null"
- fi
- done
- done
- rm -f ${askpass}
Add Comment
Please, Sign In to add comment