yum install expect -y
#先安装expect
#!/usr/bin/expect
#解释语言,这边运行要以./运行,bash运行会报错
spawn ssh root@192.168.0.14
#启动新的进程
expect "*password:"
#进程接收字符串,匹配
send "yxy7714707@\r"
#前面匹配到了就输入 “ ” 里的内容
expect "*#"
send "ifconfig>>123.txt\r"
send "exit\r"
interact
#!/bin/bash
ip=$1
#传递参数
user=$2
password=$3
expect <>123.txt\n" }
expect "]#" { send "exit\n" }
#退出
expect eof
EOF
#!/bin/bash
x=`cat .ssh/id_rsa.pub`
ip=$1
password=$2
if [ ! -f "/root/.ssh/id_rsa.pub" ];then
echo "文件不存在"
expect <
#!/bin/bash
expect <
PS :注意匹配为模糊匹配,可以不用写全,写个关键字即可
实战脚本
yum install expect -y
fsip=192.168.0.25
password=yxy7714707@
expect <
#脚本用途,传送本地的hosts文件给 对方