通过一个ssh连接管理多个文件,并为每个文件获取返回值 [英] cat multiple files over one ssh connection and get return value for each

查看:84
本文介绍了通过一个ssh连接管理多个文件,并为每个文件获取返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如标题中所述,我正在尝试通过一个ssh连接管理多个文件(内容需要附加到主机上的现有文件),并为每个文件获取返回值,即,如果该特定文件的目录成功了或不。
到目前为止,我只是对每个文件重复执行以下命令并检查返回值,从而分别对每个文件执行了此操作。

As said in the title, i'm trying to cat multiple files (content needs to be appended to existing files on host) over one ssh connection and get return value for each, i.e. if that cat for the particular file was successful or not. Up to now, i did this for each file individually, by just repeating the following command for each and checking the return value.

cat specific_file | ssh user@host -i /root/.ssh/id_rsa "cat >> result/specific_file"



<然后,我只是自动检查了每次传输的返回值,从而可以确定每个文件的状态。我的问题是:是否可以通过一个单一的ssh连接来实现此功能,但可以为每个文件获取返回值?

I then just checked the return value for each transfer (automatically) and thereby could determine the status for each file. My question is: is it possible to to this over one single ssh connection, but to obtain a return value for every single file ?

请先感谢!

编辑:

(b)如以下所示,我生成了一个命令一个特定文件,然后检查返回码。然后,针对不同的类型调用包含程序这一部分的方法o

(b) As you can see in the following, i generate a command for one specific file and then check the return code. The method containing this part of the program is then called for the different types o

如果文件已成功记录,则可以将其从客户端删除,并进行进一步记录(仍然)允许的文件类型。如果文件超出其限制,则在程序的另一部分中未设置该位。

If the file was successfully logged, it can be removed from the client, and that further logging to the type of file is (still) allowed. If the file is exceeding its limits, this bit is unset in another part of the program.

sprintf(command,"/bin/cat /root/%s%s | /usr/bin/ssh log-bot@192.168.5.1 -i"
        " /root/.ssh/id_rsa \"/bin/cat >> result/%s%s\"", apmac,
        ending_str[source], apmac, ending_str[dest]);
rc = system(command);

if(rc != 0)
{
    sprintf(buffer, "[%i] - LOG ERROR from %s to %s CODE %i ", (int)time(0),
            ending_str[source], ending_str[dest], rc);
    sprintf(filename,"%s%s%s", LOCAL, apmac, ending_str[source]);
}
else
{
    sprintf(filename,"%s%s%s", LOCAL, apmac, ending_str[source]);
    remove(filename);
    sprintf(buffer, "[%i] - LOG process from %s to %s ok", (int)time(0),
            ending_str[source], ending_str[dest]);

    switch(source)
    {
    case PROBE_FILE:
        LOG_MASK_SET(globals->log_mask, LOG_MASK_PROB);
        break;
    case LIST_FILE:
        LOG_MASK_SET(globals->log_mask, LOG_MASK_LIST);
        break;
    case SCAN_FILE:
        LOG_MASK_SET(globals->log_mask, LOG_MASK_SCAN);
        break;
    default:
        /* Other cases not YET considered */
        break;
    }
}

第二编辑:

我正在路由器上将此代码作为程序的一部分运行。请注意,我不允许向系统添加新库或非基本功能。另外,系统本身的ssh客户端不允许使用 -M模式。

I'm running this code on a router as a part of a program. Please note that i'm not allowed to add new libraries or non basic functions to the system. In addition, the ssh client on the system itself doesn't allow "-M" mode.

推荐答案

编辑,以响应添加的信息(和代码):



对于代码:我强烈考虑在接收端编写脚本/程序与之交谈通过ssh pipe 发送过程。这样,您就具有了充分的灵活性。

Edit in response to the added info (and code):

To the code: I'd strongly consider writing a script/program on the receiving end to talk to the sending process over the ssh pipe. That way you have full flexibility.

最简单的可行方法似乎仍然是将存档发送到接收主机。在接收端,使用 过滤存档

The simplest thing that could work, would still appear to be sending an archive over to the receiving host. On the receiving end, filter the archive with a script that


  • 将每个文件解压缩到临时位置的脚本

  • 尝试附加操作 cat>> specific_file

  • 将结果记录打印到标准输出,作为对发件人的反馈

所以您应该这样做:

tar c file1 file2 file3 |
   ssh log-bot@remote /home/log-bot/handle_logappends.sh |
   while read resultcode filename
   do
       echo "$filename" resulted in code "resultcode"
   done

要处理C / C ++中的反馈,请查看 popen ,它使您可以像读取文件一样简单地读取流式反馈!

To handle the feedback in C/C++ you'd look at popen, that will allow you to read the streaming feedback as if from a file, simple!

在接收端这样的 handle_logappends.sh 脚本的示例:

An example of such a handle_logappends.sh script on the receiving end:

#!/bin/bash
set -e # bail on error

TEMPDIR="/tmp/.receiving_$RANDOM"
mkdir "$TEMPDIR"

trap "rm -rf '$TEMPDIR/'" INT ERR EXIT

tar x -v -C "$TEMPDIR/" | while read filename
do
    echo "unpacked file $filename" > /dev/stderr

    ## implement your file append logic here :)
    ## e.g. (?):
    cat "$TEMPDIR/$filename" >> "result/$filename"

    ## HERE COMES THE FEEDBACK PART: '<code> <filename>'
    echo "$?" "$filename"
done

其中最整洁的部分是由于所有内容都处于流式传输模式,因此第一个文件的反馈可能会在发送tar仍将后面的文件发送到接收主机的同时到达。 没有不必要的延迟!

The really neat part of this is, that since everything is in streaming mode, the feedback for the first file(s) may be arriving while the sending tar is still sending the later files to the receiving host. No unnecessary delays!

我加入了一些理智的错误处理/清理功能,但我建议

I included a tiny bit of sane error handling/cleanup but I would suggest


  • 也许首先接收整个档案,然后遍历文件?

  • 以原子方式执行附加操作(即在副本上,然后如果整个附加操作成功,则将副本移动到 only 位置;这样可以防止部分追加日志)

  • perhaps receiving the whole archive first, then iterating through the files?
  • doing the appends in atomic fashion (i.e. on a copy, then move the copy into place only if the whole append operation succeeded; this prevents partially appended logs)

希望

旧答案:


您通常会使用一些曲折的小技巧( not ),例如:

tar c file1 file2 file3 | ssh user@host -i /root/.ssh/id_rsa "tar x -C result/ -"

添加详细标志以查看进度详细信息

Add a verbose flag to see progress details

tar c file1 file2 file3 | ssh user@host -i /root/.ssh/id_rsa "tar xvC result/ -"

如果您可以使用 cpio 代替tar。添加选项以获得更多功能(例如, -p 用于保留权限)

If you want, you can substitute cpio for tar. Add options to get more functionality (-p for preserve permissions, e.g.)

要通过单个逻辑连接执行各种单独的步骤,可以使用ssh主连接:

To do various separate steps over a single logical connection, you can use a ssh Master connection:

ssh user@host -i /root/.ssh/id_rsa  -MNf  # login, master, background without a command

for specific_file in file1 file2 file3
do
     cat "$specific_file" |
         ssh user@host -Mi /root/.ssh/id_rsa "cat >> 'result/$specific_file'"
     # check/use error code
done

这篇关于通过一个ssh连接管理多个文件,并为每个文件获取返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