~nilsschlupp/auto-ndiswrapper/rewrite

1 by Gabriel Joel
initial import
1
#!/usr/bin/env python
4 by Gabriel Joel
Auto detection/fetching/install almost ready
2
7 by Gabriel Joel
Took all of wolf's new code, a lot by the way, and added a thing that asks if you agree with the EULA
3
#Auto-NDISwrapper by Nils Schulupp, Gabriel J. Perez and Richard Kaufman
19 by Gabriel Joel
Cleaned some stuff up, general improvements
4
#
7 by Gabriel Joel
Took all of wolf's new code, a lot by the way, and added a thing that asks if you agree with the EULA
5
#This program automatically looks at what Wi-Fi card you have, it disables any wireless driver currently installed, fetches #the correct Windows driver from the Internet and installs it with NDISwrapper.
6
19 by Gabriel Joel
Cleaned some stuff up, general improvements
7
#Copyright (C) 2007, 2008 Gabriel J. Perez <gabrieljoel@gmail.com>, Nils Schulupp <nils.schlupp@gmail.com> and Richard Kaufman <richardbkaufman@gmail.com>
8
#
7 by Gabriel Joel
Took all of wolf's new code, a lot by the way, and added a thing that asks if you agree with the EULA
9
#This program is free software; you can redistribute it and/or modify
10
#it under the terms of the GNU General Public License as published by
11
#the Free Software Foundation; either version 2 of the License, or
12
#(at your option) any later version.
19 by Gabriel Joel
Cleaned some stuff up, general improvements
13
#
7 by Gabriel Joel
Took all of wolf's new code, a lot by the way, and added a thing that asks if you agree with the EULA
14
#This program is distributed in the hope that it will be useful,
15
#but WITHOUT ANY WARRANTY; without even the implied warranty of
16
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
#GNU General Public License for more details.
19 by Gabriel Joel
Cleaned some stuff up, general improvements
18
#
7 by Gabriel Joel
Took all of wolf's new code, a lot by the way, and added a thing that asks if you agree with the EULA
19
#You should have received a copy of the GNU General Public License
20
#along with this program; if not, write to the Free Software
21
#Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22
19 by Gabriel Joel
Cleaned some stuff up, general improvements
23
"""Auto-NDISwrapper -- Linux Wi-Fi as easy as it can get"""
24
7 by Gabriel Joel
Took all of wolf's new code, a lot by the way, and added a thing that asks if you agree with the EULA
25
__version__ = '0.0.1'
1 by Gabriel Joel
initial import
26
23 by Gabriel Joel
Improved the installation method once again, replaced all uses of commands.getoutput with a new function called getoutput that uses subprocess. General improvements
27
import os, platform, sys, re, subprocess
19 by Gabriel Joel
Cleaned some stuff up, general improvements
28
from database import data
7 by Gabriel Joel
Took all of wolf's new code, a lot by the way, and added a thing that asks if you agree with the EULA
29
from ConfigParser import ConfigParser
30
from optparse import OptionParser
31
sys.path.append(os.getcwd())
32
19 by Gabriel Joel
Cleaned some stuff up, general improvements
33
print 'Auto-NDISwrapper %s' % (__version__, )
7 by Gabriel Joel
Took all of wolf's new code, a lot by the way, and added a thing that asks if you agree with the EULA
34
22 by Gabriel Joel
Added some safeguards in case the decompression or the download failed, organized some stuff, overall improvements
35
##Parse options
19 by Gabriel Joel
Cleaned some stuff up, general improvements
36
parser = OptionParser(version='version: '+__version__)
7 by Gabriel Joel
Took all of wolf's new code, a lot by the way, and added a thing that asks if you agree with the EULA
37
parser.add_option('--ndiswrapper-bin', default='/usr/sbin/ndiswrapper', help='default: %default', metavar='FILE')
38
parser.add_option('-t', '--tmp-dir', default='/tmp/auto-ndis', help='default: %default', metavar='DIR')
11 by Gabriel Joel
Added the logfile
39
parser.add_option('-a', '--ask', action='store_true', default=False, help='ask for confirmation')
7 by Gabriel Joel
Took all of wolf's new code, a lot by the way, and added a thing that asks if you agree with the EULA
40
parser.add_option('-q', '--quiet', action='store_true', default=False, help='reduce some output')
41
parser.add_option('--nocheck', action='store_true', default=False, help='doesnt check for ndiswrapper binary (debug only)')
42
parser.add_option('-d', '--debug', action='store_true', default=False, help='turn on debugging output')
43
opts, args = parser.parse_args()
44
9 by Gabriel Joel
Added new feature 'offline mode', added some comments, organized some functions, add new function 'check for internet'
45
##If the user isn't root the program will quit
5 by Gabriel Joel
Some bug fixes I think it is fully working now, havent finished implementing wolf's new code
46
if os.geteuid() != 0:
19 by Gabriel Joel
Cleaned some stuff up, general improvements
47
  	print '[ERROR] You must be root to run this script.'
