Advertisement
symdrome

nm_connect

Jan 23rd, 2024
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.10 KB | Software | 0 0
  1. #include <ctype.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <unistd.h>
  6.  
  7. #define LINE_MAX 255
  8.  
  9. void print_usage() {
  10.     fprintf(stderr,
  11.         "Usage: <nm_connect> [Options]\n      Options:\n\t -l: list "
  12.         "networks\n\t -c: connect <SSID>\n");
  13.        
  14.     exit(1);
  15. }
  16.  
  17. int run_nm(char *cmd, char lines[][LINE_MAX])
  18. {
  19.     FILE *nm;
  20.     char buffer[LINE_MAX];
  21.  
  22.     int cnt = 0;
  23.    
  24.     if ((nm = popen(cmd, "r")) == NULL)
  25.         perror("popen error ....");
  26.  
  27.     while (fgets(buffer, sizeof(buffer), nm) != NULL) {
  28.         strtok(buffer, "\n");
  29.         strcpy(lines[cnt++], buffer);
  30.     }
  31.  
  32.     pclose(nm);
  33.     return cnt;
  34. }
  35.  
  36. void connect(char *ssid, char *pass)
  37. {
  38.     char nmcli[] = "nmcli device wifi";
  39.     char command[256];
  40.     sprintf(command, "%s connect %s password %s", nmcli, ssid, pass);
  41.  
  42.     int statusCode = system(command);
  43.    
  44.     if (statusCode == 0) {
  45.         fprintf(stdout, "connected on %s wifi ssid\n", ssid);
  46.     } else {
  47.         fprintf(stderr, "nmcli command error...");
  48.        
  49.     }
  50. }
  51.  
  52. int main(int argc, char *argv[])
  53. {
  54.     int option;
  55.     char ssid[15];
  56.     char pass[10];
  57.     int lflag = 0;
  58.     int cflag = 0;
  59.     char out[100][LINE_MAX];
  60.     char comm_nm[LINE_MAX] = "nmcli device wifi";
  61.     char cmd[LINE_MAX];
  62.  
  63.     if (argc < 2) {
  64.         print_usage();
  65.     }
  66.    
  67.     while ((option = getopt(argc, argv, "lhc:")) != -1) {
  68.         switch (option) {
  69.         case 'l':
  70.             if (lflag) {
  71.                 print_usage();
  72.             } else {
  73.                 lflag = 1;
  74.             }
  75.             sprintf(cmd, "%s list", comm_nm);
  76.             int a = run_nm(cmd, out);
  77.             printf("\n%55s\n\n", "Available WIFI Networks");
  78.             for (int i = 0; i < a; ++i)
  79.                 printf("%s\n", out[i]);
  80.            
  81.             exit(EXIT_SUCCESS);
  82.         case 'c':
  83.             if (cflag) {
  84.                 print_usage();
  85.             } else {
  86.                 cflag = 1;
  87.             }
  88.             strcpy(ssid, optarg);
  89.             strcpy(pass, getpass("password: "));
  90.  
  91.             connect(ssid, pass);
  92.             exit(EXIT_SUCCESS);
  93.  
  94.         case 'h':
  95.             print_usage();
  96.             exit(EXIT_SUCCESS);
  97.            
  98.         default:
  99.             print_usage();
  100.         }
  101.     }
  102.  
  103.     if (strcmp(argv[optind],"-l") != 0 || strcmp(argv[optind], "-c") != 0 || strcmp(argv[optind], "-h") != 0) {
  104.         fprintf(stderr, "ERROR: Expected options\n");
  105.         puts("");
  106.         print_usage();
  107.     }
  108.  
  109.     return EXIT_SUCCESS;
  110. }
  111.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement