1 / 15

利用 shell script 與 expect 製作 PDC 新增帳號腳本

利用 shell script 與 expect 製作 PDC 新增帳號腳本. 指導老師 : 蔡德明 組員 : 徐啟閎 李 平 李承鴻 林啟哲. 前言. 由於專題需要撰寫新增使用者的自動化腳本,但是新增帳號與同步資料庫的過程中有許多需要管理者手動輸入的地方 (EX. 輸入密碼 ) 這些部分如果單純的使用 SHELL SCRIPT 的話能達到的效果有限,於是經由老師建議便使用 EXPECT 來繼續撰寫腳本。. EXPECT.

Download Presentation

利用 shell script 與 expect 製作 PDC 新增帳號腳本

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 利用shell script與expect製作PDC新增帳號腳本 指導老師:蔡德明 組員:徐啟閎 李 平 李承鴻 林啟哲

  2. 前言 由於專題需要撰寫新增使用者的自動化腳本,但是新增帳號與同步資料庫的過程中有許多需要管理者手動輸入的地方(EX.輸入密碼)這些部分如果單純的使用SHELL SCRIPT的話能達到的效果有限,於是經由老師建議便使用EXPECT來繼續撰寫腳本。

  3. EXPECT • 用來進行自動化控制的工具軟體,常應用在需要進行交談的程序中例如sshscpftppasswd等…..,用來自動輸入原本需要操作者手動keyin的部分。

  4. 安裝 • yum install expect

  5. EXPECT基本語法 • spawn- 啟動expect監聽 • expect-尋找關鍵字串 • send-傳遞指定訊息 • expect oef-關閉expect • Interact-結束expect後並轉交控制權

  6. 使用範例 • #!/usr/bin/expect • spawn ssh 127.0.0.1-對ssh啟動expect • expect “password:”-尋找password:字串 • send “123456“ -做出回應 • expect eof-關閉expect

  7. 實做於本專題-分析需求 • 在巨無霸中新增使用者並且刷新nis資料庫使各教室server同步使用者帳號 • 各教室server需個別將該使用者加入smaba中

  8. 實做於本專題-分析 • 以下為此次腳本中需要進行交談的軟體程序 • ssh • scp • sudo • passwd • smbpasswd • /usr/lib64/yp/ypinit -m

  9. 實做於本專題-解決方案 • 針對各程序找出像應解決方案 • ssh製作公私金鑰 • scp製作公私金鑰 • sudo從visudo中取消輸入密碼 • passwd使用echo與|指令 • smbpasswd使用expect • /usr/lib64/yp/ypinit –m 到/var/yp中執行make指令 (make –C /var/yp)

  10. 撰寫腳本-巨無霸 • 巨無霸 • #!/bin/bash • echo "$1" > newuser.txt • scp newuser.txt dic@120.114.140.129:~ • useradd -d /sbam/pdc_home/$1 $1 • echo "${1}" | passwd --stdin $1 • /usr/sbin/edquota -p pdcsample -u $1 • cd /var/yp • make • cd ~ • ssh -f dic@120.114.140.129 "sh smbadd.sh"

  11. 撰寫腳本-教室SERVER • I2511SERVER-smbadd.sh • #!/bin/bash • for i in $(cat newuser.txt) • do • expect useradd.exp $i • done • I2511SERVER-useradd.exp • #!/usr/bin/expect • set username [lindex $argv 0] • spawn sudosmbpasswd -a $username • expect "New SMB password:" • send "$username\r" • expect "Retype new SMB password:" • send "$username\r" • expect eof

  12. 測試

  13. 測試-續 • 新增meeting帳號

  14. 測試-結果

  15. 參考資料 • http://phorum.study-area.org/index.php?topic=25948.0 • http://zh.wikipedia.org/wiki/Expect#.E5.BC.8A • http://linux.chinaitlab.com/c/809655.html • http://linux.vbird.org/(NISsamba sshsudopasswd)

More Related