22 by Gabriel Joel
Added some safeguards in case the decompression or the download failed, organized some stuff, overall improvements
48
	print 'Try, "su then python auto-ndis.py" or "sudo python auto-ndis.py"'
7 by Gabriel Joel
Took all of wolf's new code, a lot by the way, and added a thing that asks if you agree with the EULA
49
	sys.exit(1)
50
9 by Gabriel Joel
Added new feature 'offline mode', added some comments, organized some functions, add new function 'check for internet'
51
##If the user doesn't have ndiswrapper installed the program will quit
7 by Gabriel Joel
Took all of wolf's new code, a lot by the way, and added a thing that asks if you agree with the EULA
52
if not os.path.exists(opts.ndiswrapper_bin) and not opts.nocheck:
19 by Gabriel Joel
Cleaned some stuff up, general improvements
53
	print '[ERROR] NDISwrapper not found!!!'
22 by Gabriel Joel
Added some safeguards in case the decompression or the download failed, organized some stuff, overall improvements
54
	print 'Please look for "ndiswrapper" in your distribution\'s package manager and install it or go to www.ndiswrapper.com and download the latest sources' 
7 by Gabriel Joel
Took all of wolf's new code, a lot by the way, and added a thing that asks if you agree with the EULA
55
	sys.exit(1)
56
23 by Gabriel Joel
Improved the installation method once again, replaced all uses of commands.getoutput with a new function called getoutput that uses subprocess. General improvements
57
##Print the debug options enabled
58
if (opts.debug):
19 by Gabriel Joel
Cleaned some stuff up, general improvements
59
	print '[DEBUG] Options:'
7 by Gabriel Joel
Took all of wolf's new code, a lot by the way, and added a thing that asks if you agree with the EULA
60
	print opts
19 by Gabriel Joel
Cleaned some stuff up, general improvements
61
	print '[DEBUG] Arguments:'
7 by Gabriel Joel
Took all of wolf's new code, a lot by the way, and added a thing that asks if you agree with the EULA
62
	print args
19 by Gabriel Joel
Cleaned some stuff up, general improvements
63
	print
7 by Gabriel Joel
Took all of wolf's new code, a lot by the way, and added a thing that asks if you agree with the EULA
64
23 by Gabriel Joel
Improved the installation method once again, replaced all uses of commands.getoutput with a new function called getoutput that uses subprocess. General improvements
65
def getoutput(*cmd):
66
	"""
67
	Like commands.getoutput, but uses subprocess.
68
	"""
69
70
	myproc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
71
	value  =  myproc.stdout.read()
72
	return value
73
74
def get_output_retcode(*cmd):
75
	
76
	myproc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
77
	value  =  myproc.stdout.read()
78
	retcode = myproc.wait()
79
	return value, retcode
80
81
def get_print_card_info():
82
	"""
83
	Prints the pci id and other information about the pci wireless cards.
84
	"""
85
	output = getoutput('lspci')
86
	
87
	
88
19 by Gabriel Joel
Cleaned some stuff up, general improvements
89
	print '  -- ' + device 
90
	print '      ==> PCI ID = ' + pci_id
10 by Gabriel Joel
Fixed some bugs, and it now prints the card's name when beginning installation
91
	
92
22 by Gabriel Joel
Added some safeguards in case the decompression or the download failed, organized some stuff, overall improvements
93
def check_for_internet(url, card_id):
23 by Gabriel Joel
Improved the installation method once again, replaced all uses of commands.getoutput with a new function called getoutput that uses subprocess. General improvements
94
	"""
95
	Pings google to see if the user has an internet conecction available.
96
	"""
97
	
19 by Gabriel Joel
Cleaned some stuff up, general improvements
98
	print '[*] Looking if an Internet connection is available, this may take a few seconds.'
23 by Gabriel Joel
Improved the installation method once again, replaced all uses of commands.getoutput with a new function called getoutput that uses subprocess. General improvements
99
	test = getoutput('ping', '-c', '1', 'google.com')
19 by Gabriel Joel
Cleaned some stuff up, general improvements
100
	if 'unknown host' in test:
101
		print '[ERROR] An Internet connection was not found'
102
		choice = raw_input('(C)ontinue, (R)etry, (O)ffline mode or (E)xit: ')
10 by Gabriel Joel
Fixed some bugs, and it now prints the card's name when beginning installation
103
		
19 by Gabriel Joel
Cleaned some stuff up, general improvements
104
		if choice == 'C' or choice == 'c':
9 by Gabriel Joel
Added new feature 'offline mode', added some comments, organized some functions, add new function 'check for internet'
105
			return 0
106
		
19 by Gabriel Joel
Cleaned some stuff up, general improvements
107
		elif choice == 'R' or choice == 'r':
22 by Gabriel Joel
Added some safeguards in case the decompression or the download failed, organized some stuff, overall improvements
108
			return check_for_internet(url,card_id)
9 by Gabriel Joel
Added new feature 'offline mode', added some comments, organized some functions, add new function 'check for internet'
109
		
19 by Gabriel Joel
Cleaned some stuff up, general improvements
110
		elif choice == 'O' or choice == 'o':
22 by Gabriel Joel
Added some safeguards in case the decompression or the download failed, organized some stuff, overall improvements
111
			manualmode(url,card_id)
112
			installdriver(card_id)
9 by Gabriel Joel
Added new feature 'offline mode', added some comments, organized some functions, add new function 'check for internet'
113
19 by Gabriel Joel
Cleaned some stuff up, general improvements
114
		elif choice == 'E' or choice == 'e':
9 by Gabriel Joel
Added new feature 'offline mode', added some comments, organized some functions, add new function 'check for internet'
115
			sys.exit(1)
116
		
117
		else:
22 by Gabriel Joel
Added some safeguards in case the decompression or the download failed, organized some stuff, overall improvements
118
			return check_for_internet(url,card_id)
9 by Gabriel Joel
Added new feature 'offline mode', added some comments, organized some functions, add new function 'check for internet'
119
	
120
	else:
19 by Gabriel Joel
Cleaned some stuff up, general improvements
121
		print '[*] Internet connection found, continuing installation'
9 by Gabriel Joel
Added new feature 'offline mode', added some comments, organized some functions, add new function 'check for internet'
122
		return 0
1 by Gabriel Joel
initial import
123
23 by Gabriel Joel
Improved the installation method once again, replaced all uses of commands.getoutput with a new function called getoutput that uses subprocess. General improvements
124
def searchcard(devices_list, database):
125
	"""
126
	Looks if one of the ids in the devices list given as argument matches an entry in the database given as argument. 
127
	"""
128
		
129
	for item in devices_list:
21 by Gabriel Joel
Some cosmetic changes here and there
130
		if item in database:
131
			return item
23 by Gabriel Joel
Improved the installation method once again, replaced all uses of commands.getoutput with a new function called getoutput that uses subprocess. General improvements
132
	return -1
7 by Gabriel Joel
Took all of wolf's new code, a lot by the way, and added a thing that asks if you agree with the EULA
133
	 
134
def remove_old_drivers():
23 by Gabriel Joel
Improved the installation method once again, replaced all uses of commands.getoutput with a new function called getoutput that uses subprocess. General improvements
135
        """
136
	Removes existing wireless drivers.
137
	"""
138
19 by Gabriel Joel
Cleaned some stuff up, general improvements
139
	print '[*] Removing Old Drivers:'
22 by Gabriel Joel
Added some safeguards in case the decompression or the download failed, organized some stuff, overall improvements
140
        baddrivers = yourcard_dic['blacklist']
23 by Gabriel Joel
Improved the installation method once again, replaced all uses of commands.getoutput with a new function called getoutput that uses subprocess. General improvements
141
        if baddrivers in getoutput('lsmod| awk "{print $1}"'):
19 by Gabriel Joel
Cleaned some stuff up, general improvements
142
                print '   removing %s' % (baddrivers, )
23 by Gabriel Joel
Improved the installation method once again, replaced all uses of commands.getoutput with a new function called getoutput that uses subprocess. General improvements
143
                subprocess.call(['rmmod', baddrivers])
7 by Gabriel Joel
Took all of wolf's new code, a lot by the way, and added a thing that asks if you agree with the EULA
144
        else:
19 by Gabriel Joel
Cleaned some stuff up, general improvements
145
                print '   drivers not loaded, nothing to remove'
7 by Gabriel Joel
Took all of wolf's new code, a lot by the way, and added a thing that asks if you agree with the EULA
146
147
def blacklist_drivers():
23 by Gabriel Joel
Improved the installation method once again, replaced all uses of commands.getoutput with a new function called getoutput that uses subprocess. General improvements
148
        """
149
	Blacklists existing wireless drivers.
150
	"""
151
19 by Gabriel Joel
Cleaned some stuff up, general improvements
152
	print '[*] Blacklisting:'
22 by Gabriel Joel
Added some safeguards in case the decompression or the download failed, organized some stuff, overall improvements
153
        baddrivers = yourcard_dic['blacklist']
7 by Gabriel Joel
Took all of wolf's new code, a lot by the way, and added a thing that asks if you agree with the EULA
154
        if any(baddrivers in line.split('#')[0] for line in open('/etc/modprobe.d/blacklist')):
19 by Gabriel Joel
Cleaned some stuff up, general improvements
155
                print '   driver already blacklisted, skipping'
7 by Gabriel Joel
Took all of wolf's new code, a lot by the way, and added a thing that asks if you agree with the EULA
156
        else:
19 by Gabriel Joel
Cleaned some stuff up, general improvements
157
                print '   writing to /etc/modprobe.d/blacklist'
7 by Gabriel Joel
Took all of wolf's new code, a lot by the way, and added a thing that asks if you agree with the EULA
158
                blacklist = open('/etc/modprobe.d/blacklist', 'a')
159
                blacklist.write('\n#%s' % (baddrivers, ))
160
                blacklist.close()
161
22 by Gabriel Joel
Added some safeguards in case the decompression or the download failed, organized some stuff, overall improvements
162
def print_driver_license():
23 by Gabriel Joel
Improved the installation method once again, replaced all uses of commands.getoutput with a new function called getoutput that uses subprocess. General improvements
163
	"""
164
	Checks if the user agrees with the driver's EULA.
165
	"""
166
	
19 by Gabriel Joel
Cleaned some stuff up, general improvements
167
	print 'Do you agree with this proprietary driver\'s End User License Agreement? You can find it in the website of your card\'s manufacturer.'
168
	choice = raw_input('(Y)es or (N)o, if you don\'t agree the program will quit: ')
7 by Gabriel Joel
Took all of wolf's new code, a lot by the way, and added a thing that asks if you agree with the EULA
169
	
19 by Gabriel Joel
Cleaned some stuff up, general improvements
170
	if choice == 'y' or choice == 'Y':
7 by Gabriel Joel
Took all of wolf's new code, a lot by the way, and added a thing that asks if you agree with the EULA
171
		return 0
19 by Gabriel Joel
Cleaned some stuff up, general improvements
172
	elif choice == 'n' or choice == 'N':
7 by Gabriel Joel
Took all of wolf's new code, a lot by the way, and added a thing that asks if you agree with the EULA
173
		sys.exit(1)
174
	else:
175
		return driver_license()
176
10 by Gabriel Joel
Fixed some bugs, and it now prints the card's name when beginning installation
177
def create_auto_ndis_folder():
23 by Gabriel Joel
Improved the installation method once again, replaced all uses of commands.getoutput with a new function called getoutput that uses subprocess. General improvements
178
	"""
179
	Creates a folder in which place the downloaded drivers.
180
	"""
181
9 by Gabriel Joel
Added new feature 'offline mode', added some comments, organized some functions, add new function 'check for internet'
182
	try:
22 by Gabriel Joel
Added some safeguards in case the decompression or the download failed, organized some stuff, overall improvements
183
		os.makedirs('%s/%s' % (opts.tmp_dir, card_id, ))
9 by Gabriel Joel
Added new feature 'offline mode', added some comments, organized some functions, add new function 'check for internet'
184
		print 'Tmpdir created'
185
		os.chmod(opts.tmp_dir,0777) 
22 by Gabriel Joel
Added some safeguards in case the decompression or the download failed, organized some stuff, overall improvements
186
		os.chdir('%s/%s' % (opts.tmp_dir, card_id, ))
9 by Gabriel Joel
Added new feature 'offline mode', added some comments, organized some functions, add new function 'check for internet'
187
	except:
22 by Gabriel Joel
Added some safeguards in case the decompression or the download failed, organized some stuff, overall improvements
188
		if os.path.exists('%s/%s' % (opts.tmp_dir, card_id, )):
9 by Gabriel Joel
Added new feature 'offline mode', added some comments, organized some functions, add new function 'check for internet'
189
			print 'Tmpdir exists and ok, continuing'
22 by Gabriel Joel
Added some safeguards in case the decompression or the download failed, organized some stuff, overall improvements
190
			os.chdir('%s/%s' % (opts.tmp_dir, card_id, ))
9 by Gabriel Joel
Added new feature 'offline mode', added some comments, organized some functions, add new function 'check for internet'
191
		else:
192
			print '[ERROR] cant create tmpdir'
193
			print '   Either specify an alternative or ensure that /tmp is writable and not full'
194
			sys.exit(1)
195
21 by Gabriel Joel
Some cosmetic changes here and there
196
def find_decompression(driver_url):
23 by Gabriel Joel
Improved the installation method once again, replaced all uses of commands.getoutput with a new function called getoutput that uses subprocess. General improvements
197
	"""
198
	Determines the right decompression program to be used with the downloaded driver.
199
	"""
200
19 by Gabriel Joel
Cleaned some stuff up, general improvements
201
	decompression = 'manual'
9 by Gabriel Joel
Added new feature 'offline mode', added some comments, organized some functions, add new function 'check for internet'
202
	
21 by Gabriel Joel
Some cosmetic changes here and there
203
	if '.exe' in driver_url or '.zip' in driver_url:
22 by Gabriel Joel
Added some safeguards in case the decompression or the download failed, organized some stuff, overall improvements
204
		decompression = 'unzip'
7 by Gabriel Joel
Took all of wolf's new code, a lot by the way, and added a thing that asks if you agree with the EULA
205
		
21 by Gabriel Joel
Some cosmetic changes here and there
206
	elif '.tar' in driver_url:
22 by Gabriel Joel
Added some safeguards in case the decompression or the download failed, organized some stuff, overall improvements
207
		decompression = 'tar -vvf'
19 by Gabriel Joel
Cleaned some stuff up, general improvements
208
	
21 by Gabriel Joel
Some cosmetic changes here and there
209
	elif '.tar.gz' in driver_url:
22 by Gabriel Joel
Added some safeguards in case the decompression or the download failed, organized some stuff, overall improvements
210
		decompression = 'tar -xvvzf'
19 by Gabriel Joel
Cleaned some stuff up, general improvements
211
	
21 by Gabriel Joel
Some cosmetic changes here and there
212
	elif '.tar.bz2' in driver_url:
22 by Gabriel Joel
Added some safeguards in case the decompression or the download failed, organized some stuff, overall improvements
213
		decompression = 'tar -xvvjf'
19 by Gabriel Joel
Cleaned some stuff up, general improvements
214
	
21 by Gabriel Joel
Some cosmetic changes here and there
215
	elif '.cab' in driver_url:
22 by Gabriel Joel
Added some safeguards in case the decompression or the download failed, organized some stuff, overall improvements
216
		decompression = 'cabextract'
19 by Gabriel Joel
Cleaned some stuff up, general improvements
217
	
21 by Gabriel Joel
Some cosmetic changes here and there
218
	elif '.rar' in driver_url:
22 by Gabriel Joel
Added some safeguards in case the decompression or the download failed, organized some stuff, overall improvements
219
		decompression = 'unrar'
19 by Gabriel Joel
Cleaned some stuff up, general improvements
220
	
221
	if decompression != 'manual':	
9 by Gabriel Joel
Added new feature 'offline mode', added some comments, organized some functions, add new function 'check for internet'
222
		#Checks if the system has the necessary decompression program
23 by Gabriel Joel
Improved the installation method once again, replaced all uses of commands.getoutput with a new function called getoutput that uses subprocess. General improvements
223
		test = getoutput(decompression)
19 by Gabriel Joel
Cleaned some stuff up, general improvements
224
		if 'command not found' in test:
225
			print '[ERROR] The program to extract %s files is not installed in your system' % (decompression, )
226
			print 'Install a program on your system to extract %s files and then rerun the script' % (decompression, )
9 by Gabriel Joel
Added new feature 'offline mode', added some comments, organized some functions, add new function 'check for internet'
227
			sys.exit(1)
7 by Gabriel Joel
Took all of wolf's new code, a lot by the way, and added a thing that asks if you agree with the EULA
228
	return decompression
229
	
22 by Gabriel Joel
Added some safeguards in case the decompression or the download failed, organized some stuff, overall improvements
230
def fetch(url, card_id):
23 by Gabriel Joel
Improved the installation method once again, replaced all uses of commands.getoutput with a new function called getoutput that uses subprocess. General improvements
231
	"""
232
	Downloads the apropiate driver.
233
	"""
234
22 by Gabriel Joel
Added some safeguards in case the decompression or the download failed, organized some stuff, overall improvements
235
	print '[*] Downloading driver now from "%s"' % (url, )
236
	if (opts.quiet):
237
		retcode = subprocess.call(['wget', url, '-qO', opts.tmp_dir/card_id/driver])
238
		if retcode != 0:
23 by Gabriel Joel
Improved the installation method once again, replaced all uses of commands.getoutput with a new function called getoutput that uses subprocess. General improvements
239
			print '[ERROR] Download unsuccessfull, please check your Internet conecction'
22 by Gabriel Joel
Added some safeguards in case the decompression or the download failed, organized some stuff, overall improvements
240
			sys.exit(1)
7 by Gabriel Joel
Took all of wolf's new code, a lot by the way, and added a thing that asks if you agree with the EULA
241
		else:
22 by Gabriel Joel
Added some safeguards in case the decompression or the download failed, organized some stuff, overall improvements
242
			retcode = subprocess.call(['wget', url, '-O', opts.tmp_dir/card_id/driver])
243
			if retcode != 0:
23 by Gabriel Joel
Improved the installation method once again, replaced all uses of commands.getoutput with a new function called getoutput that uses subprocess. General improvements
244
				print '[ERROR] Download unsuccessfull, please check your Internet conecction'
22 by Gabriel Joel
Added some safeguards in case the decompression or the download failed, organized some stuff, overall improvements
245
				sys.exit(1)
21 by Gabriel Joel
Some cosmetic changes here and there
246
		print '   Download successfull'
22 by Gabriel Joel
Added some safeguards in case the decompression or the download failed, organized some stuff, overall improvements
247
		
248
def extract(decompression):
23 by Gabriel Joel
Improved the installation method once again, replaced all uses of commands.getoutput with a new function called getoutput that uses subprocess. General improvements
249
	"""
250
	Extracts the dowloaded driver.
251
	"""
252
22 by Gabriel Joel
Added some safeguards in case the decompression or the download failed, organized some stuff, overall improvements
253
	print '[*] Extracting driver'
254
	
255
	if (opts.debug):
256
		print '[DEBUG] Decompression method: %s' % (decompression, )
257
		
258
	retcode = subprocess.call([decompression, opts.tmp_dir/card_id/driver])
259
	
260
	if recode != 0:
261
		print '[ERROR] Decompression failed'
262
		choice = raw_input('(C)ontinue, (R)etry, (S)pecify different decompression method or (E)xit: ')
263
			
264
		if choice == 'C' or choice == 'c':
265
			return 0
266
			
267
		elif choice == 'R' or choice == 'r':
268
			return extract(decompression)
269
		
270
		elif choice == 'S' or choice == 's':
271
			divided_url = url.split('.')
272
			file_type = divided_url[len(divided_url)-1]
273
			decompression = raw_input('Enter command to decompress %s files: ' % (file_type, ))
274
			return extract(decompression)		
275
276
		elif choice == 'E' or choice == 'e':
277
			sys.exit(1)
278
		
279
		else:
280
			return extract(decompression)
9 by Gabriel Joel
Added new feature 'offline mode', added some comments, organized some functions, add new function 'check for internet'
281
22 by Gabriel Joel
Added some safeguards in case the decompression or the download failed, organized some stuff, overall improvements
282
def manualmode(url, card_id):
23 by Gabriel Joel
Improved the installation method once again, replaced all uses of commands.getoutput with a new function called getoutput that uses subprocess. General improvements
283
	"""
284
	Comes into play if the user doesn't have an internet connection or if the driver need to be fetched manually.
285
	"""
22 by Gabriel Joel
Added some safeguards in case the decompression or the download failed, organized some stuff, overall improvements
286
	yourcard_dic = data[card_id]
287
	driver = yourcard_dic['driver']
19 by Gabriel Joel
Cleaned some stuff up, general improvements
288
	print 'The driver needs to be fetched manually from: %s' % (url, )
9 by Gabriel Joel
Added new feature 'offline mode', added some comments, organized some functions, add new function 'check for internet'
289
	print 'Please place the "%s" file, along with the .sys files into:' % (driver, )
23 by Gabriel Joel
Improved the installation method once again, replaced all uses of commands.getoutput with a new function called getoutput that uses subprocess. General improvements
290
	print '"%s/%s"' % (opts.tmp_dir, card_id, )
22 by Gabriel Joel
Added some safeguards in case the decompression or the download failed, organized some stuff, overall improvements
291
	while not any(driver in line for line in os.listdir('%s/%s/' % (opts.tmp_dir, card_id, ))):
23 by Gabriel Joel
Improved the installation method once again, replaced all uses of commands.getoutput with a new function called getoutput that uses subprocess. General improvements
292
		
9 by Gabriel Joel
Added new feature 'offline mode', added some comments, organized some functions, add new function 'check for internet'
293
		try:
19 by Gabriel Joel
Cleaned some stuff up, general improvements
294
			dummy = raw_input('When you have succesfully dowloaded the driver and extracted it press <Enter>: ')
9 by Gabriel Joel
Added new feature 'offline mode', added some comments, organized some functions, add new function 'check for internet'
295
			
19 by Gabriel Joel
Cleaned some stuff up, general improvements
296
		except: 
23 by Gabriel Joel
Improved the installation method once again, replaced all uses of commands.getoutput with a new function called getoutput that uses subprocess. General improvements
297
			##Executed when CTRL + C is pressed.
19 by Gabriel Joel
Cleaned some stuff up, general improvements
298
			print '\n\nCanceled!\n'
9 by Gabriel Joel
Added new feature 'offline mode', added some comments, organized some functions, add new function 'check for internet'
299
			sys.exit(1)
300
	print '  Driver file found! Continuing installation.'
4 by Gabriel Joel
Auto detection/fetching/install almost ready
301
22 by Gabriel Joel
Added some safeguards in case the decompression or the download failed, organized some stuff, overall improvements
302
def installdriver(card_id):
23 by Gabriel Joel
Improved the installation method once again, replaced all uses of commands.getoutput with a new function called getoutput that uses subprocess. General improvements
303
	"""
304
	Installs the driver with NDISwrapper.
305
	"""
306
22 by Gabriel Joel
Added some safeguards in case the decompression or the download failed, organized some stuff, overall improvements
307
	driver = yourcard_dic['driver']
23 by Gabriel Joel
Improved the installation method once again, replaced all uses of commands.getoutput with a new function called getoutput that uses subprocess. General improvements
308
	inf_file = getoutput('find', opts.tmp_dir/card_id, '-name', driver)
7 by Gabriel Joel
Took all of wolf's new code, a lot by the way, and added a thing that asks if you agree with the EULA
309
	if ( opts.debug ):
23 by Gabriel Joel
Improved the installation method once again, replaced all uses of commands.getoutput with a new function called getoutput that uses subprocess. General improvements
310
		print '[DEBUG] Driver found at %s' % (inf, )
311
312
	#Attempt to install driver.
313
	output, retcode = get_output_retcode('ndiswrapper', '-i', inf_file)
314
315
	#Attempt to detect errors.
316
	if "already" in output:
317
		print 'Driver is already installed!'
318
		sys.exit(1)
14 by Gabriel Joel
Now the program adds ndiswrapper to the modules list
319
	
23 by Gabriel Joel
Improved the installation method once again, replaced all uses of commands.getoutput with a new function called getoutput that uses subprocess. General improvements
320
	elif retcode != 0:
