#!/bin/bash
#********************************************************************
#Author: zhang
#QQ: 531908902
#Date: 2019-01-15
#FileName: expect_scp.sh
#URL:
#Description: The test script
#Copyright (C): 2019 All rights reserved
#********************************************************************
cat name.txt |while read ip user passwd;do
expect <<EOF
spawn scp $user@$ip:/date/* /date/dong
expect "yes" {send "yes\n" }
expect "est" {send "$user\n" }
expect "password" {send "$passwd\n" }
expect eof
EOF
done
其中 name.txt 文件和该脚本都在同一个文件夹 name.txt 格式 172.20.120.39 root centos7 cat的行赋值给while循环的变量 read赋值是一行一行的读取的 read 后面的变量以空格 分别获得 name.txt第一行的值,name.txt 格式也是空格隔开 从而赋值以后 执行后面的命令