Guest User

Untitled

a guest
Mar 14th, 2023
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 3.87 KB | None | 0 0
  1. #!/bin/bash
  2. # This script is essentially an automated pmount tool which allows you to
  3. # drop .key files into a directory and automatically mount any/all of them.
  4.  
  5.  
  6.  
  7. #Point this at your directory containing .key files used to unlock any device
  8. keydir=$SECRETSDIR/luks
  9.  
  10. # This assigns every keyfile as a usable variable for later
  11. keyfiles=$(ls -f $keydir | grep key )
  12.  
  13. # Strips the ".key" from all keyfiles
  14. keys=$(ls -f $keydir | grep key | cut -d '.' -f 1)
  15.  
  16. # Gets a list of all UUIDs
  17. devices=$(lsblk -o UUID)
  18.  
  19. # Compares all keyfiles with devices, assigning all eligable ones to an array
  20. identified=(`echo ${keys[@]} ${devices[@]} | tr ' ' '\n' | sort | uniq -d `)
  21.  
  22.  
  23. menu(){
  24. clear
  25. while true; do
  26.     read -p $'>> ' input
  27.     case $input in
  28.         exit|quit|end|terminate|kill|back ) return 0;;
  29.         scan|ls|show ) scan;;
  30.         mount|mnt ) mount;;
  31.         unmount|unmnt|umount|un|remove ) unmount;;
  32.         help ) help;;
  33.         cls|clear|clr ) clear;;
  34.         ? ) help;;
  35.         * ) echo 'Input not recognized, please enter "help" or "?" ';;
  36.     esac
  37. done
  38. }
  39.  
  40. help(){
  41.     echo "help: this menu"
  42.     echo "scan|ls|show: list eligible devices"
  43.     echo "mount|mnt: enter interactive mount menu"
  44.     echo "  0-9 select a mount device   "
  45.     echo "  cls|clr|clear: clears the screen    "
  46.     echo "back|exit|return: return to previous menu and/or exit the program"
  47. }
  48.  
  49.  
  50.  
  51. mount(){
  52.     clear
  53.         echo "Enter index number to begin mount process:"
  54.         for (( i=0; i<${#identified[@]}; i++ ));
  55.         do
  56.         echo $i".)" ${identified[$i]}
  57.         done
  58. #       echo pmount -p "$keydir"/"${identified[$input]}"".key" /dev/disk/by-uuid/"${identified[$input]}" "${identified[$input]}"
  59.     while true; do
  60.     read -p $'[mnt] >> ' input
  61.     case $input in
  62.         [Yy]* ) return 1;;
  63.         [Nn]* ) return 1;;
  64.         exit|back|return|cancel ) return 1;;
  65.         scan|ls|show ) scan;;
  66.         mount|mnt ) echo "You are already in the mount menu";;
  67.         cls|clear|clr ) clear;;
  68.         1|2|3|4|5|6|7|8|9|0 ) mount_prompt $input;;
  69.         help ) help;;
  70.         ? ) help;;
  71.         * ) echo 'Input not recognized, please enter "help" or "?" ';;
  72.     esac
  73. done
  74.  
  75. }
  76.  
  77. mount_prompt(){
  78.     clear
  79.     echo "Are you sure you wish to unlock ${identified[$input]}?"
  80.     echo "This will be mounted at /media/${identified[$input]}"
  81.         while true; do
  82.         read -p $'[y/N] >> ' prompt
  83.         case $prompt in
  84.         [Yy]* ) break;;
  85.         [Nn]* ) return 1;;
  86.         * ) echo 'Please enter yes or no. ';;
  87.     esac
  88.     done
  89.     pmount -d -p $keydir/${identified[$input]}".key" /dev/disk/by-uuid/${identified[$input]} ${identified[$input]}
  90. }
  91.  
  92. scan(){
  93. echo -e "Detected the following eligible devices:"
  94. mediadir=$(ls /media)
  95. currentlymounted=(`echo ${currentkeys[@]} ${mediadir[@]} | tr ' ' '\n' | sort | uniq -d `)
  96. currentkeys=$(ls -f $keydir | grep key | cut -d '.' -f 1)
  97. identified=(`echo ${keys[@]} ${devices[@]} | tr ' ' '\n' | sort | uniq -d `)
  98.  
  99.         for (( i=0; i<${#identified[@]}; i++ ));
  100.         do
  101.         echo $i".)" ${identified[$i]}
  102.         done
  103.         echo ""
  104.  
  105.         echo -e "Detected the following mounted devices:"
  106.         for (( i=0; i<${#currentlymounted[@]}; i++ ));
  107.         do
  108.         echo $i".)" ${currentlymounted[$i]}
  109.         done
  110. i=0
  111. mediadir=0
  112. currentlymounted=0
  113. curentkeys=0
  114. }
  115.  
  116. unmount(){
  117.     clear
  118.         echo "Enter index number to begin unmount process:"
  119.         for (( i=0; i<${#identified[@]}; i++ ));
  120.         do
  121.         echo $i".)" ${identified[$i]}
  122.         done
  123. #       echo pmount -p "$keydir"/"${identified[$input]}"".key" /dev/disk/by-uuid/"${identified[$input]}" "${identified[$input]}"
  124.     while true; do
  125.     read -p $'[mnt] >> ' input
  126.     case $input in
  127.         [Yy]* ) return 1;;
  128.         [Nn]* ) return 1;;
  129.         exit|back|return|cancel ) return 1;;
  130.         scan|ls|show ) scan;;
  131.         mount|mnt ) echo "You are already in the mount menu";;
  132.         cls|clear|clr ) clear;;
  133.         1|2|3|4|5|6|7|8|9|0 ) mount_prompt $input;;
  134.         help ) help;;
  135.         ? ) help;;
  136.         * ) echo 'Input not recognized, please enter "help" or "?" ';;
  137.     esac
  138. done
  139. }
  140.  
  141. unmount_prompt(){
  142.     echo test
  143. }
  144.  
  145.  
  146. menu
Advertisement
Add Comment
Please, Sign In to add comment