Monday, April 25, 2011

.:: Expect (Komunikasi program dengan program) ::.

#!/usr/bin/expect -f
# Password change shell script, tested on Linux and FreeBSD
# ----------------------------------
# It need expect tool. If you are using Linux use following command
# to install expect
# apt-get install expect
# FreeBSD user can use ports or following command:
# pkg_add -r -v expect
# ----------------------------------
# If you are using linux change first line
# From:
#!/usr/local/bin/expect -f
# To:
#!/usr/bin/expect -f
# -------------------------------------------------------------------------
# display usage

# script must be run by root user
set whoami [exec id -u]
if {$whoami!=0} {
send_user "You must be a root user to run this script\n"
exit
}
#
set timeout -1
match_max 100000
# stopre password
set password [lindex $argv 1]
# username
set user [lindex $argv 0]
# opem shell
spawn $env(SHELL)
# send passwd command
send -- "passwd root\r"
expect "assword:"
send "rahasia\r" ------> Misal : "rahasia" adalah password baru anda
expect "assword:"
send "rahasia\r" ------> Untuk pengulangan input karakter/ retype password
send "\r"
expect eof
Yakz...pada initinya kita ingin merubah password root dengan program. Program ini berguna bila di suatu sistem ada banyak admin yang ingin merubah password root sesuai kehendaknya, tapi ketika dijalankan script ini, password root akan berubah kembali ke asal.

NB : Bila program expect belum ada, install dahulu. Kalau menggunakan Yum bisa ketikkan #yum install expect.

Lumayan, ilmu baru....

0 comments:

Post a Comment