OpenSSH有一个名为的选项LocalCommand
,当您建立ssh连接时,该选项在客户端运行命令。不幸的是,它在建立ssh会话之前而不是之后运行命令。但这给了我一个想法,我也许可以以某种方式使前面的过程等待ssh会话结束。尽管ssh进程是LocalCommand的父PID,但事实证明,它并不是那么容易。
但是,我确实找到了在MacOS X下对我有用的东西,并且应该在(其他)BSD(如果不是Linux)上工作。我编写了一个小型C程序,该程序使用该kqueue()
接口等待自己的ppid,然后在该进程退出后运行提供的命令。(下面列出了有兴趣的人的源代码。)
现在,我只需要在~/.ssh/config
文件中引用此程序:
host hp-switch*
PermitLocalCommand yes
LocalCommand ~/bin/wait4parent 'tput smam'
这似乎很好用。那些在Linux上的人……我想您可以通过轮询LocalCommand
的ppid 尝试相同的方法,并希望该pid不会被重用。(请参阅/programming/1157700/how-to-wait-for-exit-of-non-children-processes)
wait4parent.c:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/event.h>
#include <sys/time.h>
int main(int argc, char **argv) {
pid_t ppid, fpid;
struct kevent kev;
int kq;
int kret;
struct timespec timeout;
if ( argc > 2 ) {
fprintf(stderr, "Please quote the command you want to run\n");
exit(-1);
}
ppid = getppid();
fpid = fork();
if ( fpid == -1 ) {
perror("fork");
exit(-1);
}
if ( fpid != 0 ) {
exit(0);
}
EV_SET(&kev, ppid, EVFILT_PROC, EV_ADD, NOTE_EXIT, 0, 0);
kq = kqueue();
if ( kq == -1 ) {
perror("kqueue");
exit(-1);
}
kret = kevent(kq, &kev, 1, NULL, 0, NULL);
if ( kret == -1 ) {
perror("kevent");
exit(-1);
}
timeout.tv_sec = ( 8 /*hours*/ * 60 /*minutes per hour*/ * 60 /*seconds per minute*/ );
timeout.tv_nsec = 0;
kret = kevent(kq, NULL, 0, &kev, 1, &timeout);
if ( kret == -1 ) {
perror("kevent");
exit(-1);
}
if ( kret > 0 ) {
system(argv[1]);
}
/* ( kret == 0 ) means timeout; don't do anything */
exit(0);
}