/
ipcheck.py
1831 lines (1616 loc) · 55.7 KB
/
ipcheck.py
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/python
import base64, getopt, urllib, httplib, os, re, sys, stat, string, time, telnetlib
try:
import syslog
except: # for platforms without syslog that try to use --syslog option
class fake_syslog:
def openlog(self,foo):
raise Exception("Syslog not supported on this platform")
def syslog(self,foo):
raise Exception("Syslog not supported on this platform")
syslog = fake_syslog()
#
# ipcheck.py
#
# Copyright GNU GENERAL PUBLIC LICENSE Version 2
# http://www.gnu.org/copyleft/gpl.html
#
# Author : Kal <kal@users.sourceforge.net>
#
# Acknowledgements
# ================
#
# dyndns crew -a great service, reliable and professional
# bgriggs@pobox.com -ls_dyndns.py client
# zweije@xs4all.nl -HTTP Date header idea for wuHHMM codes
# todd.r@rocketmail.com -Various suggestions and Linksys support
# yminsky@cs.cornell.edu -syslog patch and RT311 support
# KCHANCELLOR@nc.rr.com -RT311 tests
# Johannes Maslowski -Draytek Vigor support
# Ulf Axelsson -option -d fixes
# Del Hodge -Netopia R9100 support
# Jan Bjorvik -Cisco support
# Robert Towster -SMC barricade
# Onno Kortmann -acctfile security suggestion
# Greg Bentz -Linksys fixes for firmware 1.37
#
# global constants
#
Version = "0.67"
Dyndnshost = "www.ovh.com"
Dyndnsnic = "/nic/update"
Useragent = "ipcheck/" + Version
Touchage = 25 # days after which to force an update
Linuxip = "/sbin/ifconfig" # ifconfig command under linux
Win32ip = "ipconfig /all" # ipconfig command under win32
#
# Linksys router support details from ls_dyndns.py bgriggs@pobox.com
#
# leave Linksys_host = "" to autodetect via the default route
# enter an ip here to skip the autodetect
#
Linksys_host = ""
Linksys_user = " "
Linksys_page = "/Status.htm"
#password is specified by command line option -L
#
# Netgear router support
#
Netgear_host = ""
Netgear_user = "admin"
Netgear_page = "/mtenSysStatus.html"
#password is specified by command line option -N
#
# Draytek Vigor2000 router support
#
Draytek_host = ""
Draytek_user = "admin"
Draytek_page = "/doc/digisdn.sht"
#password is specified by command line option -D
#
# Netopia R9100 router support
#
Netopia_host = ""
Netopia_user = ""
Netopia_page = "/WanEvtLog"
#password is specified by command line option -O
#
# Cisco router support
# uses telnet with no user name
#
Cisco_host = ""
Cisco_user = ""
Cisco_page = ""
#password is specified by command line option -C
#
# SMC Barricade
#
SMC_host = ""
SMC_page = "/status.htm"
#user and password are not needed
# regular expression for address
Addressgrep = re.compile ('\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}')
def Usage():
print
print "Usage : ipcheck.py [options] Username Password Hostnames"
print "or ipcheck.py [options] --acctfile <dyndns account info file> "
print
print "Options: -a address manually specify the address "
print " -b backup mx option ON (default OFF) "
print " -d dir directory for data files (default current)"
print " -e script execute script after a successful update "
print " -f force update regardless of current state "
print " -g NAT router, let dyndns guess your IP "
print " (do not use this in a cronjob, try -r) "
print " -h print the detailed help text "
print " --help print both usage and detailed help text "
print " -i interface interface for local address (default ppp0) "
print " -l log debugging text to ipcheck.log file "
print " --syslog log debugging text to syslog (Unix only) "
print " -m mxhost mx host to send if -b (default none) "
print " -o set dyndns offline mode "
print " -p proxy bypass on port 8245 "
print " -r URL NAT router, use web IP detection "
print " -s static dns option (default dynamic) "
print " -v verbose mode "
print " -w wildcard mode ON (default OFF) "
print " -L password Linksys (BEFSR41) NAT router password "
print " -N password Netgear (RT311) NAT router password "
print " -D password Draytek (Vigor2000) NAT router password "
print " -O password Netopia (R9100) NAT router password "
print " -C password Cisco (667i) DSL router password "
print " -U username override default NAT router username "
print " leave this option out to use the default "
print " -S SMC Barricade (no password needed) "
print
print "Note you will have to set the -i interface option if your "
print "link to the internet is not on ppp0 (the default). "
print
print "Hostnames can be a comma separated list (no spaces). Example: "
print "python ipcheck.py username password host1.dyndns.org,host2.dyndns.org "
print
print "You can place your username password and hostnames in a file "
print "(all on the first line) and use the --acctfile option if you do "
print "not wish your password to show up on a ps. "
def Help():
print
print "Start ipcheck.py with no arguments to get the options screen."
print
print "If -f is set, all hosts will be updated regardless of the "
print "current error, wait or IP state. You should never need this. "
print
print "The file ipcheck.dat contains the IP address and hostnames "
print "used in the last update. If the ipcheck.dat file is older "
print "than " + `Touchage` + " days, an update will be made to touch "
print "the hostnames with dyndns. "
print
print "The best way to run ipcheck is in the /etc/ppp/ip-up.local file "
print "but this won't work for many setups. The script will run from "
print "a cronjob. Just make sure the hostnames are the same in each "
print "execution. Also, you should make sure it is ran from the same "
print "directory each time or use the -d option to specify the directory "
print "where data and error files should be placed. "
print
print "The file ipcheck.wait is created if dyndns requests a wait "
print "before retrying. This file will be automatically removed when "
print "the wait is no longer in effect. "
print
print "The file ipcheck.err is created if dyndns responds with an error. "
print "Ipcheck will not try to update again until this error is resolved. "
print "You must remove the file yourself once the problem is fixed. "
print
print "If your ISP has a badly behaved transparent proxy on port 80 "
print "traffic, you can try the -p option to use port 8245. "
print
print "If a http message is sent to dyndns.org, the response will be "
print "saved in the ipcheck.html file."
print
print "Natively Supported NAT Routers:"
print "Ipcheck will locate the IP of your router automatically by "
print "looking at the default route of the machine you are running on. "
print "Then Ipcheck will read the status page for the WAN IP address "
print "and use that for updates. You must specify the admin password "
print "with the appropriate option. Currently supported devices are: "
print "-N netgear, -L linksys, -D draytek, -O netopia and -C cisco. "
print
print "If your have an unsupported device and are willing to help with "
print "some testing, email me and I will look into adding support for it. "
print
print "Other NAT Routers:"
print "You can use a specific web based IP detection via -r URL. Example:"
print "python ipcheck.py -r checkip.dyndns.org:8245 ... "
print "where ... = username password hostnames "
print "Do not schedule this job more often than once every 15 mintes "
print "because it is costing dyndns bandwidth. "
print
print "You can also quickly get an update done by using the -g option "
print "to let dyndns guess your IP. The guessed IP will be saved in the "
print "ipcheck.dat file. DO NOT RUN THE -g OPTION FROM A CRONJOB. "
print "Your local IP will not be the same as the guessed IP resulting "
print "in abusive updates of your hostnames. "
print
print "Win32 Systems: "
print "1) install python from www.python.org "
print "2) copy the ipcheck.py file to the python directory "
print "3) set the interface parameter to some text that appears "
print "before the correct address in the output of ipconfig /all "
print
print "For example you can use the model name or the mac address: "
print "python ipcheck.py -i LNE100TX username password hostname "
print "python ipcheck.py -i 4A-C9-1F-3E-A3-E7 username password hostname "
print
print "The ipcheck homepage can be found at:"
print "http://ipcheck.sourceforge.net/"
print
print "Client development information can be found at:"
print "http://support.dyndns.org/dyndns/clients/devel/"
class Logger:
#
# open a new log file in the target dir if logging
# a race condition if there are tons of scripts
# starting at the same time and should really use locking
# but that would be overkill for this app
#
def __init__(self, logname = "ipcheck.log", verbose = 0, logging = 0, use_syslog = 0):
self.logname = logname
self.verbose = verbose
self.logging = logging
self.syslog = use_syslog
self.prefix = "ipcheck.py: "
if self.syslog == 1:
syslog.openlog("ipcheck")
if self.logging == 1:
self.logfp = open(self.logname, "w")
self.logfp.write(Useragent + "\n")
self.logfp.write(self.prefix + "logging to " + self.logname + "\n")
self.logfp.close()
# normal logging message
def logit(self, logline):
if self.verbose:
print self.prefix + logline
if self.logging:
self.logfp = open(self.logname, "a")
self.logfp.write(self.prefix + logline + "\n")
self.logfp.close()
if self.syslog:
syslog.syslog(logline)
# logging message that gets printed even if not verbose
def logexit(self, logline):
print self.prefix + logline
if self.logging:
self.logfp = open(self.logname, "a")
self.logfp.write(self.prefix + logline + "\n")
self.logfp.close()
if self.syslog:
syslog.syslog(logline)
def DefaultRoute(logger, Tempfile):
iphost = ""
if sys.platform == "win32":
logger.logit("WIN32 default route detection for router.")
os.system ("route print " + " > " + Tempfile)
fp = open(Tempfile, "r")
while 1:
fileline = fp.readline()
if not fileline:
fp.close()
break
p1 = string.find(fileline, "0.0.0.0")
if p1 != -1:
#
# replacing findall to support older python 1.5.1 sites
#
#ipmatch = Addressgrep.findall(fileline)
#if ipmatch != None:
# if len(ipmatch) > 2:
# iphost = ipmatch[2]
ipmatch = Addressgrep.search(fileline)
ip1 = ipmatch.group()
p1 = string.find(fileline, ip1) + len(ip1)
ipmatch = Addressgrep.search(fileline, p1)
ip2 = ipmatch.group()
p2 = string.find(fileline, ip2) + len(ip2)
ipmatch = Addressgrep.search(fileline, p2)
iphost = ipmatch.group()
else:
logger.logit("Linux default route detection for router.")
os.system ("/sbin/route -n " + " > " + Tempfile)
fp = open(Tempfile, "r")
while 1:
fileline = fp.readline()
if not fileline:
fp.close()
break
p1 = string.find(fileline, "UG")
if p1 != -1:
#
# replacing findall to support older python 1.5.1 sites
#
#ipmatch = Addressgrep.findall(fileline)
#if ipmatch != None:
# if len(ipmatch) > 1:
# iphost = ipmatch[1]
ipmatch = Addressgrep.search(fileline)
ip1 = ipmatch.group()
p1 = string.find(fileline, ip1) + len(ip1)
ipmatch = Addressgrep.search(fileline, p1)
iphost = ipmatch.group()
return iphost
if __name__=="__main__":
#
# default options
#
opt_address = ""
opt_force = 0
opt_logging = 0
opt_syslog = 0
opt_verbose = 0
opt_hostnames = ""
opt_interface = "ppp0"
opt_username = ""
opt_password = ""
opt_static = 0
opt_wildcard = 0
opt_backupmx = 0
opt_mxhost = ""
opt_proxy = 0
opt_router = ""
opt_guess = 0
opt_offline = 0
opt_execute = ""
opt_directory = ""
opt_acctfile = ""
opt_natuser = ""
opt_Linksys_password = ""
opt_Netgear_password = ""
opt_Draytek_password = ""
opt_Netopia_password = ""
opt_Cisco_password = ""
opt_SMC_router = 0
#
# parse the command line options
#
if len(sys.argv) == 1:
Usage()
sys.exit(0)
try:
opts, args = getopt.getopt(sys.argv[1:], "a:bd:e:hi:flm:pr:svwgL:oN:U:D:O:C:S", ["syslog", "acctfile=", "help"])
except getopt.error, reason:
print reason
sys.exit(-1)
#
# check verbose, logging and detailed help options first
# check directory to place logging file
#
for opt in opts:
(lopt, ropt) = opt
if lopt == "-l":
opt_logging = 1
elif lopt == "--syslog":
opt_syslog = 1
elif lopt == "-v":
opt_verbose = 1
elif lopt == "--help":
Usage()
Help()
sys.exit(0)
elif lopt == "-h":
Help()
sys.exit(0)
elif lopt == "-d":
if os.path.isdir(ropt):
opt_directory = ropt
else:
print "bad directory option"
sys.exit()
# fix the dir name to end in slash
if opt_directory[-1:] != "/":
opt_directory = opt_directory + "/"
#
# create the logger object
#
if opt_directory == "":
logger = Logger("ipcheck.log", opt_verbose, opt_logging, opt_syslog)
else:
logger = Logger(opt_directory + "ipcheck.log", opt_verbose, opt_logging, opt_syslog)
logline = "opt_directory set to " + opt_directory
logger.logit(logline)
#
# check acctfile option
#
for opt in opts:
(lopt, ropt) = opt
if lopt == "--acctfile":
opt_acctfile = ropt
logline = "opt_acctfile set to " + opt_acctfile
logger.logit(logline)
if len(args) != 3 and opt_acctfile == "":
Usage()
sys.exit(0)
#
# okay now parse rest of the options and log as needed
#
for opt in opts:
(lopt, ropt) = opt
if lopt == "-a":
opt_address = ropt
logline = "opt_address set to " + opt_address
logger.logit(logline)
elif lopt == "-i":
opt_interface = ropt
logline = "opt_interface set to " + opt_interface
logger.logit(logline)
elif lopt == "-f":
opt_force = 1
logline = "opt_force set "
logger.logit(logline)
elif lopt == "-w":
opt_wildcard = 1
logline = "opt_wildcard set "
logger.logit(logline)
elif lopt == "-s":
opt_static = 1
logline = "opt_static set "
logger.logit(logline)
elif lopt == "-b":
opt_backupmx = 1
logline = "opt_backupmx set "
logger.logit(logline)
elif lopt == "-p":
opt_proxy = 1
logline = "opt_proxy set "
logger.logit(logline)
elif lopt == "-m":
opt_mxhost = ropt
logline = "opt_mxhost set to " + opt_mxhost
logger.logit(logline)
elif lopt == "-r":
opt_router = ropt
logline = "opt_router set to " + opt_router
logger.logit(logline)
elif lopt == "-g":
opt_guess = 1
logline = "opt_guess set "
logger.logit(logline)
elif lopt == "-o":
opt_offline = 1
logline = "opt_offline set "
logger.logit(logline)
elif lopt == "-e":
opt_execute = ropt
logline = "opt_execute set to " + opt_execute
logger.logit(logline)
elif lopt == "-U":
opt_natuser = ropt
logline = "opt_natuser set to " + opt_natuser
logger.logit(logline)
Linksys_user = opt_natuser
Netgear_user = opt_natuser
Draytek_user = opt_natuser
Netopia_user = opt_natuser
Cisco_user = opt_natuser
elif lopt == "-L":
opt_Linksys_password = ropt
logline = "opt_Linksys_password = "
for x in xrange(0, len(ropt)):
logline = logline + "*"
logger.logit(logline)
elif lopt == "-N":
opt_Netgear_password = ropt
logline = "opt_Netgear_password = "
for x in xrange(0, len(ropt)):
logline = logline + "*"
logger.logit(logline)
elif lopt == "-D":
opt_Draytek_password = ropt
logline = "opt_Draytek_password = "
for x in xrange(0, len(ropt)):
logline = logline + "*"
logger.logit(logline)
elif lopt == "-O":
opt_Netopia_password = ropt
logline = "opt_Netopia_password = "
for x in xrange(0, len(ropt)):
logline = logline + "*"
logger.logit(logline)
elif lopt == "-C":
opt_Cisco_password = ropt
logline = "opt_Cisco_password = "
for x in xrange(0, len(ropt)):
logline = logline + "*"
logger.logit(logline)
elif lopt == "-S":
opt_SMC_router = 1
logline = "opt_SMC_router set "
logger.logit(logline)
#
# store the command line arguments
#
if opt_acctfile != "":
try:
fp = open (opt_acctfile, "r")
acctdata = fp.read()
fp.close()
except:
logline = "Bad acctfile: " + opt_acctfile
logger.logexit(logline)
sys.exit(-1)
args = string.split(acctdata)
opt_username = args[0]
logline = "opt_username = " + opt_username
logger.logit(logline)
opt_password = args[1]
logline = "opt_password = "
for x in xrange(0, len(opt_password)):
logline = logline + "*"
logger.logit(logline)
opt_hostnames = args[2]
logline = "opt_hostnames = " + opt_hostnames
logger.logit(logline)
hostnames = string.split(opt_hostnames, ",")
#
# taint check, make sure each hostname is a dotted fqdn
#
for host in hostnames:
if string.find(host, ".") == -1:
logline = "Bad hostname: " + host
logger.logexit(logline)
sys.exit(-1)
#
# taint check the mx host
#
if opt_mxhost != "":
if string.find(opt_mxhost, ".") == -1:
logline = "Bad mxhost: " + opt_mxhost
logger.logexit(logline)
sys.exit(-1)
#
# create the full path names
#
Datfile = "ipcheck.dat"
if opt_directory != "":
Datfile = opt_directory + Datfile
Errfile = "ipcheck.err"
if opt_directory != "":
Errfile = opt_directory + Errfile
Waitfile = "ipcheck.wait"
if opt_directory != "":
Waitfile = opt_directory + Waitfile
Htmlfile = "ipcheck.html"
if opt_directory != "":
Htmlfile = opt_directory + Htmlfile
Tempfile = "ipcheck.tmp"
if opt_directory != "":
Tempfile = opt_directory + Tempfile
#
# determine the local machine's ip
#
localip = ""
if opt_address != "":
logger.logit("manually setting localip")
localip = opt_address
elif opt_SMC_router != 0:
#
# SMC barricade router ip detection
#
ipdir = SMC_page
#
# determine the router host address
#
iphost = ""
if SMC_host != "":
logger.logit("SMC_host set explicitly.")
iphost = SMC_host
else:
iphost = DefaultRoute(logger, Tempfile)
if iphost == "":
logger.logit("No router ip detected. Assuming 192.168.0.254")
iphost = "192.168.0.254"
else:
logline = "Trying router at " + iphost
logger.logit(logline)
# connect to the router's admin webpage
try:
ipurl = "http://" + iphost + SMC_page
urlfp = urllib.urlopen(ipurl)
ipdata = urlfp.read()
urlfp.close()
except:
logline = "No address found on router at " + iphost
logger.logexit(logline)
sys.exit(-1)
# create an output file of the response
filename = "smc.html"
if opt_directory != "":
filename = opt_directory + filename
fp = open(filename, "w")
fp.write(ipdata)
fp.close()
logger.logit("smc.html file created")
# grab first thing that looks like an IP address
ipmatch = Addressgrep.search(ipdata)
if ipmatch != None:
localip = ipmatch.group()
if localip == "0.0.0.0":
logline = "The router has no WAN IP assigned. "
logger.logexit(logline)
sys.exit(-1)
if localip == "":
logline = "No address found on router "
logger.logexit(logline)
sys.exit(-1)
elif opt_Cisco_password != "":
#
# Cisco router ip detection
#
ipdir = Cisco_page
#
# determine the router host address
#
iphost = ""
if Cisco_host != "":
logger.logit("Cisco_host set explicitly.")
iphost = Cisco_host
else:
iphost = DefaultRoute(logger, Tempfile)
if iphost == "":
logger.logit("No router ip detected. Assuming 192.168.10.5")
iphost = "192.168.10.5"
else:
logline = "Trying router at " + iphost
logger.logit(logline)
# connect to the router's admin webpage
try:
tn = telnetlib.Telnet(iphost)
logger.logit("Creating telnetlib obj done")
tn.read_until("assword:")
logger.logit("Password prompt found")
tn.write(opt_Cisco_password + "\r\n")
logger.logit("opt_Cisco_password sent")
tn.write("show er\r\n")
logger.logit("show er sent")
tn.write("exit\r\n")
logger.logit("exit sent")
ipdata = tn.read_until("Total Number", 1000)
logger.logit("ipdata read")
except:
logline = "No address found on router at " + iphost
logger.logexit(logline)
sys.exit(-1)
# create an output file of the response
filename = "cisco.html"
if opt_directory != "":
filename = opt_directory + filename
fp = open(filename, "w")
fp.write(ipdata)
fp.close()
logger.logit("cisco.html file created")
# look for the negotiated IP in the log
p1 = string.rfind(ipdata, "Negotiated IP Address")
if p1 != -1:
ipmatch = Addressgrep.search(ipdata, p1)
if ipmatch != None:
localip = ipmatch.group()
if localip == "0.0.0.0":
logline = "The router has no WAN IP assigned. "
logger.logexit(logline)
sys.exit(-1)
if localip == "":
logline = "No address found on router with password " + opt_Cisco_password
logger.logexit(logline)
sys.exit(-1)
elif opt_Netopia_password != "":
#
# Netopia router ip detection
#
ipdir = Netopia_page
#
# determine the router host address
#
iphost = ""
if Netopia_host != "":
logger.logit("Netopia_host set explicitly.")
iphost = Netopia_host
else:
iphost = DefaultRoute(logger, Tempfile)
if iphost == "":
logger.logit("No router ip detected. Assuming 192.168.0.1")
iphost = "192.168.0.1"
else:
logline = "Trying router at " + iphost
logger.logit(logline)
# connect to the router's admin webpage
try:
h1 = httplib.HTTP(iphost)
h1.putrequest("GET", Netopia_page)
h1.putheader("USER-AGENT", Useragent)
authstring = base64.encodestring(Netopia_user + ":" + opt_Netopia_password)
authstring = string.replace(authstring, "\012", "")
h1.putheader("AUTHORIZATION", "Basic " + authstring)
h1.endheaders()
errcode, errmsg, headers = h1.getreply()
fp = h1.getfile()
ipdata = fp.read()
fp.close()
except:
logline = "No address found on router at " + iphost
logger.logexit(logline)
sys.exit(-1)
# create an output file of the response
filename = "netopia.html"
if opt_directory != "":
filename = opt_directory + filename
fp = open(filename, "w")
fp.write(ipdata)
fp.close()
logger.logit("netopia.html file created")
# look for local ip in the log
p1 = string.find(ipdata, "local")
if p1 != -1:
ipmatch = Addressgrep.search(ipdata, p1)
if ipmatch != None:
localip = ipmatch.group()
if localip == "0.0.0.0":
logline = "The router has no WAN IP assigned. "
logger.logexit(logline)
sys.exit(-1)
if localip == "":
logline = "No address found on router with password " + opt_Netopia_password
logger.logexit(logline)
sys.exit(-1)
elif opt_Draytek_password != "":
#
# Draytek router ip detection
#
ipdir = Draytek_page
#
# determine the router host address
#
iphost = ""
if Draytek_host != "":
logger.logit("Draytek_host set explicitly.")
iphost = Draytek_host
else:
iphost = DefaultRoute(logger, Tempfile)
if iphost == "":
logger.logit("No router ip detected. Assuming 192.168.1.1")
iphost = "192.168.1.1"
else:
logline = "Trying router at " + iphost
logger.logit(logline)
# connect to the router's admin webpage
try:
h1 = httplib.HTTP(iphost)
h1.putrequest("GET", Draytek_page)
h1.putheader("USER-AGENT", Useragent)
authstring = base64.encodestring(Draytek_user + ":" + opt_Draytek_password)
authstring = string.replace(authstring, "\012", "")
h1.putheader("AUTHORIZATION", "Basic " + authstring)
h1.endheaders()
errcode, errmsg, headers = h1.getreply()
fp = h1.getfile()
ipdata = fp.read()
fp.close()
except:
logline = "No address found on router at " + iphost
logger.logexit(logline)
sys.exit(-1)
# create an output file of the response
filename = "draytek.html"
if opt_directory != "":
filename = opt_directory + filename
fp = open(filename, "w")
fp.write(ipdata)
fp.close()
logger.logit("draytek.html file created")
# grab first thing that looks like an IP address
ipmatch = Addressgrep.search(ipdata)
if ipmatch != None:
localip = ipmatch.group()
if localip == "0.0.0.0":
logline = "The router has no WAN IP assigned. "
logger.logexit(logline)
sys.exit(-1)
if localip == "":
logline = "No address found on router with password " + opt_Draytek_password
logger.logexit(logline)
sys.exit(-1)
elif opt_Netgear_password != "":
#
# Netgear router ip detection
#
ipdir = Netgear_page
#
# determine the router host address
#
iphost = ""
if Netgear_host != "":
logger.logit("Netgear_host set explicitly.")
iphost = Netgear_host
else:
iphost = DefaultRoute(logger, Tempfile)
if iphost == "":
logger.logit("No router ip detected. Assuming 192.168.0.1")
iphost = "192.168.0.1"
else:
logline = "Trying router at " + iphost
logger.logit(logline)
# connect to the router's admin webpage
try:
h1 = httplib.HTTP(iphost)
h1.putrequest("GET", Netgear_page)
h1.putheader("USER-AGENT", Useragent)
authstring = base64.encodestring(Netgear_user + ":" + opt_Netgear_password)
authstring = string.replace(authstring, "\012", "")
h1.putheader("AUTHORIZATION", "Basic " + authstring)
h1.endheaders()
errcode, errmsg, headers = h1.getreply()
fp = h1.getfile()
ipdata = fp.read()
fp.close()
except:
logline = "No address found on router at " + iphost
logger.logexit(logline)
sys.exit(-1)
# create an output file of the response
filename = "netgear.html"
if opt_directory != "":
filename = opt_directory + filename
fp = open(filename, "w")
fp.write(ipdata)
fp.close()
logger.logit("netgear.html file created")
# look for the WAN Port in the log
p1 = string.rfind(ipdata, "WAN Port")
if p1 != -1:
ipmatch = Addressgrep.search(ipdata, p1)
if ipmatch != None:
localip = ipmatch.group()
if localip == "0.0.0.0":
logline = "The router has no WAN IP assigned. "
logger.logexit(logline)
sys.exit(-1)
if localip == "":
logline = "No address found on router with password " + opt_Netgear_password
logger.logexit(logline)
sys.exit(-1)
elif opt_Linksys_password != "":
#
# Linksys router ip detection
#
ipdir = Linksys_page
#
# determine the linksys router host address
#
iphost = ""
if Linksys_host != "":
logger.logit("Linksys_host set explicitly.")
iphost = Linksys_host
else:
iphost = DefaultRoute(logger, Tempfile)
if iphost == "":
logger.logit("No router ip detected. Assuming 192.168.1.1")
iphost = "192.168.1.1"
else:
logline = "Trying linksys router at " + iphost
logger.logit(logline)
# connect to the router's admin webpage
try:
h1 = httplib.HTTP(iphost)
#
# Hack from ls_dyndns.py for authorization.
#
# For some reason the dyndns router won't authenticate when
# using standard headers for authorization. Like this:
#
#h1.putrequest('GET', ipdir)
#authstring = base64.encodestring(Linksys_user + ":" + opt_Linksys_password)
#h1.putheader("Authorization", "Basic " + authstring)
#
# It may be looking for lines that end with just \n not \r\n.
# Anyways here is the snip from ls_dyndns.py
#
# Note: Modified by Greg Bentz.
# Linksys firmware 1.37, doesn't like the trailing \n on the authstring.
# Also requires "\r\n".
# My theory concerning the standard headers is that the Linksys header
# parsing requires all the header info to appear in one packet.
# Use of an extra putheader() call puts the authorization information into
# a second packet which is not read by the Linksys device.
#
authstring = base64.encodestring(Linksys_user + ":" + opt_Linksys_password)
authstring = string.replace(authstring, "\012", "")
ipdir = Linksys_page + ' \r\n' \
+ 'Authorization: Basic ' \
+ authstring + '\r\n'
h1.putrequest('GET', ipdir)
h1.endheaders()
errcode, errmsg, headers = h1.getreply()
fp = h1.getfile()
ipdata = fp.read()
fp.close()
except:
logline = "No address found on router at " + iphost
logger.logexit(logline)
sys.exit(-1)
# create an output file of the linksys response
filename = "linksys.html"
if opt_directory != "":
filename = opt_directory + filename
fp = open(filename, "w")
fp.write(ipdata)
fp.close()