-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBasicRequest.java
More file actions
285 lines (242 loc) · 8.67 KB
/
Copy pathBasicRequest.java
File metadata and controls
285 lines (242 loc) · 8.67 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
package http;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.Authenticator;
import java.net.PasswordAuthentication;
import java.net.Socket;
import java.net.URL;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
/*
*
var lanPara = new Array(
"00-21-27-3D-9A-C2", "192.168.1.1", 2,
0,0 );
var wanPara = new Array(
0, "00-21-27-3D-9A-C3", "222.95.250.89", 3, "255.255.255.255", 0, 0, "222.95.250.89", 0, 1, 0, "218.2.135.1 , 61.147.37.1", "1 day(s) 02:30:28", "Connected", "", 0,
0,0 );
var wlanPara = new Array(
1, "KKK", 10, 2, "00-21-27-3D-9A-C2", "192.168.1.1",
10,
0,0 );
*/
public class BasicRequest {
private static final String USER = "administrator"; // 用户名
private static final String PASS = "administrator"; // 密码
private static final String URL = "http://192.168.1.1/userRpm/StatusRpm.htm";
private static String wanIP = "";
private static class MyAuthenticator extends Authenticator {
@Override
public PasswordAuthentication getPasswordAuthentication() {
return (new PasswordAuthentication(USER, PASS.toCharArray()));
}
}
private static ScheduledExecutorService execService = Executors.newScheduledThreadPool(1);
public static void main(String[] args) {
// init();
String urlStr = URL;
if (args.length == 1 && args[0] != null && !args[0].isEmpty()) {
urlStr = args[0];
} else if (args.length > 1) {
System.out.println("only support one param: url");
return;
}
final String fs = urlStr;
execService.scheduleWithFixedDelay(new Runnable() {
public void run() {
String ip = getWanIP(fs);
if (!ip.equals(wanIP) && ip.length() > 7) {
System.out.println("ip changed from " + wanIP + " to " + ip);
cacheIP(ip);
try {
sendNotification(ip);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else {
System.out.println("ip not change or ip less than 7." + wanIP + " to " + ip);
}
}
}, 5, 600, TimeUnit.SECONDS);
}
private static void init() {
// TODO Auto-generated method stub
}
private static void cacheIP(String ip) {
wanIP = ip;
// ProcessBuilder pb = new ProcessBuilder("/bin/sh"); // or any other
// program you want to run
//
// Map<String, String> envMap = pb.environment();
//
// envMap.put("WAN_IP", wanIP);
// ProcessBuilder pb = new ProcessBuilder("export WAN_IP=\"" + wanIP +
// "\"");
// try {
// pb.start();
// } catch (IOException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
// System.out.println("new ip:" + System.getenv("WAN_IP"));
// try {
//
// Runtime.getRuntime().exec(new String[]{"export WAN_IP=\"" + wanIP +
// "\""});
// // Runtime.getRuntime().exec("WAN_IP=" + wanIP +
// // ";export WAN_IP ");
// System.out.println("new ip:" + System.getenv("WAN_IP"));
// } catch (IOException e) {
// e.printStackTrace();
// }
}
// private static void sendIP(String ip) {
// SimpleEmail mail = new SimpleEmail();
// mail.setHostName("smtp.163.com");
// // mail.setTLS(true);
// mail.setSmtpPort(25);
// String acount = "hkw1113@163.com";
// // mail.setAuthentication("kewen.h@qq.com", "Msgnm!!1"); // Input email
// mail.setAuthentication(acount, "8781811hsyHKW"); // Input email
// // id/pass
// String result = "Failure";
// try {
// mail.addTo(acount);
// mail.setFrom(acount);
// mail.setSubject("new ip:" + ip);
// mail.setMsg("ip=" + ip);
// result = mail.send();
// } catch (Exception e) {
// e.printStackTrace();
// }
// System.out.println("Result: " + result);
// }
private static void sendNotification(String ip) {
// System.out.println("send ip");
String acount = "hkw1113@163.com";
String server = "smtp.163.com";
String toAddr = "61477785@qq.com";
// String toAddr = acount;
String helo = "HELO " + server;
String auth = "AUTH LOGIN";
String username = new sun.misc.BASE64Encoder()
.encode(acount.getBytes());
String password = new sun.misc.BASE64Encoder().encode("8781811hsyHKW"
.getBytes());
// System.out.println(username+"/t"+password);
String mailFrom = "MAIL FROM:<" + acount + ">";
String rcpt = "RCPT TO:<" + toAddr + ">";
String data = "DATA";
String subject = "Subject:ip=" + ip;
String from = "From:<" + acount + ">";
String to = "To:<" + toAddr + ">";
// String contxt01 = "null";
// String contxt02 = "this progrom is writed by qinlouke";
//
// String contxt03 = "thanks";
String end = ".";
String quit = "QUIT";
Socket client = null;
BufferedReader buf = null;
PrintStream out = null;
try {
client = new Socket(server, 25);
out = new PrintStream(client.getOutputStream());
buf = new BufferedReader(new InputStreamReader(
client.getInputStream()));
out.println(helo);
System.out.println("response:" + buf.readLine());
out.println(auth);
System.out.println("response:" + buf.readLine());
out.println(username);
System.out.println("response:" + buf.readLine());
out.println(password);
System.out.println("response:" + buf.readLine());
out.println(mailFrom);
System.out.println("response:" + buf.readLine());
out.println(rcpt);
System.out.println("response:" + buf.readLine());
out.println(data);
System.out.println("response:" + buf.readLine());
out.println(subject);
out.println(from);
out.println(to);
out.println("");
out.println("");
out.println("");
// out.println(contxt01);
// out.println(contxt02);
// out.println(contxt03);
out.println(end);
System.out.println("response:" + buf.readLine());
out.println(quit);
System.out.println("response:" + buf.readLine());
} catch (Exception e) {
// TODO: handle exception
} finally {
if (buf != null) {
try {
buf.close();
} catch (IOException e) {
}
}
if (out != null) {
out.close();
}
if (client != null) {
try {
client.close();
} catch (IOException e) {
}
}
}
}
private static String getWanIP(String urlStr) {
String strWan = getList(urlStr);
String[] arr = strWan.split(",");
if (arr.length > 3) {
String tmp = arr[2].trim();
return tmp.substring(1, tmp.length() - 1);
}
return "";
}
private static String getList(String urlStr) {
InputStream ins = null;
BufferedReader reader = null;
try {
Authenticator.setDefault(new MyAuthenticator());
URL url;
url = new URL(urlStr);
ins = url.openConnection().getInputStream();
reader = new BufferedReader(new InputStreamReader(ins));
String str;
while ((str = reader.readLine()) != null) {
// System.out.println(str);
if (str.contains("var wanPara")) {
return reader.readLine();
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
}
}
if (ins != null) {
try {
ins.close();
} catch (IOException e) {
}
}
}
return "";
}
}