自动化数据同步脚本

简介: 利用两个脚本完成集群中的所有电脑数据同步,当然也可以选择使用 RSYNC 进行同步 目标:避免重复输入不同的主机名, 不同的密码   /root/bin/data_ssh.sh   #!/bin/bash server=`grep -E -v  "localhost|^#|^$" /etc/hosts | awk '{print $2}'` echo -n "what

利用两个脚本完成集群中的所有电脑数据同步,当然也可以选择使用 RSYNC 进行同步

目标:避免重复输入不同的主机名, 不同的密码

 

/root/bin/data_ssh.sh

 

#!/bin/bash

server=`grep -E -v  "localhost|^#|^$" /etc/hosts | awk '{print $2}'`

echo -n "what is password: "

read pass

echo -n   "command is: "

read command

for name in $server

do

          /root/bin/data_auto_ssh.sh $name   $pass $command

done

 

 

/root/bin/data_auto_ssh.sh

 

#!/usr/bin/expect -f

set host [lindex $argv 0 ]

set password [lindex $argv 1 ]

set command [lindex $argv 2 ]

set command1 [lindex $argv 3 ]

set command2 [lindex $argv 4 ]

set command3 [lindex $argv 5 ]

set command4 [lindex $argv 6 ]

set command5 [lindex $argv 7 ]

set command6 [lindex $argv 8 ]

set command7 [lindex $argv 9 ]

set command8 [lindex $argv 10 ]

set command9 [lindex $argv 11 ]

 

set timeout 10

spawn   ssh  root@$host $command  $command1 $command2 $command3 $command4 $command5 $command6 $command7  $command8 $command9

expect {

 "*yes/no" { send  "yes\r"; exp_continue}

 "*password:" { send  "$password\r" }

}

expect eof

 

 

用法

[root@test bin]# ./data_ssh.sh

what is password: 你的密码

command is: ifconfig  eth0 | grep Mask | awk '{print $4}'

spawn ssh root@nginx_a ifconfig eth0 |  grep Mask | awk '{print $4}'

root@nginx_a's password:

Mask:255.255.255.0

 

=================================

/root/bin/data_sync.sh

 

server=`grep -E -v  "localhost|^#|^$" /etc/hosts | awk '{print $2}'`

echo -n   "source file is: "

read source

echo $source

echo -n "dest file is: "

read dest

echo $dest

echo -n "what is password: "

read pass

for name in $server

do

          /root/bin/data_auto_scp.sh $name $pass $source $dest

done

 

 

/root/bin/data_auto_scp.sh

 

#!/usr/bin/expect -f

set host [lindex $argv 0 ]

set password [lindex $argv 1 ]

set source [lindex $argv 2 ]

set dest [lindex $argv 3 ]

set timeout 10

spawn scp -r $source  root@$host:$dest

expect {

 "*yes/no" { send  "yes\r"; exp_continue}

 "*password:" { send  "$password\r" }

}

expect eof

 

用法 (缺点,每次只能够传输一个文件,待解决中, 也可以通过自动生成执行脚本方法解决)

 如要传输整个目录,可以填写目录名字,如 /root/bin

[root@test bin]# ./data_sync.sh

source file is: /root/bin/data_auto_ssh.sh

/root/bin/data_auto_ssh.sh

dest file is: /root/bin/

/root/bin/

what is password: 你的密码

spawn scp /root/bin/data_auto_ssh.sh  root@nginx_a:/root/bin/

root@nginx_a's password:

data_auto_ssh.sh 

 

 

 

目录
相关文章
|
20天前
|
jenkins 持续交付
Jenkins自动化部署脚本
Jenkins自动化部署脚本
23 0
|
28天前
|
JavaScript 前端开发 测试技术
使用Selenium执行JavaScript脚本:探索Web自动化的新领域
本文介绍了如何在Selenium中使用JavaScript解决自动化测试中的复杂问题。Selenium的`execute_script`函数用于同步执行JS,例如滑动页面、操作时间控件等。在滑动操作示例中,通过JS将页面滚动到底部,点击下一页并获取页面信息。对于只读时间控件,利用JS去除readonly属性并设置新日期。使用JS扩展了Selenium的功能,提高了测试效率和精准度,适用于各种自动化测试场景。
40 1
|
2月前
|
Python
【速看】如何通过合理的封装,让你的自动化脚本更上一层楼!
【速看】如何通过合理的封装,让你的自动化脚本更上一层楼!
|
2月前
|
Shell 测试技术
Airtest如何自动连接重启后的设备并继续执行自动化脚本呢?
Airtest如何自动连接重启后的设备并继续执行自动化脚本呢?
|
3月前
|
监控 安全 Shell
Shell脚本实现企业电脑屏幕监控的自动化部署与维护
企业信息安全一直是重要的议题,而屏幕监控是一种有效的手段之一。本文将介绍如何使用Shell脚本实现企业电脑屏幕监控的自动化部署与维护,并在结尾部分说明如何将监控到的数据自动提交到指定网站。
207 1
|
3月前
|
Shell Linux
shell 脚本常用于自动化执行文件备份与压缩的任务
shell 脚本常用于自动化执行文件备份与压缩的任务
28 1
|
3月前
|
Unix Shell Linux
在Unix/Linux操作系统中,Shell脚本广泛用于自动化任务
在Unix/Linux操作系统中,Shell脚本广泛用于自动化任务
26 2
|
7天前
|
JSON 测试技术 持续交付
自动化测试与脚本编写:Python实践指南
【4月更文挑战第9天】本文探讨了Python在自动化测试中的应用,强调其作为热门选择的原因。Python拥有丰富的测试框架(如unittest、pytest、nose)以支持自动化测试,简化测试用例的编写与维护。示例展示了使用unittest进行单元测试的基本步骤。此外,Python还适用于集成测试、系统测试等,提供模拟外部系统行为的工具。在脚本编写实践中,Python的灵活语法和强大库(如os、shutil、sqlite3、json)助力执行复杂测试任务。同时,Python支持并发、分布式执行及与Jenkins、Travis CI等持续集成工具的集成,提升测试效率和质量。
|
19天前
|
运维 监控 Linux
linux脚本自动化运维任务
Linux自动化运维通过脚本提升效率,涵盖服务管理(启停服务、异常恢复)、系统监控(资源警报)、日志管理(清理分析)、备份恢复、补丁更新、自动化部署(如Ansible)、网络管理、定时任务(cron)和故障排查。结合shell、Python及工具,形成高效运维体系。
18 3
|
26天前
|
监控
通过Lua脚本实现禁止员工上班玩游戏的软件的自动化任务管理
使用Lua脚本,企业可以自动化管理员工行为,防止上班时间玩游戏。Lua是一种轻量级脚本语言,适合编写监控任务。示例脚本展示了如何检测工作时间内员工是否玩游戏,并在发现时执行相应操作,如关闭游戏或发送警告。此外,另一脚本演示了如何将监控数据通过HTTP POST自动提交到网站,以实现有效的行为管理。这种解决方案灵活且可定制,有助于提升工作效率。
88 1

热门文章

最新文章