20.31 expect脚本同步文件

expect脚本批量文件同步
本文介绍如何使用expect脚本实现文件的批量同步与远程命令执行。通过具体示例展示了如何构建文件分发系统,包括rsync命令的使用、expect脚本的编写及常见问题的解决。

20.31 expect脚本同步文件

自动同步文件 :

#!/usr/bin/expect
set passwd "123456"
spawn rsync -av root@192.168.133.132:/tmp/12.txt /tmp/
expect {
"yes/no" { send "yes\r"}
"password:" { send "$passwd\r" }
}
expect eof

脚本解析:

spawn rsync -av root@192.168.133.132:/tmp/12.txt /tmp/
##以上是将/tmp/12.txt同步到本机/tmp/目录下;rsync同步
expect eof
##脚本执行完之后 ,再退出;如果没有这句 则直接突出,不会执行任何命令

执行效果:

[root@DasonCheng sbin]# rm -f /tmp/12.txt 
[root@DasonCheng sbin]# ls /tmp/12.txt
ls: 无法访问/tmp/12.txt: 没有那个文件或目录
[root@DasonCheng sbin]# ./4.expect 
spawn rsync -av root@192.168.60.12:/tmp/12.txt /tmp/
root@192.168.60.12's password: 
receiving incremental file list
12.txt
sent 30 bytes  received 84 bytes  76.00 bytes/sec
total size is 5  speedup is 0.04
[root@DasonCheng sbin]# !l
ls /tmp/12.txt
/tmp/12.txt

将expect eof注释掉:

[root@DasonCheng sbin]# !vim
vim 4.expect
[root@DasonCheng sbin]# !r
rm -f /tmp/12.txt 
[root@DasonCheng sbin]# ./4.expect 
spawn rsync -av root@192.168.60.12:/tmp/12.txt /tmp/
root@192.168.60.12's password: [root@DasonCheng sbin]# !l    ##可能密码都没有输入就退出了,所以expect eof还是很有必要的;
ls /tmp/12.txt
ls: 无法访问/tmp/12.txt: 没有那个文件或目录

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

指定host和要同步的文件(从本地到远程地址):

#!/usr/bin/expect
set passwd "123456"
set host [lindex $argv 0]
set file [lindex $argv 1]
set file2 [lindex $argv 2]
spawn rsync -av $file root@$host:$file2
expect {
"yes/no" { send "yes\r"}
"password:" { send "$passwd\r" }
}
expect eof

执行结果(缺陷,只能传送一个文件;):

[root@DasonCheng sbin]# vim 5.expect
[root@DasonCheng sbin]# chmod +x 5.expect 
[root@DasonCheng sbin]# ./5.expect 192.168.60.12 /tmp/12.txt /root/
spawn rsync -av /tmp/12.txt root@192.168.60.12:/root/
root@192.168.60.12's password: 
sending incremental file list
12.txt

sent 79 bytes  received 31 bytes  220.00 bytes/sec
total size is 5  speedup is 0.05
[root@DasonCheng sbin]# 

20.33 构建文件分发系统

shell项目-分发系统-构建文件分发系统:

需求背景:
对于大公司而言,肯定时不时会有网站或者配置文件更新,而且使用的机器肯定也是好多台,少则几台,多则几十甚至上百台。所以,自动同步文件是至关重要的。
实现思路:
首先要有一台模板机器,把要分发的文件准备好,然后只要使用expect脚本批量把需要同步的文件分发到目标机器即可。
核心命令
rsync -av --files-from=list.txt  /  root@host:/

文件分发系统的实现:

  • rsync.expect 内容
#!/usr/bin/expect
set passwd "123456"
set host [lindex $argv 0]
set file [lindex $argv 1]
spawn rsync -av --files-from=$file / root@$host:/
expect {
"yes/no" { send "yes\r"}
"password:" { send "$passwd\r" }
}
expect eof
  • list.txt内容
[root@DasonCheng sbin]# vim list.txt
/root/777
/etc/111/12.txt
  • ip.txt内容
[root@DasonCheng sbin]# vim ip.txt
192.168.133.132
192.168.133.133
......
  • rsync.sh 内容
#!/bin/bash
for ip in `cat ip.txt`
do
    echo $ip
    ./rsync.expect $ip list.txt
done

文件分发系统内容解析:

rsync -avR --files-from=$file / root@$host:/
##这个为核心命令,$file指的是第二个参数list.txt文件集,都得用绝对路径;/指的是本机根目录、$host:/指的是远程根目录
##参数-R是在同步的时候,如果没有目录就会自动创建

执行效果:

