0

First of all sorry for my bad English.

I made a script that connects to a Mikortik RB750 and executes commands by SSH. When I run it (without compiling), it works well and meets the objective. The problem I'm having is that when I try to compile it on Windows fails. Works fine until it try to run the SSH connection.

I'm trying to compile it using the command python -m py2exe.build_exe ssh_menu.py It tells me that 4 modules are missing: UserDict; gssapi; packaging; readline. I have no idea how to solve this, and I google a lot, pitifully this is the first time that I attempt to do something with python and I’m lost now.

Here is my code:

import os 
import sys
import time
import select
import paramiko
server, username, password = ('192.168.88.1', 'admin', '976431')
comando1=" "
comando2=" "
ipUbicacion=" "
flagUbicacion=True
ipSalida=" "
flagSalida=True
addresList1=" "
miIp=input("Cual es su dirección IP: ")
while flagUbicacion:
    print("""
        1.ESSO Centro
        2.ESSO Rotonda
        3.Casa Don Oscar / Sebastian
        4.Transporte Garay
        5.Salir
    """)
idUbicacion=input("Cual es su ubicación? ")
if idUbicacion=="1":
  ipUbicacion="192.168.88.1"
  flagUbicacion=False
elif idUbicacion=="2":
  ipUbicacion="192.168.88.2"
  flagUbicacion=False
elif idUbicacion=="3":
  ipUbicacion="192.168.88.3"
  flagUbicacion=False
elif idUbicacion=="4":
  ipUbicacion="192.168.88.4"
  flagUbicacion=False
elif idUbicacion=="5":
  print("\n Programa Terminado") 
  idUbicacionans = None
else:
   print("\n El dato ingresado no es valido")
while flagSalida:
    print("""
    1.Modem ESSO Centro
    2.Integra ESSO Centro
    3.Modem ESSO Rotonda
    4.Modem Casa Don Oscar
    5.10+ Transporte Garay
    6.GampelCom Transporte Garay
    7.Salir
    """)
idSalida=input("Por cual puerta de enlace quiere salir? ")
if idSalida=="1":
  ipSalida="192.168.88.1"
  auxUbicacion="1"
  flagSalida=False
elif idSalida=="2":
  ipSalida="192.168.88.1"
  auxUbicacion="1"
  flagSalida=False
elif idSalida=="3":
  ipSalida="192.168.88.2"
  auxUbicacion="2"
  flagSalida=False
elif idSalida=="4":
  ipSalida="192.168.88.3"
  auxUbicacion="3"
  flagSalida=False
elif idSalida=="5":
  ipSalida="192.168.88.4"
  auxUbicacion="4"
  flagSalida=False
elif idSalida=="6":
  ipSalida="192.168.88.4"
  auxUbicacion="4"
  flagSalida=False
elif idSalida=="7":
  print("\n Programa Terminado") 
  idSalida = None
  flagSalida=False
else:
   print("\n El dato ingresado no es valido")
if ipUbicacion==ipSalida:
    cantComandos=1
else:
    cantComandos=2
i=0
while i<cantComandos:
    if idUbicacion=="1": #SI ESTOY EN LA ESSO DEL CENTRO Y...
        if idSalida=="1": #...QUIERO SALIR POR EL MODEM
            addresList1="WAN1"
        elif idSalida=="2": #...QUIERO SALIR POR EL INTEGRA
            addresList1="WAN2"
        elif idSalida=="3": #...QUIERO SALIR POR RB_ROTONDA
            addresList1="WAN3"
        elif idSalida=="4": #...QUIERO SALIR POR RB_OSCAR
            addresList1="WAN4"
        elif idSalida=="5": #...QUIERO SALIR POR RB_TRANSPORTE
            addresList1="WAN5"
        elif idSalida=="6": #...QUIERO SALIR POR RB_TRANSPORTE
            addresList1="WAN5"
    if idUbicacion=="2": #SI ESTOY EN LA ESSO DE LA ROTONDA Y...
        if idSalida=="1": #...QUIERO SALIR POR RB_CENTRO
            addresList1="WAN2"
        elif idSalida=="2": #...QUIERO SALIR POR RB_CENTRO
            addresList1="WAN2"
        elif idSalida=="3": #...QUIERO SALIR POR MODEM ROTONDA
            addresList1="WAN1"
        elif idSalida=="4": #...QUIERO SALIR POR RB_OSCAR
            addresList1="WAN3"
        elif idSalida=="5": #...QUIERO SALIR POR RB_TRANSPORTE
            addresList1="WAN4"
        elif idSalida=="6": #...QUIERO SALIR POR RB_TRANSPORTE
            addresList1="WAN4"
    if idUbicacion=="3": #SI ESTOY EN LA CASA DE OSCAR Y...
        if idSalida=="1": #...QUIERO SALIR POR RB_CENTRO
            addresList1="WAN2"
        elif idSalida=="2": #...QUIERO SALIR POR RB_CENTRO
            addresList1="WAN2"
        elif idSalida=="3": #...QUIERO SALIR POR RB_ROTONDA
            addresList1="WAN3"
        elif idSalida=="4": #...QUIERO SALIR POR MODEM OSCAR
            addresList1="WAN1"
        elif idSalida=="5": #...QUIERO SALIR POR RB_TRANSPORTE
            addresList1="WAN4"
        elif idSalida=="6": #...QUIERO SALIR POR RB_TRANSPORTE
            addresList1="WAN4"
    if idUbicacion=="4": #SI ESTOY EN TRANSPORTE Y...
        if idSalida=="1": #...QUIERO SALIR POR RB_CENTRO
            addresList1="WAN3"
        elif idSalida=="2": #...QUIERO SALIR POR RB_CENTRO
            addresList1="WAN3"
        elif idSalida=="3": #...QUIERO SALIR POR RB_ROTONDA
            addresList1="WAN4"
        elif idSalida=="4": #...QUIERO SALIR POR RB_OSCAR
            addresList1="WAN5"
        elif idSalida=="5": #...QUIERO SALIR POR 10+ TRANSPORTE
            addresList1="WAN1"
        elif idSalida=="6": #...QUIERO SALIR POR GAMPELCOM TRANSPORTE
            addresList1="WAN2"
    if i==0:
        server=ipUbicacion
        while True:
            print ("Conectando a "+server+": Intento "+repr(i)+" de 10")
            try:
                ssh = paramiko.SSHClient()
                ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
                ssh.connect(server, username=username, password=password)
                print ("Conectado a "+server)
            break
            except paramiko.AuthenticationException:
                print ("Fallo de autentificación conectando a "+server)
            except:
                print ("Fallo de SSH en "+server+", esperando que se inicie")
            i += 1
            time.sleep(1)
        if i == 10:
            print ("No se pudo conectar a "+server+". Conexión cancelada") % server
    ssh.exec_command('ip firewall address-list remove [find where address="'+miIp+'"]')
    ssh.exec_command('ip firewall address-list add list="'+addresList1+'" address="'+miIp+'"')
    print('ip firewall address-list add list="'+addresList1+'" address="'+miIp+'"')
    print ("Comando ejecutado, cerrando conexión SSH")
    ssh.close()
    time.sleep(1)
else:
    server=ipSalida
    while True:
        print ("Conectando a "+server+": Intento "+repr(i)+" de 10")
        try:
            ssh = paramiko.SSHClient()
            ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
            ssh.connect(server, username=username, password=password)
            print ("Conectado a "+server)
            break
        except paramiko.AuthenticationException:
            print ("Fallo de autentificación conectando a "+server)
        except:
            print ("Fallo de SSH en "+server+", esperando que se inicie")
            i += 1
            time.sleep(1)
        if i == 10:
            print ("No se pudo conectar a "+server+". Conexión cancelada") % server
    ssh.exec_command('ip firewall address-list remove [find where address="'+miIp+'"]')
    ssh.exec_command('ip firewall address-list add list="'+addresList1+'" address="'+miIp+'"')
    print('ip firewall address-list add list="'+addresList1+'" address="'+miIp+'"')
    print ("Comando ejecutado, cerrando conexión SSH")
    ssh.close()
    time.sleep(1)
idUbicacion=auxUbicacion
i=i+1
dsh
  • 12,037
  • 3
  • 33
  • 51

1 Answers1

0

When compiling a python program, I tend to use cxfreeze instead of py2exe (use the command pip install CX_Freeze from the windows command prompt), where you can use this code to compile the program:

cxfreeze "Path\To\File.py" --target-dir "Path\to\exe\file\"

Path\To\File.py is the directory tree to the python file you are trying to compile

Path\to\exe\file\ is the directory tree to a folder where the compiled program will be stored (I recommend this to be empty)

See here for more details.

Hope this helps.

Community
  • 1
  • 1
AvahW
  • 2,083
  • 3
  • 24
  • 30
  • thank you for the reply but I'm still having the same error [screen shot](http://i66.tinypic.com/xknm0j.png) – Carlos Weirich Jun 06 '16 at 11:49
  • Ive been thinking over this, and the only thing I can suggest is reinstalling `crytography` as the error message suggests, the best option to do this would be to upgrade to a newer version of python (it can still be the same version, such as 3.4, just with the latest update) or try manually installing it (`pip Install --upgrade cryptography`) which may not work – AvahW Jun 06 '16 at 21:04
  • I notice that you have two lines of `print ("Conectando a "+server+": Intento "+repr(i)+" de 10"` could you please work out which line is being displayed on the error message you sent, it could help locate the error and fix the problem – AvahW Jun 06 '16 at 21:10
  • it's the first one, I've tryed to upgrade cryptography, but the error message persist. I'll try to upgrade python and see what happens. – Carlos Weirich Jun 08 '16 at 12:31