我想從我的C程序通過一些命令,下面是示例程序:如何使用C程序在命令propmt上傳遞命令?
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(){
FILE *ssh = popen("ssh [email protected]", "w");
pid_t pid;
pid = fork();
if(!ssh)
{
fprintf(stderr, "Could not open pipe for output.\n");
}
if (pid==0){
fprintf(stdout, "Child process properly created.\n");
fputs("user", ssh);
fputc('\n', ssh); // enter key
_exit(0);
}
fprintf(stdout, "Exit from child process.\n");
pclose(ssh);
return 0 ;
}
現在,當我運行這個程序,它要求密碼在命令提示符下是這樣的:
[email protected]'s password:
我想從我的示例程序中傳遞密碼,而不是從命令提示符處傳遞密碼。 任何人都可以請告訴我如何以編程方式在C中執行此操作。
SSH僅用於例子。也可以用於其他場景。
感謝, Yuvi
如果你的問題是關於'ssh'界面的話,我建議你在superuser.com上提問。 – pmg 2012-03-16 10:08:32
不,它與sssh無關 – Yuvi 2012-03-16 10:13:13
不,這不僅僅與ssh有關,我僅以ssh爲例。有很多情況下,當你使用系統函數啓動任何命令時,它會要求提供一些參數。 – Yuvi 2012-03-16 10:43:57