webフォームから受けとったデータを外部のbashに渡し、その受け取ったデータでlinuxユーザのパスワードを変更するという
ものを実現したいのですが、expectを使用する以外に何か手法はございますでしょうか?
(linuxユーザに限らずldapのパスワードやsambaのパスワードなど、パスワード入力が求められるコマンド全般を処理できるようにしたい)
例
passwd <ユーザ>
を実行する際に発生する入力にwebFormからの入力データpasswd1 = 123(新しいパスワード) passwd2=123(確認用パスワード)使ってパスワード変更
expectを利用するとなぜか動作が不定になります
例えばpasswd1とpasswd2が同一なのにパスワードが変更されていない(passwords do not matchとはでない)
また逆にpasswd1とpasswd2の入力が同一でないのにpasswords do not matchと表示がでない
など
よろしくお願いいたします。
以下作ったコードです
Go
1package main 2 3import ( 4 "fmt" 5 "net/http" 6 "os/exec" 7) 8 9func main() { 10 http.HandleFunc("/", handler) 11 http.ListenAndServe(":8080", nil) 12} 13 14func handler(w http.ResponseWriter, r *http.Request) { 15 fmt.Fprint(w, guestbookform) 16 pass1 := r.FormValue("passwd1") 17 pass2 := r.FormValue("passwd2") 18 if len(pass1) != 0 && len(pass2) != 0 { 19 inputunixcommand(pass1, pass2) 20 } 21} 22 23func inputunixcommand(pass1 string, pass2 string) { 24 out, _ := exec.Command("./gopasswd", pass1, pass2).Output() 25 fmt.Printf("%s", out) 26} 27 28const guestbookform = ` 29<html> 30<body> 31change linux account 32<form action="/change" method="post"> 33<label> User name: <input type="text" name="passwd1"> </label> 34<label> Password: <input type="text" name="passwd2"> </label> 35<input type="submit" name="post"> 36</form> 37</body> 38</html>
bash
1#!/bin/bash 2expect -c " 3spawn passwd testuser2 4expect \"Enter new UNIX password\" 5send \"$1\n\" 6expect \"Retype new UNIX password:\" 7send \"$2\n\" 8interact 9"
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。