[root@DasonCheng sbin]# sh -x rsync.sh 
++ cat ip.txt
+ for ip in '`cat ip.txt`'
+ echo 192.168.60.12
192.168.60.12
+ cd /usr/local/sbin
+ ./rsync.expect 192.168.60.12 list.txt
spawn rsync -avR --files-from=list.txt / root@192.168.60.12:/
root@192.168.60.12's password: 
building file list ... done
etc/
etc/111/
etc/111/12.txt
root/
root/777/
##以上效果已经实现,文件已经传达到了60.12;文件夹自动创建成功;
sent 145 bytes  received 43 bytes  376.00 bytes/sec
total size is 0  speedup is 0.00
+ for ip in '`cat ip.txt`'
+ echo 127.0.0.1
127.0.0.1
+ cd /usr/local/sbin
+ ./rsync.expect 127.0.0.1 list.txt
spawn rsync -avR --files-from=list.txt / root@127.0.0.1:/
The authenticity of host '127.0.0.1 (127.0.0.1)' can't be established.
ECDSA key fingerprint is b8:10:7b:2b:90:0a:d9:02:51:c9:57:33:81:52:ab:59.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '127.0.0.1' (ECDSA) to the list of known hosts.
root@127.0.0.1's password: 
##可是,传送本地却报错了;我试着直接用rsync同步本地内容,也是同样报错;所以本地不可同步

重新执行上面的脚本!

[root@DasonCheng sbin]# sh -x rsync.sh 
++ cat ip.txt
+ for ip in '`cat ip.txt`'
+ echo 192.168.60.12
192.168.60.12
+ cd /usr/local/sbin
+ ./rsync.expect 192.168.60.12 list.txt
spawn rsync -avR --files-from=list.txt / root@192.168.60.12:/
root@192.168.60.12's password: 
building file list ... done

sent 94 bytes  received 12 bytes  212.00 bytes/sec
total size is 0  speedup is 0.00
+ for ip in '`cat ip.txt`'
+ echo 127.0.0.1
127.0.0.1
+ cd /usr/local/sbin
+ ./rsync.expect 127.0.0.1 list.txt
spawn rsync -avR --files-from=list.txt / root@127.0.0.1:/
root@127.0.0.1's password: 
protocol version mismatch -- is your shell clean?
(see the rsync man page for an explanation)
rsync error: protocol incompatibility (code 2) at compat.c(174) [sender=3.0.9]
##对本地是这样报错的;

shell环境直接执行:

[root@DasonCheng sbin]# 
[root@DasonCheng sbin]# rsync -av /root/777 root@127.0.0.1:/tmp/777
root@127.0.0.1's password: 
protocol version mismatch -- is your shell clean?
(see the rsync man page for an explanation)
rsync error: protocol incompatibility (code 2) at compat.c(174) [sender=3.0.9]
##本地执行报同样的错误!
[root@DasonCheng sbin]# rsync -av /root/777 root@192.168.60.12:/tmp/777
root@192.168.60.12's password: 
sending incremental file list
777/

sent 39 bytes  received 16 bytes  12.22 bytes/sec
total size is 0  speedup is 0.00
##同步到远程却可以!

20.34 批量远程执行命令

分发系统批量传送文件之后,来批量执行命令--这就类似于自动化
批量执行命令实现:

  • exe.expect 内容
#!/usr/bin/expect
set host [lindex $argv 0]
set passwd "123456"
set cm [lindex $argv 1]
spawn ssh root@$host
expect {
"yes/no" { send "yes\r"}
"password:" { send "$passwd\r" }
}
expect "]*"
send "$cm\r"
expect "]*"
send "exit\r"
  • exe.sh 内容
#!/bin/bash
for ip in `cat ip.list`
do
    echo $ip
    ./exe.expect $ip "w;free -m;ls /tmp"
done

注意:
expect脚本,文件名后缀以exp或者expect;
文件头必须以#!/usr/bin/expect 亲测,必须以这个开头; 需要给与chmod +x 执行权限;

转载于:https://my.oschina.net/u/3651233/blog/1541101

