]> mj.ucw.cz Git - misc.git/blob - netrq.c
Merge branch 'master' of git+ssh://git.ucw.cz/home/mj/GIT/misc
[misc.git] / netrq.c
1 /*
2  *      Send Network SysRq Packet
3  *
4  *      (c) 1997 Martin Mares <mj@atrey.karlin.mff.cuni.cz>
5  */
6
7 #include <stdio.h>
8 #include <string.h>
9 #include <stdlib.h>
10 #include <netdb.h>
11 #include <netinet/in.h>
12 #include <unistd.h>
13
14 struct sysrq_pkt {
15         __u8 password[16];
16         __u8 command;
17 };
18
19 int main(int argc, char **argv)
20 {
21         char *p;
22         struct sysrq_pkt sp;
23         struct hostent *h;
24         int s;
25         struct sockaddr_in sa;
26
27         bzero(&sp, sizeof(sp));
28
29         if (argc != 3 && argc != 4) {
30                 fprintf(stderr, "Usage: netrq <machine> <cmd> [<passwd>]\n");
31                 return 1;
32         }
33
34         if (! (h = gethostbyname(argv[1]))) {
35                 fprintf(stderr, "Unable to resolve machine name: %m\n");
36                 return 1;
37         }
38         if ((s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
39                 fprintf(stderr, "socket: %m\n");
40                 return 1;
41         }
42         sa.sin_family = AF_INET;
43         memcpy(&sa.sin_addr.s_addr, h->h_addr, sizeof(struct in_addr));
44         sa.sin_port = htons(555);
45         if (connect(s, (struct sockaddr *) &sa, sizeof(sa)) < 0) {
46                 fprintf(stderr, "connect: %m\n");
47                 return 1;
48         }
49
50         if (argc > 3)
51                 p = argv[3];
52         else
53                 p = getpass("Enter password: ");
54         if (strlen(p) > 15) {
55                 fprintf(stderr, "Password is too long!\n");
56                 return 1;
57         }
58         strcpy(sp.password, p);
59         sp.command = argv[2][0];
60
61         if (write(s, &sp, sizeof(sp)) != sizeof(sp)) {
62                 fprintf(stderr, "write: %m\n");
63                 return 1;
64         }
65
66         return 0;
67 }