实例一:
文件名:openssh1
#!/usr/bin/expect -f
# SSH自动登录脚本
# 作者Troy
set timeout -1
spawn ssh -l eda 10.10.80.202
expect "password:"
send "123456\r" # 密码
interact #控制权移交
执行:
./openssh1
实例二:
文件名:openssh2
#!/usr/bin/expect -f
set timeout -1
spawn ssh -l eda 10.10.80.202
expect "password:"
send "123456\r"
expect "*$"
send "mkdir hello\r"
expect "*$" # 如果是root应该改为"*#"
send "cd hello\r"
expect "*$"
send "ps -aux > hello.log\r"
expect "*$"
send "exit\r"
interact
执行:
./openssh2
PS:
expect默认是不安装的,安装方式如下:
1、Ubuntu
sudo apt-get install expect
2、Redhat
rpm -ivh tcl-8.3.5-88.i386.rpm
rpm -ivh expect-5.38.0-88.i386.rpm
rpm -ivh expect-devel-5.38.0-88.i386.rpm