java远程同步文件,expect同步文件&expect指定host和要同步的文件&构建文件分发系统&批量远程执行命令...

20.31 expect脚本同步文件

expect通过与rsync结合,可以在一台机器上把文件自动同步到多台机器上

编写脚本

[root@linux-5 ~]# cd /usr/local/sbin

[root@linux-5 sbin]# vim 4.expect

#!/usr/bin/expect

set passwd "123456"

spawn rsync -av root@192.168.88.10:/root/1.txt /tmp/

expect {

"yes/no" { send "yes\r"}

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

}

expect eof

注:expect eof的作用同interact,可以为spwan所执行的命令提供足够的时间执行,不会马上退出expect,从而导致命令尚未执行完毕而退出的情况(尤其是文件传输的命令)

脚本授权

[root@linux-5 sbin]# chmod a+x !$

chmod a+x ./4.expect

执行脚本

[root@linux-5 sbin]# ./4.expect

spawn rsync -av root@192.168.88.10:/root/1.txt /tmp/

receiving incremental file list

1.txt

sent 43 bytes received 96 bytes 278.00 bytes/sec

total size is 6 speedup is 0.04

expect: spawn id exp6 not open

while executing

"expect eof"

(file "./4.expect" line 8)

结果验证

[root@linux-5 sbin]# cat /tmp/1.txt

12345

20.32 expect脚本指定host和要同步的文件

通过带参数的方式为指定的地址同步指定的文件

编写脚本

#!/usr/bin/expect

set passwd "123456"

set host [lindex $argv 0] #第一个参数

set file [lindex $argv 1] #第二个参数

spawn rsync -av $file root@$host:$file

expect {

"yes/no" { send "yes\r"}

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

}

expect eof

脚本授权

[root@linux-5 sbin]# chmod a+x 5.expect

执行脚本

[root@linux-5 sbin]# ./5.expect 192.168.88.10 /tmp/1.txt

spawn rsync -av /tmp/1.txt root@192.168.88.10:/tmp/1.txt

sending incremental file list

1.txt

sent 96 bytes received 35 bytes 262.00 bytes/sec

total size is 6 speedup is 0.05

expect: spawn id exp6 not open

while executing

"expect eof"

(file "./5.expect" line 10)

结果验证

[root@linux-10 ~]# cat /tmp/1.txt

12345

20.33 构建文件分发系统

20.34 批量远程执行命令

转载至链接:https://my.oschina.net/u/3804357/blog/1860414

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java文件夹复制(远程复制(网络传输),用于远程备份文件)(支持文件夹,嵌套子文件夹) import java.io.*; import java.util.*; public class FileSelection { private File rootDirectory;//根目录 private File[] fileList;//文件目录下面的文件列表(包括目录,用于多次判断) private ArrayList fileArrayList; // 用于存储文件(只是文件)列表 //初始化参数 public FileSelection() { fileArrayList=new ArrayList(); rootDirectory = new File("Test"); rootDirectory.mkdir(); } //获得文件(不包括目录)的列表 public void initFileArrayList() { if (rootDirectory.isDirectory()) { //遍历目录下面的文件和子目录 fileList = rootDirectory.listFiles(); for (int i = 0; i < fileList.length; i++) { //如果是文件,添加到文件列表中 if(fileList[i].isFile()){ fileArrayList.add(fileList[i]); } //否则递归遍历子目录 else if (fileList[i].isDirectory()) { fileList[i].mkdir(); rootDirectory=fileList[i]; initFileArrayList(); } } } } //将文件信息添加到列表中 public void addFiles(File f){ fileArrayList.add(f); } //访问器返回文件列表 public ArrayList getFileArrayList() { return fileArrayList; } } -------------------- BackupClient.java package com.xinxin.Client; import java.io.*; import java.net.*; /** * * @author Administrator *@version 1.0 *BackupClient类实现文件的传输到服务器 */ public class BackupClient implements Runnable{ private int port;//服务器端口 private InetAddress ipAddress;//服务器IP地址 private Socket clientSocket;//客户端套接字 private InputStream inputStream;//网络输入流 private OutputStream outputStream;//网络输出流 private File file; //构造函数(获得服务器端IP地址和监听端口号) public BackupClient(InetAddress ipAddress,int port,File file){ this.ipAddress=ipAddress; this.port=port;

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值