expect使用ssh登录脚本
基础篇:
#!u/s+ SSH
spawn ssh -l root 10.10.10.24
# 等待响应,第一次登录往往会提示是否永久保存 RSA 到本机的 know hosts 列表中;等到回答后,在提示输出密码;之后就直接提示输入密码
expect "(yes/no)?" {
send "yes\r"
expect "password:"
send "123456\r"
} "password:" {send "123456\r"} "*host "
interact
# 这里使用了 interact 命令,使执行完程序后,用户可以在 $host 终端进行交互操作。
多用户登录,使用shell进行调用
#!/usr/bin/expect -f
set ipaddress [lindex $argv 0]
set passwd [lindex $argv 1]
set timeout 30
spawn ssh root@$ipaddress
expect {
"yes/no" { send "yes\r";exp_continue }
"password:" { send "$passwd\r" }
}
expect "*from*"
interact
~
shell调用脚本
#!/bin/bash
if [ $1 == 24 ];then
i='10.10.10.24'
j='123456'
fi
if [ $1 == 17 ];then
i='10.10.10.17'
j='1qaz2wsx'
fi
echo $i
echo $j
expect /home/shell/benchmark/login.exp $i $j
- 1楼KimSoft昨天 10:15
- 如果只是ssh的话,可以试试ssh-copy-id,这种密码写在文本里的脚本有安全隐患。https://help.ubuntu.com/12.04/serverguide/openssh-server.html
- Re: gzh0222昨天 11:27
- 回复KimSoftn也可以远程执行命令,测试时方便切换服务器,这些脚本主要是方便自己用的