我需要启动从openwrt系统到云计算机的反向ssh隧道。 openwrt系统上的应用程序将在启动时作为守护程序运行。当它通过网络接收到特定代码时,应产生一个启动远程ssh隧道的进程。 系统中将没有登录。
我正在使用以下代码来做到这一点。
int spawn_orphan(char* cmd)
{
char command[1024]; // We could segfault if cmd is longer than 1000 bytes or so
int pid;
int Stat;
pid = fork();
if (pid < 0)
{
perror("FORK FAILED\n");
return pid;
}
if (pid == 0)
{ // CHILD
setsid(); // Make this process the session leader of a new session
pid = fork();
if (pid < 0)
{
printf("FORK FAILED\n");
return ( 1 );
}
if (pid == 0)
{ // GRANDCHILD
sprintf(command, "ash -c '%s'", cmd);
execl("/bin/ash", "ash", "-c", command, NULL); // Only returns on error
perror("execl failed");
return ( 1 );
}
exit(0); // SUCCESS (This child is reaped below with waitpid())
}
// Reap the child, leaving the grandchild to be inherited by init
waitpid(pid, &Stat, 0);
if ( WIFEXITED(Stat) && ( WEXITSTATUS(Stat) == 0 ))
{
printf("dbclient exit\n");
return 0; // Child forked and exited successfully
}
else
{
perror("failed to spawn orphan\n");
return 1;
}
}
这是我的调用函数
uint8_t rdsService(uint8_t state)
{
char buffer[1024];
uint8_t retVal;
if(state )
{
retVal = spawn_orphan("dbclient -f -K -I -T -N -R 1500:localhost:22 -p 22 username@HOSTMACHINE -i ~/ctusr/keys/X1_ID_RSA -y");
}
else
{
retVal = spawn_orphan("ash -c 'killall dbclient'");
}
return retVal;
}
如果我从控制台运行父应用程序,则该应用程序可以生成dbclient,并且可以将ssh反向输入系统。
当我将父应用程序作为守护程序运行时会发生问题,在这种情况下,该应用程序会生成dbclient,当我在控制台上执行“ ps”时我可以看到它,但是几秒钟后dbclient被杀死了。 >
我已经尝试使用'nohup'运行dbclient
nohup dbclient -f -K -I -T -N -R 1500:localhost:22 -p 22 用户名@ machine-i〜/ ctusr / keys / X1_ID_RSA -y&
当我从控制台运行应用程序时工作正常,但是当应用程序作为守护程序运行时出现相同的错误。
感谢帮助
答案 0 :(得分:0)
直到今天,我一个月都遇到了同样的问题, 我的项目只有一点点不同,即我使用Shell脚本而不是C编译程序来打开ash。 我想您正在使用proc脚本在引导过程之后运行逻辑,如果可以的话,我的技巧可能会有用。 在procd脚本中,我将home添加为环境变量,这为我解决了问题。 我知道已经过去了一年,您已经找到了替代解决方案,但我仍然希望此评论对其他人有用。 祝你有美好的一天。