bash经典收集
经典收集1
for f in `(cd .; find suite -type f | grep -v SCCS)`;
do
d=/usr/local/mysql/mysql-test/`dirname $f`;
mkdir -p $d ;
/usr/bin/install -c -m 644 ./$f $d ;
done
test -z "/usr/local/mysql/mysql-test" || mkdir -p -- "/usr/local/mysql/mysql-test"
expect
自动登录expect脚本
[root@nginx sbin]# cat 1.expect
#!/usr/bin/expect
set host "192.168.211.135"
set passwd "root123"
spawn ssh root@$host
expect {
"yes/no" { send "yes ";exp_continue }
"assword:" { send "$passwd " }
}
interact
[root@nginx sbin]#
自动登录后执行命令
[root@nginx sbin]# cat 2.expect
#!/usr/bin/expect
set user "root"
set host "192.168.211.135"
set passwd "root123"
spawn ssh $user@$host
expect {
"yes/no" { send "yes ";exp_continue }
"assword:" { send "$passwd " }
}
expect "]*"
send "touch /tmp/12.ext "
expect "]*"
send "echo 1212 >/tmp/12.txt "
expect "]*"
send "exit "
#interact
[root@nginx sbin]#
expect脚本传递参数
[root@nginx sbin]# cat 3.expect
#!/usr/bin/expect
set user [lindex $argv 0]
set host [lindex $argv 1]
set passwd "root123"
set cm [lindex $argv 2]
spawn ssh $user@$host
expect {
"yes/no" { send "yes " }
"password:" { send "$passwd " }
}
expect "]*"
send "$cm "
expect "]*"
send "exit "
[root@nginx sbin]#
[root@nginx sbin]# ./3.expect root 192.168.211.135 "ls /root/"
自动同步文件脚本
[root@nginx sbin]# cat 4.expect
#!/usr/bin/expect
set passwd "root123"
spawn rsync -av root@192.168.211.135:/tmp/12.txt /tmp/
expect {
"yes/no" { send "yes " }
"password:" { send "$passwd " }
}
expect eof
[root@nginx sbin]#
构建简易文件分发系统
适用于有多台机器需要同时更新文件
[root@nginx sbin]# cat rsync.expect
#!/usr/bin/expect
set passwd "root123"
set host [lindex $argv 0]
set file [lindex $argv 1]
spawn rsync -av $file root@$host:/
expect {
"yes/no" { send "yes " }
"password:" { send "$passwd " }
}
expect eof
[root@nginx sbin]#
[root@nginx sbin]# cat ip.list
192.168.211.135
192.168.211.137
[root@nginx sbin]#
[root@nginx sbin]# cat rsync.sh
#!/bin/bash
for ip in `cat ip.list`
do
echo $ip
[ -x rsync.expect ] || chmod a+x rsync.expect
./rsync.expect $ip ip.list
done
[root@nginx sbin]#
命令批量执行脚本
适用于有多台机器需要批量执行命令
[root@nginx sbin]# cat exe.expect
#!/usr/bin/expect
set host [lindex $argv 0]
set passwd "root123"
set cm [lindex $argv 1]
spawn ssh root@$host
expect {
"yes/no" { send "yes " }
"password:" { send "$passwd " }
}
expect "]*"
send "$cm "
expect "]*"
send "exit "
[root@nginx sbin]#
[root@nginx sbin]# cat ip.list
192.168.211.135
192.168.211.137
[root@nginx sbin]#
[root@nginx sbin]# cat exe.sh
#!/bin/bash
for ip in `cat ip.list`
do
echo $ip
[ -x exe.expect ] || chmod a+x exe.expect
./exe.expect $ip "w;free -m;ls /tmp"
done
[root@nginx sbin]#