<think>我们面临的任务:在C++中读取.xyz文件的数据,且不使用PCL库。.xyz文件通常是一种简单的文本格式,每行包含一个点的坐标,可能还有额外的属性(如强度、颜色等)。基本格式为:XYZ[其他属性...]例如:0.10.20.31.02.03.04.05.06.0步骤:1.打开文件2.逐行读取3.解析每一行的数据(以空格分隔的浮点数)4.将解析出的数据存储到数据结构中(例如vector<Point>,其中Point是一个结构体,包含x,y,z)注意:.xyz文件可能有不同的变种,比如有的包含法向量(nx,ny,nz)或颜色(r,g,b)等,但基本形式是空格分隔的数值。我们将实现一个简单的程序,读取只包含x,y,z坐标的.xyz文件。假设我们的Point结构体定义为:structPoint{floatx,y,z;};代码示例:*/#include<iostream>#include<fstream>#include<vector>#include<string>#include<sstream>//定义点结构structPoint{floatx,y,z;};intmain(){std::stringfilename="points.xyz";//替换为你的文件路径std::ifstreamfile(filename);if(!file.is_open()){std::cerr<<"Failedtoopenfile:"<<filename<<std::endl;return-1;}std::vector<Point>points;std::stringline;while(std::getline(file,line)){//使用stringstream解析每行std::istringstreamiss(line);Pointp;//尝试读取三个浮点数if(!(iss>>p.x>>p.y>>p.z)){//如果读取失败,跳过这一行(可能是空行或格式错误)continue;}points.push_back(p);}file.close();//打印读取的点数std::cout<<"Read"<<points.size()<<"points."<<std::endl;//可选:打印前几个点for(size_ti=0;i<std::min(points.size(),static_cast<size_t>(10));++i){std::cout<<"Point"<<i<<":"<<points[i].x<<","<<points[i].y<<","<<points[i].z<<std::endl;}return0;}/*说明:1.该程序逐行读取文件,每行解析三个浮点数(x,y,z)。2.如果文件中有额外的数据(如法向量、颜色等),则需要在结构体中添加相应字段,并修改解析部分。3.该程序假设坐标值之间以空格分隔(也可以是制表符,因为istringstream会自动处理空白字符)。如果.xyz文件包含注释(例如以#开头的行),可以在解析前跳过:if(line.empty()||line[0]==&#39;#&#39;){continue;}如果文件很大,可以调整打印部分,避免输出过多。相关问题:1.如何处理包含额外属性(如颜色、法向量)的.xyz文件2.如何提高读取大量.xyz文件的效率?3.如何将读取的点云数据可视化?4.如果文件格式不是空格分隔而是逗号分隔(如.csv),应如何修改?5.如何将读取的点云数据写入到其他格式的文件中(如PLY)?</think>### 在C++中读取.xyz文件(不使用PCL库) .xyz文件是一种常见的点云数据格式,每行包含一个点的三维坐标(X,Y,Z),格式示例如下: ``` 0.1 0.2 0.3 1.5 2.8 -0.7 3.2 4.1 5.6 ``` 以下是完整的C++实现方案: ```cpp #include <iostream> #include <fstream> #include <vector> #include <string> #include <sstream> // 定义点结构体 struct Point3D { float x, y, z; }; int main() { // 1. 打开文件 const std::string filename = "points.xyz"; // 替换为实际文件路径 std::ifstream file(filename); if (!file.is_open()) { std::cerr << "Error: Failed to open file " << filename << std::endl; return 1; } // 2. 读取数据 std::vector<Point3D> pointCloud; std::string line; int lineCount = 0; while (std::getline(file, line)) { lineCount++; // 跳过空行和注释行(以#开头) if (line.empty() || line[0] == &#39;#&#39;) continue; std::istringstream iss(line); Point3D point; // 3. 解析XYZ坐标 if (!(iss >> point.x >> point.y >> point.z)) { std::cerr << "Warning: Invalid format at line " << lineCount << std::endl; continue; } // 4. 存储点数据 pointCloud.push_back(point); } file.close(); // 5. 输出结果 std::cout << "Successfully read " << pointCloud.size() << " points\n"; if (!pointCloud.empty()) { std::cout << "First point: " << pointCloud[0].x << ", " << pointCloud[0].y << ", " << pointCloud[0].z << "\n"; } return 0; } ``` #### 关键实现细节: 1. **文件处理** 使用`std::ifstream`读取文件,自动处理不同操作系统的换行符差异。 2. **数据解析** - 通过`std::istringstream`将每行文本转换为数值 - 自动跳过空行和注释行(以`#`开头) - 错误处理无效数据行 3. **数据结构** - 自定义`Point3D`结构体存储坐标 - 使用`std::vector`动态存储点云数据 4. **扩展性** 如需处理带颜色/法线的.xyz文件(格式:`X Y Z R G B`),可修改结构体: ```cpp struct Point3D { float x, y, z; uint8_t r, g, b; // 添加颜色通道 }; ``` #### 常见问题解决方案: 1. **处理逗号分隔文件** 修改解析部分: ```cpp char comma; iss >> point.x >> comma >> point.y >> comma >> point.z; ``` 2. **处理大文件优化** 预分配内存: ```cpp pointCloud.reserve(1000000); // 预分配100万个点空间 ``` 3. **二进制.xyz文件处理** 若为二进制格式(较少见),需指定读取模式: ```cpp std::ifstream file(filename, std::ios::binary); file.read(reinterpret_cast<char*>(&point), sizeof(Point3D)); ``` ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值