使用expect捕获输出

时间:2017-10-09 20:14:24

标签: shell expect

我想比较远程服务器和本地目录上的文件数。我ssh到服务器,我能够使用$ expect_out(缓冲区)捕获“ls somewhere / * something | wc -l”的输出并将其存储为变量。现在我的问题是我如何回到我的本地计算机并在这里计算文件并进行比较。在比较之后,如果比较结果可以接受,我需要返回服务器并继续工作。

2 个答案:

答案 0 :(得分:0)

最简单的方法 - 包括从正确性的角度来看 - 是不要尝试单个长时间运行的SSH会话,而是多个短期会话(可能使用SSH multiplexing重用单个传输在这些会议之间):

 <?php
 require_once 'C:/xampp/htdocs/json/pcrov/vendor/autoload.php'; //make sure 
 that this is a correct file path
 use \pcrov\JsonReader\JsonReader;
 ini_set("max_execution_time", 0);
 $reader = new JsonReader();
 $reader->open("jsonfile.json");
 $fo = fopen("csvfile.csv", "w" );
 fputs($fo, "name, companyID, uhc".PHP_EOL);
 {
     if (preg_match('/^foo_/', $reader->name())){ //To retreive keys that 
 // starts with `foo_`, wildcard is employed here using regular expression 
 // function in php. 
    $companyID = $reader->name();
    $companyID1 = str_ireplace("foo_","",$companyID); // this line is 
 // necessary in order to remove foo_ because what is needed in the output 
 // is everything after `foo_`.

    if ($reader->read("entityName")){
        $entityName = $reader->value();
        $entityName = str_ireplace(","," ",$entityName); //comma need to be 
 // replaced because csv treats comma as a delimiter. if comma not removed, 
 // csv pushes everything after comma to another column
    }

    if ($reader->read("ultimateHoldingCompany")){       
        $uhcName = null;
        $uhc = $reader->value();
        if (empty($uhc)){
        }
        else {
            $uhcName = $uhc[0]['name'];
            $uhcName = str_ireplace(","," ",$uhcName); //comma need to be 
 // replaced because csv treats comma as a delimiter. if comma not removed, 
 // csv pushes everything after comma to another column
        }

    }       

    fputs($fo,  $companyID1.",".$entityName.",".$uhcName.PHP_EOL);
    }   
  }
 $reader->close();
 ?>

答案 1 :(得分:0)

由于您使用的是Expect,因此可以使用Tcl命令轻松计算本地文件,因为Expect是在Tcl之上构建的:

set num_local_files [llength [glob somewhere/*something]]

有关详细信息,请参阅http://nikit.tcl.tk/page/Tcl+Tutorial+Lesson+25