2011年11月10日木曜日

[工具使用] CentOS5 安装 Expect

1. 安装 tcl

$ cd tcl8.x/unix/
$ ./configure --enable-shared --prefix=/usr/tcl8.x
$ make
# make install
# ln -s /usr/local/tcl8.x/bin/tclsh8.x /usr/bin/tclsh

2. 安装 expect

$ ./configure --with-tclinclude=/usr/local/src/tcl8.x/generic --prefix=/usr/local/expect5.x
$ make
$ make install
# /sbin/ldconfig
# ln -s /usr/local/tcl8.x/bin/expect /usr/bin/expect (expect 执行文件在 tcl 目录下)

3. auto-ssh 脚本

3-1. 自动登录后保持交互模式

#!/usr/bin/expect
set timeout 5
spawn ssh go@target_ip_or_hostname
expect "go@target_ip_or_hostname's password:"
sleep 1
send "go_psw\n"
expect "Last login"
send "ls -l\n"
interact

3-2. 自动登录执行命令后退出

#!/bin/sh
echo "Connect Start...\n" >> /tmp/ssh_connect.log
#!/bin/sh
/usr/bin/expect -c "
    spawn ssh uid@target_ip_or_hostname
    expect \"uid@target_ip_or_hostname's password:\"
    sleep 1
    send \"go_psw\n\"
    expect \"Last login\"
    send \"ls -l\n\"
    expect eof
    send \"exit\n\""
echo "Connect End...\n" >> /tmp/ssh_connect.log

0 件のコメント:

コメントを投稿