321
		print '[ERROR] Driver installation failed'
322
		sys.exit(1)
323
			
324
	else:
325
		##Assume driver installed successfully. Set up and reload module.
326
		subprocess.call(['ndiswrapper',  '-ma'])
327
		subprocess.call(['modprobe', '-r', 'ndiswrapper'])
328
		subprocess.call(['modprobe', 'ndiswrapper'])
329
		print 'Installation finished'
330
		sys.exit(1)
7 by Gabriel Joel
Took all of wolf's new code, a lot by the way, and added a thing that asks if you agree with the EULA
331
18 by Gabriel Joel
Inproved somethings in the databasecreator
332
def create_log_file():
23 by Gabriel Joel
Improved the installation method once again, replaced all uses of commands.getoutput with a new function called getoutput that uses subprocess. General improvements
333
	"""
334
	Creates a simple log file with some useful info.
335
	"""
336
19 by Gabriel Joel
Cleaned some stuff up, general improvements
337
	log = open('auto-ndis-log.txt', 'w')
18 by Gabriel Joel
Inproved somethings in the databasecreator
338
21 by Gabriel Joel
Some cosmetic changes here and there
339
	os_info = os.uname()
340
	distribution = platform.dist()
23 by Gabriel Joel
Improved the installation method once again, replaced all uses of commands.getoutput with a new function called getoutput that uses subprocess. General improvements
341
	ndis_version = getoutput('ndiswrapper', '-v')
18 by Gabriel Joel
Inproved somethings in the databasecreator
342
19 by Gabriel Joel
Cleaned some stuff up, general improvements
343
	log.write('OS and date = ')
21 by Gabriel Joel
Some cosmetic changes here and there
344
	for item in os_info:
18 by Gabriel Joel
Inproved somethings in the databasecreator
345
		log.write(item)
346
19 by Gabriel Joel
Cleaned some stuff up, general improvements
347
	log.write('\nDistribution = ')
21 by Gabriel Joel
Some cosmetic changes here and there
348
	for item in distribution:
18 by Gabriel Joel
Inproved somethings in the databasecreator
349
		log.write(item)
350
19 by Gabriel Joel
Cleaned some stuff up, general improvements
351
	log.write('\nNDISwrapper info = ')
18 by Gabriel Joel
Inproved somethings in the databasecreator
352
	for item in ndis_version:
353
		log.write(item)
354
22 by Gabriel Joel
Added some safeguards in case the decompression or the download failed, organized some stuff, overall improvements
355
	log.write('\nID = %s \n' % (card_id, ))
19 by Gabriel Joel
Cleaned some stuff up, general improvements
356
	log.write('URL = %s \n' % (url, ))
357
	log.write('DRIVER = %s \n' % (driver, ))
18 by Gabriel Joel
Inproved somethings in the databasecreator
358
359
	log.close()
7 by Gabriel Joel
Took all of wolf's new code, a lot by the way, and added a thing that asks if you agree with the EULA
360
19 by Gabriel Joel
Cleaned some stuff up, general improvements
361
def get_card_id():
362
	##Gets the id of all the pci devices
23 by Gabriel Joel
Improved the installation method once again, replaced all uses of commands.getoutput with a new function called getoutput that uses subprocess. General improvements
363
	outtext = getoutput('lspci', '-n')
364
19 by Gabriel Joel
Cleaned some stuff up, general improvements
365
	##Stores the pci ids with the revision number in a list
366
	pci = re.findall('.{4}:.{4}\ \(rev .{2}\)',outtext)
367
368
	##Gets the id of all the usb devices
23 by Gabriel Joel
Improved the installation method once again, replaced all uses of commands.getoutput with a new function called getoutput that uses subprocess. General improvements
369
	outtext2 = getoutput('lsusb')
19 by Gabriel Joel
Cleaned some stuff up, general improvements
370
	##Stores the usb ids in a list
371
	usb = re.findall('.{4}:.{4}',outtext2)
372
373
	pcicard = searchcard(pci, data)
374
	usbcard = searchcard(usb, data)
375
376
	##Checks if the user has a pci card or a usb card, if he has both the program will ask card he wants to setup
377
	##If he has neither the program will exit and tell him what pci card he has if he has one
378
	if pcicard != -1 and usbcard == -1:
379
		return pcicard
380
381
	elif pcicard == -1 and usbcard != -1:
382
		return usbcard
383
384
	elif pcicard != -1 and usbcard != -1:
23 by Gabriel Joel
Improved the installation method once again, replaced all uses of commands.getoutput with a new function called getoutput that uses subprocess. General improvements
385
		choice = raw_input('Setup (w)ificard or setup your (u)sbcard?: ') 
4 by Gabriel Joel
Auto detection/fetching/install almost ready
386
		
23 by Gabriel Joel
Improved the installation method once again, replaced all uses of commands.getoutput with a new function called getoutput that uses subprocess. General improvements
387
		while choice != 'W' and choice != 'w' and choice != 'U' and choice != 'u':
