expect 安装
一、Tcl安装
(1)下载源码包
wget http://sourceforge.net/projects/tcl/files/Tcl/8.4.19/tcl8.4.19-src.tar.gz/download
下载完成后,在home目录下找到download
(2)解压源码包
tar xfvz tcl8.4.19-src.tar.gz
(3)安装配置
cd tcl8.4.19/unix
./configure --prefix=/usr/tcl --enable-shared
make
sudo make install
注:如果是使用root用户解压,需要在tcl8.4.19目录下执行chmod -R 777 *
(4)安装完成后
进入tcl源代码的根目录,把子目录unix下面的tclUnixPort.h 复制到子目录generic中。
二、expect安装
(1)下载源码包
wget http://sourceforge.net/projects/expect/files/Expect/5.45/expect5.45.tar.gz/download.1
(2)解压源码包
tar xzvf download.1
(3)安装配置
cd expect5.45
./configure --prefix=/usr/expect --with-tcl=/usr/tcl/lib --with-tclinclude=../tcl8.4.19/generic
make
sudo make install
ln -s /usr/tcl/bin/expect /usr/expect/bin/expect
注意:使用的tcl版本不同时,需要把对应的版本依赖换掉
三、expect脚本自动获取root权限
运行下面的脚本,可以自动获取root权限
#!/usr/expect/bin/expect -f
set timeout=5 #设置5秒超时
#下面几行后面不能添加注释,否则会异常
#send "exit\r" - exit命令:会退出root账号,成为普通用户
spawn su - root
expect "密码:"
send "这里是你自己的root密码\r"
send "pwd\r"
#send "exit\r"
interact #使用interact后,脚本将退出到root账号下,可以手动执行root权限的命令
exit 0 #退出脚本
注意:新建文件之后需要增加x权限
sudo chmod +x text.sh