shell远程执行脚本

shell批量传输脚本到远程服务器,再远程批量执行脚本

#!/bin/bash  
  
### << config start ###
username="root"  
password="123456"  
ip_list=("1.1.1.1" "2.2.2.2" "3.3.3.3")
cmd_1="wc -l /root/aaa.txt"
cmd_2="wc -l /root/bbb.txt"
### >> config end ###

# restart all machine
remote_exec_command(){
/usr/bin/expect <<EOF
    set timeout 30 
    spawn scp $2 $user_name@$1:/root
    expect {
        "*yes/no" {send "yes\r";exp_continue}
        "*password:" {send "$password\r"}
    }
    expect eof
EOF
    sshpass -p $password ssh $username@$1 $my_cmd
}

for ip in ${ip_list[@]};
do
    my_cmd=$cmd_1
    remote_exec_command $ip aaa.txt
done
my_cmd=$cmd_2
remote_exec_command $ip bbb.txt
echo -e "\nInit env done."

打赏一个呗

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码支持
扫码打赏,你说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