5 int eof; /* send an eof if true */
6 int crtonl; /* convert all received \r to \n */
7 int returns; /* strip \r on reception */
8 char *note = "die: yankee dog";
9 char *ruser; /* for BSD authentication */
12 void rex(int, char*, char*);
13 void tcpexec(int, char*, char*);
14 int call(char *, char*, char*, char**);
15 char *buildargs(char*[]);
17 void error(char*, char*);
18 void sshexec(char*, char*);
23 fprint(2, "usage: %s [-e] [-T] [-r] [-k keypattern] [-l user] net!host command...\n", argv0);
28 main(int argc, char *argv[])
30 char *host, *addr, *args;
51 ruser = EARGF(usage());
60 args = buildargs(&argv[1]);
62 /* try rexexec p9any then dial again with p9sk2 */
63 fd = call(0, host, "rexexec", &addr);
65 rex(fd, args, "p9any");
67 fd = call(0, host, "rexexec", &addr);
69 rex(fd, args, "p9sk2");
72 /* if there's an ssh port, try that */
73 fd = call("tcp", host, "ssh", &addr);
77 /* falls through if no ssh */
80 /* specific attempts */
81 fd = call("tcp", host, "shell", &addr);
83 tcpexec(fd, addr, args);
85 error("can't dial", host);
90 call(char *net, char *host, char *service, char **na)
92 *na = netmkaddr(host, net, service);
93 return dial(*na, 0, 0, 0);
97 rex(int fd, char *cmd, char *proto)
103 ai = auth_proxy(fd, auth_getkey, "proto=%s role=client %s", proto, key);
105 if(strcmp(proto, "p9any") == 0)
107 error("auth_proxy", nil);
109 write(fd, cmd, strlen(cmd)+1);
112 while((n=read(fd, buf, sizeof buf))>0)
113 if(write(1, buf, n)!=n)
114 error("write error", 0);
116 postnote(PNPROC, kid, note);/**/
121 tcpexec(int fd, char *addr, char *cmd)
123 char *cp, *ep, *u, *ru, buf[4096];
127 * do the ucb authentication and send command
133 if(write(fd, "", 1)<0 || write(fd, u, strlen(u)+1)<0
134 || write(fd, ru, strlen(ru)+1)<0 || write(fd, cmd, strlen(cmd)+1)<0){
136 error("can't authenticate to", addr);
140 * get authentication reply
142 if(read(fd, buf, 1) != 1){
144 error("can't authenticate to", addr);
147 while(read(fd, buf, 1) == 1){
153 error("rejected by", addr);
157 while((n=read(fd, buf, sizeof buf))>0){
159 /* convert cr's to nl's */
160 for (cp = buf; cp < buf + n; cp++)
165 /* convert cr's to null's */
168 while(cp < ep && (cp = memchr(cp, '\r', ep-cp))){
169 memmove(cp, cp+1, ep-cp-1);
174 if(write(1, buf, n)!=n)
175 error("write error", 0);
178 postnote(PNPROC, kid, note);/**/
183 sshexec(char *host, char *cmd)
200 exec("/bin/ssh", argv);
209 switch(kid = fork()){
211 error("fork error", 0);
217 while((n=read(0, buf, sizeof buf))>0)
218 if(write(fd, buf, n)!=n)
219 exits("write error");
224 return 0; /* to keep compiler happy */
228 error(char *s, char *z)
231 fprint(2, "%s: %s: %r\n", argv0, s);
233 fprint(2, "%s: %s %s: %r\n", argv0, s, z);
238 buildargs(char *argv[])
247 m = strlen(*argv) + 1;
248 args = realloc(args, n+m +1);
250 error("malloc fail", 0);
251 args[n] = ' '; /* smashes old null */
252 strcpy(args+n+1, *argv);