Guest User

Untitled

a guest
Nov 16th, 2017
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.51 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. max=20
  4. prefix=$1
  5. unique=$$-$(date +%s | base64)
  6. askpass=./sshpass-${unique}
  7.  
  8. run_command() {
  9. DISPLAY=nothing:0 SSH_ASKPASS=${askpass} setsid ssh -oStrictHostKeyChecking=no -t ${superuser}@$1 "$2" 2> /dev/null
  10. }
  11.  
  12. escape() {
  13. sed -e 's/\//\\\//g'
  14. }
  15.  
  16. trim() {
  17. tr -d $'\r' | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//'
  18. }
  19.  
  20.  
  21. user_regex="^(${prefix}[^:]*)"
  22. users=($(grep -Eo "$user_regex" /etc/shadow))
  23.  
  24. if [ ${#users[@]} -gt ${max} ]; then
  25. echo >&2 "Over ${max} users with prefix '${prefix}', please narrow down by using longer prefix."
  26. echo >&2 "Example: $0 inf5063-g"
  27. exit 1
  28. fi
  29.  
  30. read -p "Found ${#users[@]} users starting with prefix '${prefix}', continue [y/N]? " yn
  31. case $yn in
  32. [Yy]*) ;;
  33. *) exit 1;;
  34. esac
  35.  
  36. read -p "Superuser username: " superuser
  37. if [ -z "${superuser}" ]; then
  38. echo >&2 "Username can not be empty"
  39. exit 1
  40. fi
  41.  
  42. read -p "Superuser password: " -s superpass
  43. echo
  44.  
  45. if [ -z "${superuser}" ] || [ -z "${superpass}" ]; then
  46. echo >&2 "Username and password can not be empty"
  47. exit 1
  48. fi
  49.  
  50. echo "echo ${superpass}" > ${askpass}
  51. chmod +x ${askpass}
  52.  
  53. hosts=()
  54. while true; do
  55. read -p "Hostname [empty quits]: " hostname
  56. case ${hostname} in
  57. "") break;;
  58. *) ;;
  59. esac
  60. if ! realhost=$(run_command "${hostname}" "hostname"); then
  61. echo >&2 "Hostname '${hostname}' is unreachable or username+password is wrong"
  62. else
  63. hosts+=" ${hostname}"
  64. if ! mountpoint=$(run_command "${hostname}" "echo ${superpass} | sudo -S grep -E '/mnt/home' /etc/fstab"); then
  65. run_command "${hostname}" "echo ${superpass} | sudo -S echo \"128.39.36.109:/home /mnt/home\tnfs\tdefaults\t0\t0\" >> /etc/fstab"
  66. fi
  67. fi
  68. done
  69.  
  70. if [ ${#hosts[@]} -lt 1 ]; then
  71. echo >&2 "No hosts specified, aborting"
  72. exit 2
  73. fi
  74.  
  75. for host in ${hosts[@]}; do
  76. echo "Applying changes on ${host}..."
  77.  
  78. for user in ${users[@]}; do
  79. user_regex="^${user}:*"
  80. passwd=$(grep -E "${user_regex}" /etc/passwd | sed -e "s,/home/${user},/mnt/home/${user},g" | trim)
  81. shadow=$(grep -E "${user_regex}" /etc/shadow)
  82. group=$(grep -E "${user_regex}" /etc/group)
  83.  
  84. echo "${user}"
  85.  
  86. remote_passwd=`run_command "${host}" "echo ${superpass} | sudo -S grep -E \"${user_regex}\" /etc/passwd 2> /dev/null"`
  87. remote_passwd=$(echo "${remote_passwd}" | trim)
  88. if [ -z "${remote_passwd}" ]; then
  89. run_command "${host}" "echo ${superpass} | sudo -S sh -c 'echo \"${passwd}\" >> /etc/passwd' 2> /dev/null"
  90. else
  91. a=$(echo "${remote_passwd}" | escape)
  92. b=$(echo "${passwd}" | escape)
  93. run_command "${host}" "echo ${superpass} | sudo -S sed -i 's/${a}/${b}/' /etc/passwd 2> /dev/null"
  94. fi
  95.  
  96. remote_shadow=`run_command "${host}" "echo ${superpass} | sudo -S grep -E \"${user_regex}\" /etc/shadow 2> /dev/null"`
  97. remote_shadow=$(echo "${remote_shadow}" | trim)
  98. if [ -z "${remote_shadow}" ]; then
  99. run_command "${host}" "echo ${superpass} | sudo -S sh -c 'echo \"${shadow}\" >> /etc/shadow' 2> /dev/null"
  100. else
  101. a=$(echo "${remote_shadow}" | escape)
  102. b=$(echo "${shadow}" | escape)
  103. run_command "${host}" "echo ${superpass} | sudo -S sed -i 's/${a}/${b}/' /etc/shadow 2> /dev/null"
  104. fi
  105.  
  106. remote_group=`run_command "${host}" "echo ${superpass} | sudo -S grep -E \"${user_regex}\" /etc/group 2> /dev/null"`
  107. remote_group=$(echo "${remote_group}" | trim)
  108. if [ -z "${remote_group}" ]; then
  109. run_command "${host}" "echo ${superpass} | sudo -S sh -c 'echo \"${group}\" >> /etc/group' 2> /dev/null"
  110. else
  111. a=$(echo "${remote_group}" | escape)
  112. b=$(echo "${group}" | escape)
  113. run_command "${host}" "echo ${superpass} | sudo -S sed -i 's/${a}/${b}/' /etc/group 2> /dev/null"
  114. fi
  115. done
  116. done
  117.  
  118. rm -f ${askpass}
Add Comment
Please, Sign In to add comment