首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用expect脚本返回scp命令的输出?

如何使用expect脚本返回scp命令的输出?
EN

Stack Overflow用户
提问于 2013-02-08 18:11:07
回答 1查看 1.1K关注 0票数 0

我正在尝试写一个expect脚本,它将自动执行scp命令并返回scp命令的状态。由于我是新的ecpect脚本,我不能找到任何方法,可以做到这一点。我需要的是自动化的SCP命令使用预期脚本,退出与代码0,如果scp成功,否则返回错误,如连接超时或链接关闭,当传输文件等。

请在这方面帮帮我。

以下是我的脚本片段:

代码语言:javascript
复制
# connect via scp
spawn scp -o ConnectTimeout=30 /home/user/file.tar.gz root@192.168.1.146:/home/user

    #######################
    expect {
          -re ".*es.*o.*" {
                  exp_send "yes\r"
                          exp_continue
                            }
            -re ".*sword.*" {
                    exp_send "abc\r"
                          }
    }
    interact
EN

回答 1

Stack Overflow用户

发布于 2013-02-08 19:14:42

你要找的是wait命令。从expect手册页复制:

代码语言:javascript
复制
   wait [args]
         delays until a spawned process (or the current process if none is
         named) terminates.

         wait normally returns a list of four integers.  The first integer
         is the pid of the process that was waited upon.  The second inte-
         ger is the corresponding spawn id.  The third integer is -1 if an
         operating system error occurred, or 0 otherwise.   If  the  third
         integer  was  0, the fourth integer is the status returned by the
         spawned process.  If the third integer was -1, the fourth integer
         is  the  value  of errno set by the operating system.  The global
         variable errorCode is also set.

         Additional elements may appear at the end  of  the  return  value
         from  wait.   An  optional  fifth  element  identifies a class of
         information.  Currently, the only possible value for this element
         is  CHILDKILLED in which case the next two values are the C-style
         signal name and a short textual description.

         The -i flag declares the process to  wait  corresponding  to  the
         named  spawn_id  (NOT the process id).  Inside a SIGCHLD handler,
         it is possible to wait for any spawned process by using the spawn
         id -1.

         The  -nowait  flag causes the wait to return immediately with the
         indication of a successful wait.  When the process exits (later),
         it  will automatically disappear without the need for an explicit
         wait.

         The wait command may also be used wait for a forked process using
         the  arguments  "-i  -1".  Unlike its use with spawned processes,
         this command can be executed at any time.  There  is  no  control
         over  which  process is reaped.  However, the return value can be
         checked for the process id.

这不包括派生的子进程(在您的示例中为scp)或它包含的任何子进程生成的任何输出;默认情况下,这些输出指向您通过expectinteract使用的虚拟终端。通过expect/interacttimeouteof子句可以更好地处理超时和文件结束事件。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14770172

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档