简体   繁体   English

获取 system() 运行的 SFTP 命令的退出代码

[英]Get exit code of SFTP command run by system()

my program created a script that executes sftp command using expect.我的程序创建了一个使用expect 执行sftp 命令的脚本。 The script should download a file from SFTP server.该脚本应从 SFTP 服务器下载文件。 When the script is generated, my program call system command to run the script.当脚本生成时,我的程序调用系统命令来运行脚本。 The script should exit when an error is encountered.遇到错误时,脚本应退出。 For example, if the password is wrong.例如,如果密码错误。 How am I be able to get the error returned by sftp command?我怎样才能得到 sftp 命令返回的错误?

Below is the code that generates and run the script:下面是生成和运行脚本的代码:

downloadFileFromServer()
{
    ofstream downloadScript;
    string  result, command;

    downloadScript.open("/temp/download.sh");

    downloadScript << "#!/usr/bin/expect -f \n";
    downloadScript << "spawn sftp -P " + <port number> +
                      " " + <user name> +
                      "@" + <IP> + "\n";
    downloadScript << "expect { \ntimeout {\nsend_user \"Permission denied\\n\"\nexit\n}\n}\n";

    downloadScript << "send \"" + <password> + "\\n\"\n";
    downloadScript << "expect \"sftp>\"""\n";
    downloadScript << "send \"cd " + <source directory> + "\\n\"\n";
    downloadScript << "expect \"sftp>\"""\n";
    downloadScript << "send \"lcd " + <destination directory> + "\\n\"\n";
    downloadScript << "expect \"sftp>\"""\n";
    downloadScript << "send \"get " + <file name> + "\\n\"\n";
    downloadScript << "expect \"sftp>\"""\n";
    downloadScript << "send \"exit\\n\"\n";
    downloadScript << "expect -exact \"$\"""\n";

    downloadScript.close();

    command = "chmod +x /temp/download.sh && /temp/download.sh";
    system(command.c_str());

}

Below is the script created:下面是创建的脚本:

#!/usr/bin/expect -f 
spawn sftp -P <port numner> <user name>@<IP>
expect "<user name>@<IP>'s password:"
expect { 
timeout {
send_user "Permission denied\n"
exit
}
}
send "<password>\n"
expect "sftp>"
send "cd <source directory>\n"
expect "sftp>"
send "lcd <destination directory>\n"
expect "sftp>"
send "get <file name>\n"
expect "sftp>"
send "exit\n"
expect -exact "$"

I want my program to catch the "Permission denied" error.我希望我的程序捕获“权限被拒绝”错误。

All processes set a numeric exit code (0..255) when they end.所有进程在结束时都会设置一个数字退出代码 (0..255)。 Change the expect script so that you can distinguish between the script ending normally (when usually the exit code will be 0) and the explicit exit after an error.更改 expect 脚本,以便您可以区分正常结束的脚本(通常退出代码为 0)和出现错误后的显式exit Do this by adding a parameter to that line, eg:通过向该行添加参数来执行此操作,例如:

...
send_user "Permission denied\n"
exit 77
...

You can recover this code in a C/C++ program with您可以在 C/C++ 程序中恢复此代码

int wstatus = system(command.c_str());
if (WIFEXITED(wstatus))
  int exit_code = WEXITSTATUS(wstatus);

The wstatus is formed of 2 parts, the exit code of the process, and a some flags giving information about whether the process ran or was interrupted and so on. wstatus由两部分组成,进程的退出代码和一些标志,提供有关进程是运行还是被中断等的信息。 Use the above macros to extract the information.使用上面的宏来提取信息。 See man 3 exit , man waitpid .看到man 3 exitman waitpid

You need to send the password before checking for timeouts您需要在检查超时之前发送密码

expect "<user name>@<IP>'s password:"
send "<password>\n"
expect {
    timeout {
        send_user "Permission denied\n"
        exit 1
    }
    "sftp>"
}
send "cd ..."

I have expect looking for either the timeout or the prompt, whichever one comes first.我期待寻找超时或提示,以先到者为准。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何捕获在C ++中运行的命令的exit_code和stderr? - How to capture the exit_code and stderr of the command that is run in C++? 如何获取系统调用调用的程序的退出代码? - How to get the exit code of program invoked by system call? 等待单个对象检测系统命令退出 - Waitforsingleobject to detect a system command exit SDL_ttf框架无法在xCode上运行,给出错误链接器命令失败,退出代码为1 - SDL_ttf framework wont run on xCode, giving error linker command failed with exit code 1 当我在代码中使用“字符串”命令时,NetBeans C ++将建立但不会运行(退出值127) - NetBeans C++ will BUILD but not RUN (exit value 127) when I use a “string” command in my code 打包错误:命令“ adb.exe -s emulator-5554 pull / system / bin / app_process”失败。退出代码:1 - Packaging error: Command “adb.exe -s emulator-5554 pull /system/bin/app_process” failed.Exit code: 1 ld:找不到用于-lboost_system clang的库:错误:链接器命令失败,退出代码为1(使用-v查看调用) - ld: library not found for -lboost_system clang: error: linker command failed with exit code 1 (use -v to see invocation) 创建类对象时,出现“错误:链接器命令失败,退出代码为1” - When creating a class object, I get “error: linker command failed with exit code 1” 在Windows C ++中的cmd中运行命令后如何获取退出代码 - How to get exit code after running command in cmd in windows c++ C ++:Linker命令在xcode中的退出代码为-1 - C++:Linker command with exit code -1 in xcode
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM