sergio_educacionit

migrator_users.sh

Dec 12th, 2025 (edited)
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.01 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. remote="192.168.0.190"
  4. log="process.log"
  5.  
  6. file=/etc/passwd
  7.  
  8. users=()
  9.  
  10. # mientas que haya saltos de linea
  11. # se almacena la linea en la variable 'linea'
  12. while IFS= read -r linea; do
  13.  
  14.  
  15. gid=$(echo $linea | cut -d ":" -f 3) # id de usuario
  16. user=$(echo $linea | cut -d ":" -f 1) # id de usuario
  17.  
  18. if [ $gid -eq 0 ];then
  19.  
  20. continue
  21.  
  22. elif [ $gid -le 999 ] && [ $gid -ge 1 ] ;then
  23.  
  24. continue
  25.  
  26. elif [ $gid -eq 65534 ]; then
  27.  
  28. continue
  29. else
  30.  
  31. users+=("$user")
  32.  
  33. fi
  34.  
  35.  
  36.  
  37. done < "$file"
  38.  
  39.  
  40. echo ${users[@]}
  41.  
  42.  
  43.  
  44. for u in ${users[@]};do
  45.  
  46.  
  47. home=$(getent passwd $u | cut -d ":" -f 6)
  48. # envia el comando por ssh
  49. echo creando usuario $u >> $log
  50.  
  51. exit_status=$(ssh root@$remote "id noexiste >> /dev/null 2>&1; echo \$?")
  52. echo $exit_status
  53.  
  54.  
  55. [ $exit_status -ne 1 ] || { echo error, se interrumpe ; break; }
  56.  
  57.  
  58. ssh root@$remote "useradd -s /bin/bash -d $home -m $u" >> $log 2>&1
  59.  
  60. rsync -a -v -e "ssh -i /root/.ssh/id_rsa" ${home}/ root@$remote:$home
  61.  
  62. done
  63.  
Advertisement
Add Comment
Please, Sign In to add comment