-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyLinuxCommand.cpp
More file actions
75 lines (68 loc) · 2.36 KB
/
MyLinuxCommand.cpp
File metadata and controls
75 lines (68 loc) · 2.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/*------------------------------------------- Sensor LDR Class --------------------------------------------*/
/* This define LinuxCommand class function
*
* Created 2017
* by Nguyen Thanh Long
* Modified 13 April 2017 - version 1
* by Nguyen Thanh Long
*
* Reference:
--> https://www.arduino.cc/en/Reference/StringObject
*
* This code is released in public domains
*/
#include "MyLinuxCommand.h"
#include <Dhcp.h> // Dhcp library from: bjoern@cs.stanford.edu 12/30/2008
#include <Dns.h> // Dns library from: bjoern@cs.stanford.edu 12/30/2008
#include <Ethernet.h> // If you use Ethernet Shield 2, use instead <Ethernet2.h>
#include <EthernetUdp.h> // UDP library from: bjoern@cs.stanford.edu 12/30/2008
#include <SPI.h> // needed for Arduino versions later than 0018
bool LinuxCommand::CheckCommand(String str) {
String tempStr = str;
tempStr.trim();
if ( tempStr.equalsIgnoreCase("ifconfig -a") ) { // Show ifconfig respond
Serial.println("IP address: ");
Serial.println(Ethernet.localIP());
char output[500];
Serial.println("More from log.txt: ");
system("ifconfig > log.txt");
FILE *fp;
fp = fopen("log.txt", "r");
while (fgets(output, 500, fp) != NULL) {
Serial.println(output);
}
fclose(fp);
return true;
} else if ( tempStr.equalsIgnoreCase("netstat") || tempStr.equalsIgnoreCase("netstat -a") ) { // Show netstat respond
char output[500];
Serial.println("Show listening port: ");
system("netstat -a > log.txt");
delay(5000);
FILE *fp;
fp = fopen("log.txt", "r");
while (fgets(output, 500, fp) != NULL) {
Serial.println(output);
}
fclose(fp);
return true;
} else if ( tempStr.equalsIgnoreCase("ls") ) { // Show all file
char output[500];
Serial.println("Show Files: ");
system("ls > log.txt");
delay(1000);
FILE *fp;
fp = fopen("log.txt", "r");
while (fgets(output, 500, fp) != NULL) {
Serial.println(output);
}
fclose(fp);
return true;
} else if ( tempStr.equalsIgnoreCase("reboot") ) { // Reboot the device
Serial.println("System is restarting...");
system("reboot");
Serial.println("Success in restart");
for(;;);
return true;
}
return false;
}