Advertisement
sergio_educacionit

accout_migration.sh

Apr 8th, 2025 (edited)
15
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.44 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. # $ sudo ./account_migration.sh 1005 [email protected]
  4.  
  5. user_input=$1
  6. conn=$2
  7. host=$(echo $conn | cut -d '@' -f2)
  8.  
  9.  
  10.  
  11. # comparando cadena
  12. if [ $UID != "0" ]; then
  13.  
  14. echo "Debe ejeuctar este programa con privilegios de 'root'."
  15. exit 1
  16. fi
  17.  
  18.  
  19.  
  20. if ! ping -c 1 $host > /dev/null 2>&1;then
  21.  
  22. echo "Host inalcanzable, se sale."
  23. exit 1
  24.  
  25. fi
  26.  
  27.  
  28. echo "
  29. Instalando dependencias locales y remotas...
  30. "
  31. apt install rsync -y
  32. ssh $conn "apt install rsync -y"
  33.  
  34.  
  35.  
  36.  
  37. echo "Host disponible, se continua"
  38.  
  39. # campos de usuario
  40. # comando or comando (se ejeucta si el anterorior temrino
  41. # con errores exit status distito de 0)
  42. getent passwd $user_input || { echo No existe el usuario, se sale.; exit 1; }
  43.  
  44.  
  45. passwd_fields=$(getent passwd $1)
  46.  
  47. user_field=$(echo $passwd_fields | cut -d ":" -f1)
  48. uid_field=$(echo $passwd_fields | cut -d ":" -f3)
  49. gid_field=$uid_field
  50. home_field=$(echo $passwd_fields | cut -d ":" -f6)
  51. shell_field=$(echo $passwd_fields | cut -d ":" -f7)
  52.  
  53.  
  54. echo "
  55. Creando usuario '$user_field' en el remoto...
  56. "
  57. ssh $conn "useradd -s $shell_field \
  58. -u $uid_field \
  59. -d $home_field \
  60. -m $user_field"
  61.  
  62.  
  63. # envia el directorio
  64. # rsync /ruta/al/directorio
  65.  
  66. # envia el contenido del directorio
  67. # rsync /ruta/al/directorio/
  68.  
  69. echo "
  70. Migrando directorio de usuario..."
  71. rsync -a $home_field/ ${conn}:$home_field
  72.  
  73. echo "
  74. Terminado.
  75. "
  76.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement