admin 发表于 2024-8-21 16:32:21

SHELL编程密码远程拷贝文件脚本

1.1SHELL编程密码远程拷贝文件脚本

[*]自动服务器IP和用户名、密码表:list.txt;
[*]自动安装expect远程交互工具;
[*]自动编写expect远程执行拷贝文件;
[*]支持任意文件远程拷贝操作;
[*]支持列表循环操作多个台服务器。

<font size="4">#!/bin/bash
#by author www.jfedu.net 2024
#######################
if
      [ ! -e /usr/bin/expect ];then
      yuminstall expect -y
fi
#Judge passwd.txt exist
if
      [ ! -e ./passwd.txt ];then
      echo -e "The passwd.txt is not exist......Please touch ./passwd.txt ,Content Example:\n192.168.1.11 passwd1\n192.168.1.12 passwd2"
      sleep 2 &&exit 0
fi
#Auto Tuoch login.exp File
cat>login.exp <<EOF
#!/usr/bin/expect -f
set ip
set passwd
set src_file
set des_dir
set timeout -1
spawn scp -r \$src_file root@\$ip:\$des_dir
expect {
"yes/no"   { send "yes\r"; exp_continue }
"password:"{ send "\$passwd\r" }
}
expect "100%"
expect eof
EOF
##Auto exec shell scripts
if
    [ "$1" == "" ];then
    echo ========================================================
    echo "Please insert your are command ,Example {/bin/sh$0 /src/des } ,waiting exit ........... "
    sleep 2
    exit 1
fi
for i in `awk '{print $1}'passwd.txt`
do
      j=`awk -v I="$i" '{if(I==$1)print $2}' passwd.txt`
      expect ./login.exp$i$j $1 $2
done</font>


页: [1]
查看完整版本: SHELL编程密码远程拷贝文件脚本