Guest User

Untitled

a guest
Mar 12th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.13 KB | None | 0 0
  1. #!/bin/bash
  2. usage="Usage: $(basename "$0") [-h|-u|-i|-p|-f] -- Copys your identity key to a list of remote hosts
  3. where:
  4. -h Show this help text
  5. -u Username of ssh user
  6. -i Location of identity file (Default: /home/$USER/.ssh/id_rsa.pub)
  7. -p Password (not secure)
  8. -f Location of hosts file (Default: ./inventory)"
  9.  
  10. # Default location for files
  11. CURR_DIR="$(cd "$(dirname "$0")" && pwd)"
  12. HOSTS_FILE=${CURR_DIR}/inventory
  13. IDENT_FILE=/home/$USER/.ssh/id_rsa.pub
  14.  
  15. # required_flag=false
  16.  
  17. while getopts 'hu:i:p:f:' option; do
  18. case $option in
  19. # Help
  20. h) echo "$usage"; exit;;
  21. # Hosts file
  22. f) HOSTS_FILE=$OPTARG;;
  23. # Password
  24. p) PASSWORD=$OPTARG;;
  25. # Indentity file
  26. i) IDENT_FILE=$OPTARG; echo "$IDENT_FILE";;
  27. # Username
  28. u) USERNAME=$OPTARG;;
  29. # Missing args
  30. :) printf "Option -%sn requires an argument." "-$OPTARG" >&2; echo "$usage" >&2; exit 1;;
  31. # Illegal option
  32. ?) printf "Illegal option: -%sn" "$OPTARG" >&2; echo "$usage" >&2; exit 1;;
  33. esac
  34. done
  35.  
  36. shift "$((OPTIND-1))"
  37. # Decrements the argument pointer so it points to next argument.
  38. # $1 now references the first non-option item supplied on the command-line
  39. #+ if one exists.
  40.  
  41. # if ! $required_flag && [[ -d $1 ]]
  42. # then
  43. # echo "You must specify a hosts file. -h for more help." >&2
  44. # exit 1
  45. # fi
  46.  
  47. while IFS= read -r line;
  48. do
  49. echo "Current IP: " "$IP"
  50. echo "Current Username: " "$USERNAME"
  51. echo "Current Identity: " "$IDENT_FILE"
  52. echo "Current Hosts: " "$HOSTS_FILE"
  53. echo "Current Password: " "$PASSWORD"
  54. sshpass -p "$PASSWORD" ssh-copy-id -i "$IDENT_FILE" "$USERNAME"@"$IP"
  55. done < $HOSTS_FILE
  56.  
  57. $ ./autocopyid -u user -p password
  58. Current IP:
  59. Current Username: user
  60. Current Identity: /home/user/.ssh/id_rsa.pub
  61. Current Hosts: /home/user/inventory
  62. Current Password: password
  63. /usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/home/user/.ssh/id_rsa.pub"
  64. /usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
  65.  
  66. /usr/bin/ssh-copy-id: ERROR: ssh: Could not resolve hostname : Name or service not known
  67.  
  68. while IFS= read -r IP
  69.  
  70. while IFS= read -r line
Add Comment
Please, Sign In to add comment