Kaarsemaker.net


reset_passwords
2.18 KB

78 lines

Download

pygments-get-style-defs
resolve.py

Preview

 1 #!/bin/bash
 2 #
 3 # Find linux installs and reset passwords
 4 # (c) 2008 Dennis Kaarsemaker
 5 #
 6 # Bugs:
 7 #  - Doesn't work if /usr is on a separate filesystem
 8 #  - Doesn't work for non-lsb distros
 9 
10 # Warning, disclaimer
11 if [ "$1" != "--nowarn" ]; then
12     echo -e "\033[1;31mThis tool will let you reset the passwords of any accounts\033[0m"
13     echo -e "\033[1;31mit finds on installed linux systems. Use with care!\033[0m"
14     echo -n "Press enter to continue, or CTRL-C to abort "
15     read discard
 1 #!/bin/bash
 2 #
 3 # Find linux installs and reset passwords
 4 # (c) 2008 Dennis Kaarsemaker
 5 #
 6 # Bugs:
 7 #  - Doesn't work if /usr is on a separate filesystem
 8 #  - Doesn't work for non-lsb distros
 9 
10 # Warning, disclaimer
11 if [ "$1" != "--nowarn" ]; then
12     echo -e "\033[1;31mThis tool will let you reset the passwords of any accounts\033[0m"
13     echo -e "\033[1;31mit finds on installed linux systems. Use with care!\033[0m"
14     echo -n "Press enter to continue, or CTRL-C to abort "
15     read discard
16 fi
17 
18 if [ $UID -ne 0 ]; then
19     echo -e "\033[0;37mYou are $USER, not root. Attempting to run sudo\033[0m"
20     exec sudo bash "$0" --nowarn
21 fi
22 
23 # Warn if things are mounted on /mnt
24 mount | grep -q '/mnt' && {
25     echo "A partition has been mounted under /mnt, aborting"
26     echo "To unmount these partitions, run the following commands:"
27     awk '/mnt/{ print "sudo umount " $2}' < /proc/mounts
28     exit;
29 }
30 
31 # Find partitions
32 partitions=$(fdisk -l | sed -n -e 's@^\(/dev/[[:alnum:]]\+\).*@\1@p')
33 
34 # Iterate over partitions
35 for part in $partitions; do
36     echo -e "\033[0;37mTrying partition $part\033[0m"
37 
38     # Mount
39     mount $part /mnt >/dev/null 2>&1 || {
40         echo -e "\033[0;37mCouldn't mount $part\033[0m"
41         continue
42     }
43 
44     # Detect linux
45     if [ ! -e /mnt/etc/shadow ] || [ ! -e /mnt/usr/bin/lsb_release ]; then
46         echo "$part doesn't seem to be a linux rootpartition"
47         umount /mnt
48         continue
49     fi
50     echo -e "\033[4;30mFound the following distribution on $part\033[0m:"
51     chroot /mnt /usr/bin/lsb_release -i -r -c -d
52     echo -ne "\033[4;30mReset passwords in this system\033[0m? [y/n] "
53     read cont
54     case $cont in
55         [yY])
56             ;;
57         *)
58             echo -e "\033[0;37mSkipping $part\033[0m"
59             umount /mnt
60             continue
61     esac
62 
63     # Detect users with usable passwords
64     users=$(chroot /mnt /usr/bin/passwd -S -a | sed -n -e 's/\([^[:space:]]\+\) P.*/\1/p')
65 
66     # Iterate over them
67     for user in $users; do
68         echo -ne "Reset the password for \033[4;30m$user\033[0m? [y/n] "
69         read input
70         case $input in
71             [yY])
72                 chroot /mnt /usr/bin/passwd $user
73                 ;;
74         esac
75     done
76 
77     umount /mnt
78 done

Show all