下面的脚本演示了在Ubuntu上安装expect,写一个切换用户的expect脚本,并运行脚本看到效果的过程。
root@guserver:~# apt-get install expect godu@guserver:~\( cat su.exp #!/usr/bin/expect set timeout 20 set user [lindex $argv 0] set password [lindex $argv 1] spawn su $user expect "Password:" send "\)password\r"; interact godu@guserver:~\( ./su.exp chad mypwd spawn su chad Password: chad@guserver:/home/godu\)
参考:6 Expect Script Examples to Expect the Unexpected (With Hello World)