[英]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 exit
, man 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.