19 by Gabriel Joel
Cleaned some stuff up, general improvements
388
			print 'Please try again'
23 by Gabriel Joel
Improved the installation method once again, replaced all uses of commands.getoutput with a new function called getoutput that uses subprocess. General improvements
389
			choice = raw_input('Setup (w)ificard or setup your (u)sbcard?: ') 
390
		if choice == 'W' or choice == 'w':
19 by Gabriel Joel
Cleaned some stuff up, general improvements
391
			return pcicard
23 by Gabriel Joel
Improved the installation method once again, replaced all uses of commands.getoutput with a new function called getoutput that uses subprocess. General improvements
392
		elif choice == 'U' or choice == 'u':
19 by Gabriel Joel
Cleaned some stuff up, general improvements
393
			return usbcard
23 by Gabriel Joel
Improved the installation method once again, replaced all uses of commands.getoutput with a new function called getoutput that uses subprocess. General improvements
394
		else:
395
			return get_card_id()
4 by Gabriel Joel
Auto detection/fetching/install almost ready
396
		
19 by Gabriel Joel
Cleaned some stuff up, general improvements
397
	elif pcicard == -1 and usbcard == -1:
21 by Gabriel Joel
Some cosmetic changes here and there
398
		print 'Sorry, card not yet supported by Auto-NDISwrapper'
19 by Gabriel Joel
Cleaned some stuff up, general improvements
399
		print 'Save this output as it will help other people give you support'
23 by Gabriel Joel
Improved the installation method once again, replaced all uses of commands.getoutput with a new function called getoutput that uses subprocess. General improvements
400
		get_print_card_info()
6 by Gabriel Joel
Added all of wolf's code except the blacklisting and the removal, ready to merge and functional
401
		sys.exit(1)
4 by Gabriel Joel
Auto detection/fetching/install almost ready
402
22 by Gabriel Joel
Added some safeguards in case the decompression or the download failed, organized some stuff, overall improvements
403
card_id = get_card_id()
404
yourcard_dic = data[card_id]
405
url = yourcard_dic['url']
406
driver = yourcard_dic['driver']
407
decompression = find_decompression(url)
408
19 by Gabriel Joel
Cleaned some stuff up, general improvements
409
print '[*] Card Supported'
22 by Gabriel Joel
Added some safeguards in case the decompression or the download failed, organized some stuff, overall improvements
410
print '[*] Will attempt to fetch and install driver for the card with id: %s' % (card_id, )
19 by Gabriel Joel
Cleaned some stuff up, general improvements
411
print '[*] Will attempt to fetch driver from: %s' % (url, )
22 by Gabriel Joel
Added some safeguards in case the decompression or the download failed, organized some stuff, overall improvements
412
print '[*] Will attempt to install driver: %s' % (driver, )
7 by Gabriel Joel
Took all of wolf's new code, a lot by the way, and added a thing that asks if you agree with the EULA
413
414
if (opts.ask):
19 by Gabriel Joel
Cleaned some stuff up, general improvements
415
	print '   Do you want to continue?'
7 by Gabriel Joel
Took all of wolf's new code, a lot by the way, and added a thing that asks if you agree with the EULA
416
	try:
19 by Gabriel Joel
Cleaned some stuff up, general improvements
417
		dummy = raw_input('   Please hit <Enter> to continue, or use Ctrl+C to cancel: ')
7 by Gabriel Joel
Took all of wolf's new code, a lot by the way, and added a thing that asks if you agree with the EULA
418
	except:
19 by Gabriel Joel
Cleaned some stuff up, general improvements
419
		print '\n\nCanceled!\n'		
7 by Gabriel Joel
Took all of wolf's new code, a lot by the way, and added a thing that asks if you agree with the EULA
420
		sys.exit(1)
19 by Gabriel Joel
Cleaned some stuff up, general improvements
421
	print
7 by Gabriel Joel
Took all of wolf's new code, a lot by the way, and added a thing that asks if you agree with the EULA
422
22 by Gabriel Joel
Added some safeguards in case the decompression or the download failed, organized some stuff, overall improvements
423
print '[*] Beginning Setup Procedure:\n'
424
print_driver_license()
10 by Gabriel Joel
Fixed some bugs, and it now prints the card's name when beginning installation
425
create_auto_ndis_folder()
18 by Gabriel Joel
Inproved somethings in the databasecreator
426
create_log_file()
22 by Gabriel Joel
Added some safeguards in case the decompression or the download failed, organized some stuff, overall improvements
427
428
if decompression != 'manual':
23 by Gabriel Joel
Improved the installation method once again, replaced all uses of commands.getoutput with a new function called getoutput that uses subprocess. General improvements
429
	check_for_internet(url, card_id)
22 by Gabriel Joel
Added some safeguards in case the decompression or the download failed, organized some stuff, overall improvements
430
	fetch(url, card_id)
431
	extract(decompression)
432
	installdriver(card_id)
433
else:
434
	manualmode(url,card_id)
435
	installdriver(card_id)