from netmiko import ConnectHandler
from datetime import datetime

from net_system.models import NetworkDevice
import django

from multiprocessing import Process


def device_list(device_obj):
    for dev in device_obj:
        yield dev

def device_connection(devices):
    creds = devices.credentials
    remote_conn = ConnectHandler(device_type=devices.device_type,
                                 ip=devices.ip_address,
                                 username=creds.username,
                                 password=creds.password,
                                 port=devices.port, secret='')
    print '#' * 80
    print remote_conn.send_command_expect('show version')
    print '#' * 80

def main():
    start_time = datetime.now()
    django.setup()
    devices_db = NetworkDevice.objects.all()
    devices = device_list(devices_db)
    process = []
    for device in devices:
        my_proc = Process(target=device_connection, args=(device,))
        my_proc.start()
        process.append(my_proc)

    for a_proc in process:
        print a_proc
        a_proc.join()

    print "\nElapsed time: " + str(datetime.now() - start_time)

if __name__ == "__main__":
    main()

