Wednesday, November 19, 2014

Change password shell script

  1. #!/usr/local/bin/expect -f
  2. # Password change shell script, tested on Linux and FreeBSD
  3. # ----------------------------------
  4. # It need expect tool. If you are using Linux use following command
  5. # to install expect
  6. # apt-get install expect
  7. # FreeBSD user can use ports or following command:
  8. # pkg_add -r -v expect
  9. # ----------------------------------
  10. # If you are using linux change first line
  11. # From:
  12. #!/usr/local/bin/expect -f
  13. # To:
  14. #!/usr/bin/expect -f
  15. # -----------------------------------------------
  16. # Copyright (c) 2006 nixCraft project
  17. # This script is licensed under GNU GPL version 2.0 or above
  18. # -------------------------------------------------------------------------
  19. # This script is part of nixCraft shell script collection (NSSC)
  20. # Visit http://bash.cyberciti.biz/ for more information.
  21. # -------------------------------------------------------------------------
  22. # display usage
  23. if {$argc!=2} {
  24. send_user "usage: $argv0 username password \n"
  25. exit
  26. }
  27. # script must be run by root user
  28. set whoami [exec id -u]
  29. if {$whoami!=0} {
  30. send_user "You must be a root user to run this script\n"
  31. exit
  32. }
  33. #
  34. set timeout -1
  35. match_max 100000
  36. # stopre password
  37. set password [lindex $argv 1]
  38. # username
  39. set user [lindex $argv 0]
  40. # opem shell
  41. spawn $env(SHELL)
  42. # send passwd command
  43. send -- "passwd $user\r"
  44. expect "assword:"
  45. send "$password\r"
  46. expect "assword:"
  47. send "$password\r"
  48. send "\r"
  49. expect eof

0 comments: