编程小贴士

给你的编程提供小点子


Linux如何批量修改密码

最近管理的一批机器,有个需求是要统一修改一个帐号的用户名密码,比如将qa帐号的密码改为1234,后来还为了脚本化,很方便的执行,还使用了非交互式地修改用户的密码。简单记录一下吧。

第一种方式: 交互式配置本地用户的密码:passwd 命令

[root@host_221-81 ~]# passwd qa
Changing password for user qa.
New password:
BAD PASSWORD: it is too short
BAD PASSWORD: is too simple
Retype new password:
passwd: all authentication tokens updated successfully.

 

第二种方式:非交互式修改本地用户的密码:chpasswd

# chpasswd命令使用起来很简洁[root@host_221-81 ~]# echo “qa:1234″ | chpasswd

# 使用passwd命令,也可以实现非交互式修改密码[root@host_221-81 ~]# echo “1234” | passwd –stdin “qa”
Changing password for user qa.
passwd: all authentication tokens updated successfully.

第三种方式:使用expect来处理交互式输入,从而实现非交互式的密码修改。
!/bin/sh# \exec expect -f”$0″”$@”if{$argc!= 2}{puts “Usage: $argv0 “exit1}set password [lindex $argv1]
spawn passwd[lindex $argv0]sleep1
expect “assword:”
send “$password\r”
expect “assword:”
send “$password\r”
expect eof

注 意:脚本的第二行,这种写法可能比较陌生,这是在TCL语言中的语法,The backslash is recognized as part of a comment to sh, but in Tcl the backslash continues the comment into the next line which keeps the exec command from executing again.

该脚本的执行结果为:

[root@smilejay ~]# ./change-pwd-expect.sh qa 1234
spawn passwd qa
Changing password for user qa.
New password:
BAD PASSWORD: it is too short
BAD PASSWORD: is too simple
Retype new password:
passwd: all authentication tokens updated successfully.

84 Responses to “ Linux如何批量修改密码 ”

  1. see pron说道:

    kRUi5B Merely wanna say that this is very helpful , Thanks for taking your time to write this.

  2. animal coloring说道:

    You made some nice points there. I did a search on the subject and found most persons will approve with your blog.

  3. Lung Detox说道:

    Perfectly pent content, Really enjoyed reading through.

  4. visit说道:

    This blog was how do I say it? Relevant!! Finally I have found something which helped me. Kudos!

  5. omlet arcade说道:

    Very interesting subject , thanks for putting up. It is much easier to try one as hand at many things than to concentrate one as powers on one thing. by Quintilian.

  6. W88说道:

    very trivial column, i certainly love this website, be on it

  7. Thanks-a-mundo for the blog article.Much thanks again. Keep writing.

  8. to learn more说道:

    very nice publish, i actually love this web site, carry on it

  9. Say, you have got a nice blog post.Really looking forward to reading through much more. Cool.

  10. moment this time I am visiting this web site and reading very informative posts here.

  11. status说道:

    I wanted to thank you for this great read!! I definitely enjoying every little bit of it I have you bookmarked to check out new stuff you post.

  12. What are some good sites and blogs for affordable fashion for adults?

  13. What as up, all is going fine here and ofcourse every

  14. Studying this write-up the donate of your time

  15. Nwokolo说道:

    So happy to get found this article.. Is not it awesome when you uncover an excellent article? Treasure the entry you made available.. Excellent views you ave got here..

  16. cay thong noel说道:

    Thanks for sharing, this is a fantastic post. Great.

  17. klm voucher说道:

    You have brought up a very great points , thankyou for the post.

  18. I value the article.Really thank you! Fantastic.

  19. There is noticeably a bundle to know about this. I think you made certain good points in features also.

  20. Landfill说道:

    You have brought up a very fantastic points , regards for the post.

  21. mp3 sounds说道:

    your useful info. Thanks for the post. I will certainly return.

  22. see more说道:

    Utterly indited content, Really enjoyed looking through.

  23. securities说道:

    Wow, amazing blog layout! How long have you been blogging for? you made blogging look easy. The overall look of your website is great, as well as the content!

  24. Top Sales Tools说道:

    Thanks for sharing this very good post. Very inspiring! (as always, btw)

  25. you possess an incredible weblog right here! would you like to make some invite posts in my weblog?

  26. Very good information. Lucky me I found your website by accident (stumbleupon). I ave bookmarked it for later!

  27. Muchos Gracias for your blog article.Really looking forward to read more. Really Cool.

  28. Thanks for taking the time to talk about this, I feel fervently about this and I take pleasure in learning about this topic. Please, as you gain

  29. This is a set of phrases, not an essay. you are incompetent

  30. This is a topic which is near to my heart Cheers! Where are your contact details though?

  31. Elena Matei说道:

    Nice blog right here! Also your site lots up very fast! What host are you the usage of? Can I am getting your affiliate hyperlink for your host? I want my web site loaded up as quickly as yours lol

  32. David说道:

    If some one needs expert view about running a blog afterward i recommend him/her to go to see this weblog, Keep up the pleasant work.

  33. women说道:

    Wonderful issues altogether, you simply won a new reader. What would you suggest in regards to your publish that you made a few days ago? Any certain?

  34. check this out说道:

    If a man does not make new acquaintances as he advances through life, he will soon find himself alone. A man should keep his friendships in constant repair.

  35. Wow, fantastic blog format! How long have you ever been blogging for? you made running a blog look easy. The entire glance of your website is magnificent, let alone the content material!

  36. Investment tips说道:

    Thanks a lot for the post.Thanks Again. Really Cool.

  37. Thanks, I have been hunting for details about this subject for ages and yours is the best I ave found so far.

  38. Blogger SEO说道:

    Right now it seems like Movable Type is the best blogging platform available right now. (from what I ave read) Is that what you are using on your blog?

  39. Some truly nice and utilitarian info on this web site, as well I conceive the style and design contains good features.

  40. Crochet bolero说道:

    Major thankies for the article.Really looking forward to read more. Want more.

  41. I really relate to that post. Thanks for the info.

  42. Kangerlussuaq说道:

    There is clearly a bundle to know about this. I consider you made some good points in features also.

  43. local说道:

    You are my aspiration , I possess few blogs and occasionally run out from to brand.

  44. to learn more说道:

    Wow, great article post.Much thanks again. Awesome.

  45. Waec expo说道:

    Now I am ready to do my breakfast, once having my breakfast coming yet again to read other news. Look at my blog post; billigste ipad

  46. This is very interesting, You are a very skilled blogger. I have joined your feed and look forward to seeking more of your fantastic post. Also, I ave shared your site in my social networks!

  47. This blog was how do I say it? Relevant!! Finally I have found something that helped me. Appreciate it!

  48. Perfectly pent content , thanks for information.

  49. Open heaven说道:

    Thanks so much for the blog post.Really looking forward to read more. Will read on

  50. This is one awesome blog.Really thank you!

  51. Really enjoyed this article.Thanks Again. Cool.

  52. internet explorer crashing on launch How to build a wordpress site while domain is on redirect.?

  53. Thanks-a-mundo for the post.Really thank you! Great.

  54. IaаАа’б‚Т€ТšаЂаŒаАа’б‚Т€ТžаБТžm glad to become a visitor in this pure web site, regards for this rare information!

  55. I really love I really love the way you discuss this kind of topic.~; a.~

  56. Thanks so much for the blog post.Really thank you! Will read on

  57. Very good article post.Thanks Again. Keep writing.

  58. Thank you for what you ave. This really is the very best article IaаАа’б‚Т€ТšаЂаŒаАа’б‚Т€ТžаБТžve study

  59. find more here说道:

    There is evidently a bunch to realize about this. I believe you made certain good points in features also.

  60. Only wanna say that this is very useful, Thanks for taking your time to write this.

  61. if the buffalo in my head could speak german i would not know a god damm thing. What i do know is that the language of art is out of this world.

  62. for details说道:

    You should take part in a contest for one of the most useful websites on the net. I am going to highly recommend this blog!

  63. dclm devotionals说道:

    I think other website proprietors should take this web site as an model, very clean and excellent user genial style and design, as well as the content. You are an expert in this topic!

  64. know more说道:

    This very blog is obviously cool as well as factual. I have chosen a lot of interesting things out of this amazing blog. I ad love to return over and over again. Thanks a lot!

  65. Wow, great post.Much thanks again. Fantastic.

  66. Some genuinely good posts on this web site , thankyou for contribution.

  67. Way cool! Some very valid points! I appreciate you writing this write-up and the rest of the site is really good.

  68. This site was how do I say it? Relevant!! Finally I have found something which helped me. Cheers!

  69. Thank you for your article.Much thanks again. Really Great.

  70. Very nice article. I certainly appreciate this website. Stick with it!

  71. Terrific paintings! That is the kind of info that should be shared across the web. Disgrace on the search engines for not positioning this put up higher! Come on over and visit my site. Thank you =)

  72. In the great I always visit your blog everyday to read new topics.,:~-~

  73. more info说道:

    I went over this internet site and I conceive you have a lot of good information, saved to bookmarks (:.

  74. for more details说道:

    Useful info. Fortunate me I found your website by chance, and I am surprised why this twist of fate did not happened earlier! I bookmarked it.

  75. Lovely just what I was looking for.Thanks to the author for taking his clock time on this one.

  76. It as not that I want to copy your web site, but I really like the pattern. Could you let me know which style are you using? Or was it custom made?

  77. Thank you for your blog article.Really looking forward to read more. Cool.

  78. Wow, awesome blog layout! How long have you been blogging for? you made blogging look easy. The overall look of your site is fantastic, as well as the content!

  79. clickhere说道:

    I think this is a real great blog post.Thanks Again. Fantastic. ventolin

  80. Adaline说道:

    Precisely what I was searching for, thanks for posting.

  81. ga bo xoi chien说道:

    Im obliged for the blog article.Really thank you! Cool.

  82. copper fit knee说道:

    Merely a smiling visitant here to share the love (:, btw outstanding design. Individuals may form communities, but it is institutions alone that can create a nation. by Benjamin Disraeli.

  83. go there说道:

    You can certainly see your enthusiasm within the work you write. The sector hopes for even more passionate writers like you who aren at afraid to say how they believe. All the time follow your heart.

  84. too substantially vitamin-a may also lead to osteoporosis but aging could be the quantity cause of it`

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>