122 lines
2.9 KiB
C
122 lines
2.9 KiB
C
#include <sys/socket.h>
|
|
#include <sys/time.h>
|
|
#include <string.h>
|
|
#include <unistd.h>
|
|
#include <stdio.h>
|
|
#include <netdb.h>
|
|
#include "myc_str.h"
|
|
|
|
int rexec(char **host, int port, char *user, char *passwd, char *command, int *err_file_desc);
|
|
|
|
int wrtRd(int fd, char *cmd, char *res, int res_len) {
|
|
int i, p, l, cnt;
|
|
char buf[8192];
|
|
struct timeval tmo;
|
|
fd_set rmask;
|
|
|
|
if (cmd[0]!='\0') {
|
|
l=send(fd, cmd, strlen(cmd), 0);
|
|
if (l<0) return -1;
|
|
}
|
|
tmo.tv_sec=2;
|
|
i=1;
|
|
p=0;
|
|
while (i>0 && p<sizeof(buf)-1) {
|
|
FD_ZERO(&rmask);
|
|
FD_SET(fd, &rmask);
|
|
tmo.tv_usec=500000;
|
|
i=select(fd+1, &rmask, NULL, NULL, &tmo);
|
|
tmo.tv_sec=0;
|
|
if (i>0) {
|
|
l=recv(fd, buf+p, sizeof(buf)-p-1, 0);
|
|
if (l<0) return -1;
|
|
p=p+l;
|
|
if (l==0) i=0;
|
|
}
|
|
}
|
|
if (p >= res_len) p=res_len-1;
|
|
buf[p]='\0';
|
|
strcpy(res, buf);
|
|
return p;
|
|
}
|
|
|
|
int main(int argc, char *argv[]) {
|
|
int port, i, j, l, fd;
|
|
char *host, inst[32], cmd[256], buf[256], path[256];
|
|
char *instrHosts[]={
|
|
"DMC","lnsa05.psi.ch",
|
|
"TOPSI","lnsa07.psi.ch",
|
|
"SANS","lnsa10.psi.ch",
|
|
"HRPT","lnsa11.psi.ch",
|
|
"TRICS","lnsa13.psi.ch",
|
|
"AMOR","lnsa14.psi.ch",
|
|
"FOCUS","lnsa16.psi.ch",
|
|
"ZOLLIKER","lnsa15.psi.ch"
|
|
};
|
|
struct servent s;
|
|
|
|
s=*getservbyname("exec", "tcp");
|
|
i=0;
|
|
if (argc < 3) {
|
|
printf("not enough arguments\n");
|
|
return 0;
|
|
}
|
|
str_upcase(inst, argv[1]);
|
|
str_copy(cmd, "\n");
|
|
for (i=2; i<argc; i++) {
|
|
if (0==strcmp(argv[i], "sics")) {
|
|
str_append(cmd, "startsics\n");
|
|
} else if (0==strcmp(argv[i], "tecs")) {
|
|
str_append(cmd, "startecs\n");
|
|
} else {
|
|
printf("unknown server %s\n", argv[i]);
|
|
return 0;
|
|
}
|
|
}
|
|
buf[0]='\0';
|
|
host=NULL;
|
|
for (i=0; i<sizeof(instrHosts)/sizeof(instrHosts[0]); i+=2) {
|
|
if (0==strcmp(instrHosts[i], inst)) {
|
|
host=instrHosts[i+1];
|
|
} else if (0==strcmp(instrHosts[i+1], argv[1])) {
|
|
host=argv[1];
|
|
str_copy(inst,instrHosts[i]);
|
|
}
|
|
}
|
|
if (0==strcmp(inst, "ZOLLIKER")) {
|
|
str_copy(buf, "69kilo");
|
|
str_copy(inst, "zolliker");
|
|
} else if (host==NULL) {
|
|
printf("unknown instrument: %s\n", argv[1]);
|
|
return 0;
|
|
}
|
|
printf("login to %s\n", host);
|
|
if (buf[0]=='\0') {
|
|
str_copy(buf, inst);
|
|
str_append(buf, "LNS");
|
|
}
|
|
fd=rexec(&host, s.s_port, inst, buf, "rlogin 0", &i);
|
|
if (fd<0) {
|
|
perror("rexec"); return -1;
|
|
}
|
|
|
|
if (wrtRd(fd, "\n", buf, sizeof(buf)) < 0) goto Error;
|
|
if (wrtRd(fd, "\n", buf, sizeof(buf)) < 0) goto Error;
|
|
if (wrtRd(fd, "\n", buf, sizeof(buf)) < 0) goto Error;
|
|
if (wrtRd(fd, cmd, buf, sizeof(buf)) < 0) goto Error;
|
|
printf("%s\n", buf);
|
|
l=1;
|
|
while (l>0) {
|
|
if (l=wrtRd(fd, "", buf, sizeof(buf)) < 0) goto Error;
|
|
printf("%s", buf);
|
|
}
|
|
printf("\n");
|
|
i=10;
|
|
while (i>0 && wrtRd(fd, "logout\n", buf, sizeof(buf)) >= 0) {
|
|
i--;
|
|
}
|
|
close(fd);
|
|
return 0;
|
|
Error: perror("error in wrtRd"); return 0;
|
|
}
|