Proyecto

General

Perfil

172 jredrejo
#!/usr/bin/env python
# -*- coding: utf-8 -*-
##############################################################################
# Project: Wicd -> Network Manager
# Module: wicdnm.py
# Purpose: AConvert wicd configuration files to NM system configurations
# Language: Python 2.5
# Date: 24-May-2011.
# File Version: 24-May-2011
# Author: José L. Redrejo Rodríguez
# Copyright: 2011 - José L. Redrejo Rodríguez <jredrejo @nospam@ debian.org>
#

# This file is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# wicd2nm.py is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this file. If not, see <http://www.gnu.org/licenses/>.
#
# Important: WHEN EDITING THIS FILE, USE TABS TO INDENT - NOT SPACES!
#
# More important: This script has been done with the public schools laptops
# of Extremadura in mind. It WILL fail in some cases that are not possible
# in these laptops, so don't use it as a general wicd to nm configuration
# conversion script, because there are not contempled case uses.
##############################################################################

import ConfigParser
import os.path
from os import makedirs,chmod
from uuid import uuid1

wicd_config='/etc/wicd/wireless-settings.conf'
175 jredrejo
#wicd_config='/tmp/wireless-settings.conf'
172 jredrejo
nm_config_dir='/etc/NetworkManager/system-connections'
175 jredrejo
#nm_config_dir='/tmp/nm'
172 jredrejo
def parse_wicd():
Connections=[]
my_parser = ConfigParser.ConfigParser()
archivo_config = my_parser.read(wicd_config)
redes=my_parser.sections()
opciones=('passphrase','bssid','essid','enctype','automatic','key')
for red in redes:
try:
red_info=my_parser.items(red)
datos_red={}
for i in red_info:
if i[0] in opciones:
datos_red[i[0]]=i[1]

Connections.append(datos_red)
except:
pass #red mal construida

return Connections

def string2ascii(cadena):
ascii=''
for car in cadena:
ascii +=str(ord(car)) + ";"
return ascii


def crea_nm_configs(conexiones):
if not os.path.exists(nm_config_dir): makedirs(nm_config_dir)
for con in conexiones:
my_parser=ConfigParser.RawConfigParser()
my_parser.add_section('connection')
my_parser.add_section('ipv4')
my_parser.add_section('ipv6')
my_parser.add_section('802-11-wireless')
my_parser.set('connection','uuid',uuid1())
my_parser.set('connection','timestamp','1305878385')
my_parser.set('connection','type','802-11-wireless')
my_parser.set('802-11-wireless','mode','infrastructure')
174 jredrejo
172 jredrejo
my_parser.set('ipv4','method','auto')
my_parser.set('ipv6','method','ignore')

try:
id='Auto ' + con['essid']
my_parser.set('connection','id',id)
my_parser.set('802-11-wireless','seen-bssids',con['bssid'] + ';')
my_parser.set('802-11-wireless','ssid',string2ascii(con['essid']))
if 'passphrase' in con.keys():
contras=con['passphrase']
174 jredrejo
elif 'key' in con.keys():
contras=con['key']
172 jredrejo
else:
174 jredrejo
contras=None
172 jredrejo
174 jredrejo
if 'enctype' in con.keys():
my_parser.set('802-11-wireless','security','802-11-wireless-security')
my_parser.add_section('802-11-wireless-security')
if con['enctype'] == 'wep-passphrase' or con['enctype'] == 'wep-hex':
my_parser.set('802-11-wireless-security','key-mgmt','none')
my_parser.set('802-11-wireless-security','auth-alg','open')
my_parser.set('802-11-wireless-security','wep-key0',contras)
175 jredrejo
if con['enctype'] == 'wep-passphrase' and ('passphrase' in con.keys()):
174 jredrejo
tipo=2
else:
tipo=1
my_parser.set('802-11-wireless-security','wep-key-type',tipo)
172 jredrejo
174 jredrejo
elif con['enctype'] == 'wpa-psk' or con['enctype'] == 'wpa':
my_parser.set('802-11-wireless-security','key-mgmt','wpa-psk')
my_parser.set('802-11-wireless-security','psk',contras)
172 jredrejo
174 jredrejo
else:
continue #caso no contemplado
else: #autenticación sin contraseña
pass #no necesita '802-11-wireless-security'

172 jredrejo
configfile=open(os.path.join(nm_config_dir,id), 'wb')
my_parser.write(configfile)
configfile.close()
chmod(os.path.join(nm_config_dir,id),0600)


except:
continue #hay algún dato mal en la configuración, así que tomamos la siguiente



if __name__ == '__main__':
conn=parse_wicd()
crea_nm_configs(conn)
173 jredrejo
#creamos archivo vacío de testigo de haber ejecutado el script
174 jredrejo
if not os.path.exists('/var/wicd2nm2'): open('/var/wicd2nm2', 'w').close()