Ich erstelle einen Shell-Befehl von der benutzerdefinierten Shell, um den SSH von einem Terminal zu einem anderen Terminal zu tun. Um das ssh zu machen, verwende ich den eingebauten ssh-Befehl des Linux. Hier ist mein Code, der die ssh-Anmeldung ausführt. Ich sehe jedoch, dass die I/O-Puffer nicht synchron sind.Wie synchronisiert man Eingabe und Ausgabe in Pipes Linux?
Dies ist, was ich auf dem Terminal sehe. Nach SSH zum anderen Terminal. Ich habe folgendes im Terminal gemacht.
PRT# ssh 192.168.10.42
PRT# Could not create directory '/root/.ssh'.
[email protected]'s password:
# screen -r
-sh: cen-: not found
# hello
-sh: el: not found
#
Ich weiß nicht, was ist der Grund hier. Hier ist der Code.
int sshLogin(chr *destIp)
{
char cmd[CMD_LEN];
char readbuff[CMD_LEN];
pid_t pid;
int ret = 0;
int fd[2];
int result;
memset(cmd,'\0',sizeof(cmd));
int status = 0;
/** --tt required to force pseudowire allocation because we are behind screen app **/
sprintf(cmd,"/usr/bin/ssh -tt %s",destIp);
/** create a pipe this will be shared on fork() **/
pipe(fd);
if((pid = fork()) == -1)
{
perror("fork:");
return -1;
}
if(pid == 0)
{
/** Child Process of Main APP --Make this parent process for the command**/
if((pid = fork()) == -1)
{
perror("fork:");
return -1;
}
if(pid == 0)
{
/** basically Main APP grand child - this is where we running the command **/
ret = execlp("ssh", "ssh", "-tt", destIp, NULL);
printf("done execlp\r\n");
}
else
{
/** child of Main APP -- keep this blocked until the Main APP grand child is done with the job **/
while((read(fd[0], readbuff, sizeof(readbuff))))
{
printf("%s",readbuff);
}
waitpid(0,&status,0);
LOG_STRING("SSH CONNC CLOSED");
exit(0);
}
}
else
{
/** Parent process APP MAIN-- **/
/** no need to wait let APP MAIN run -- **/
}
return 0;
}
auf Patrick Ideen Basierend.
POST 2 # - Es scheint, dass es funktioniert, wenn wir die Stdin im übergeordneten Prozess schließen. Allerdings wird es sehr schlampig, ich fühle mich, als würde ich die Tastatur zu langsam tippen. Das System wird zu träge. Ich habe auch einen Webserver von diesem Terminal. Ich sehe, dass ich nicht mehr auf das Web zugreifen kann. Also, die Lösung ist irgendwo um Stdin, aber ich bin mir nicht sicher.
int sshLogin(chr *destIp)
{
char cmd[CMD_LEN];
char readbuff[CMD_LEN];
pid_t pid;
int ret = 0;
int fd[2];
int result;
memset(cmd,'\0',sizeof(cmd));
int status = 0;
/** --tt required to force pseudowire allocation because we are behind screen app **/
sprintf(cmd,"/usr/bin/ssh -tt %s",destIp);
/** create a pipe this will be shared on fork() **/
pipe(fd);
if((pid = fork()) == -1)
{
perror("fork:");
return -1;
}
if(pid == 0)
{
/** Child Process of Main APP --Make this parent process for the command**/
if((pid = fork()) == -1)
{
perror("fork:");
return -1;
}
if(pid == 0)
{
/** basically Main APP grand child - this is where we running the command **/
ret = execlp("ssh", "ssh", "-tt", destIp, NULL);
printf("done execlp\r\n");
}
else
{
/** child of Main APP -- keep this blocked until the Main APP grand child is done with the job **/
while((read(fd[0], readbuff, sizeof(readbuff))))
{
printf("%s",readbuff);
}
waitpid(0,&status,0);
LOG_STRING("SSH CONNC CLOSED");
exit(0);
}
}
else
{
/** Parent process APP MAIN-- **/
/** no need to wait let APP MAIN run -- **/
close(stdin);
}
return 0;
}
Grundsätzlich habe ich hinzugefügt - close (stdin);
Ich sehe, dass das Lesen nie aufgerufen wird. Tatsächlich habe ich diesen Teil jetzt auskommentiert. –
Oh, Sie kehren auch aus dem Programm zurück, ohne darauf zu warten, dass Ihr gegabelter Prozess beendet wird, so dass Ihre Eltern-Shell versucht, von STDIN und Ihrem gegabelten 'ssh' zu lesen. – Patrick
haben das Problem jetzt umformuliert. Es scheint, dass es nur mit STDIN zu tun hat. –