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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值