<commit_before>############################################################################
#                                                                          #
# This Source Code Form is subject to the terms of the Mozilla Public      #
# License, v. 2.0. If a copy of the MPL was not distributed with this      #
# file, You can obtain one at http://mozilla.org/MPL/2.0/.                 #
#                                                                          #
# Copyright (c)2017 Digi International Inc. All Rights Reserved.           #
#                                                                          #
############################################################################

import csv
import fileinput
import logging
import os.path
import paramiko
import re
import socket
import sys
import traceback

from time import gmtime, localtime, strftime


CSV_FIELDNAMES = ['devId', 'installCode']
DEVID = '00000000-00000000-00000000-00000000'
DRM_HOSTNAME = "my.devicecloud.com"
HR = "----------------------------------------------------------------"
IP_FILENAME = "iplist.txt"
PASSWORD = "password"
SSH_TIMEOUT = 20  # seconds
USERNAME = "username"

CMD_ENABLE_DRM = "cloud 0 clientconn ON"
CMD_SET_SERVER = "cloud 0 server %s" % DRM_HOSTNAME
CMD_SAVEALL = "config 0 save"
CMD_HW_INFO = "hw ?"
CMD_REBOOT = "reboot"

# Global IP List
ip_addrs = []
csvwriter = None
csvfile = None


def config_logging(filename, console_level, file_level):
    # set up logging to file - see previous section for more details
    logging.basicConfig(level=file_level,
                        format='%(asctime)s %(levelname)-8s %(message)s',
                        datefmt='%Y-%m-%d %I:%M:%S%p',
                        filename='./{0}.log'.format(filename),
                        filemode='w')
    # define a Handler which writes INFO messages or higher to the sys.stderr
    console = logging.StreamHandler()
    console.setLevel(console_level)
    # set a format which is simpler for console use
    formatter = logging.Formatter('%(message)s')
    # tell the handler to use this format
    console.setFormatter(formatter)
    # add the handler to the root logger
    logging.getLogger('').addHandler(console)


def connect_to_router(ip_addr):
    logging.info("Connecting to %s..." % ip_addr)
    ssh = paramiko.SSHClient()
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    ssh.connect(ip_addr, username=USERNAME, password=PASSWORD, timeout=SSH_TIMEOUT)
    return ssh


def config_router(ip_addr, reboot=True):
    ssh_connection = connect_to_router(ip_addr)

    logging.info("Enabling Device Cloud Client for %s..." % ip_addr)
    stdin, stdout, stderr = ssh_connection.exec_command(CMD_ENABLE_DRM)

    logging.info("Setting Device Cloud hostname for %s..." % ip_addr)
    stdin, stdout, stderr = ssh_connection.exec_command(CMD_SET_SERVER)

    logging.info("Saving config to flash for %s..." % ip_addr)
    stdin, stdout, stderr = ssh_connection.exec_command(CMD_SAVEALL)

    logging.info("Getting hardware information for %s..." % ip_addr)
    stdin, stdout, stderr = ssh_connection.exec_command(CMD_HW_INFO)
    hw_info = stdout.readlines()

    if reboot:
        logging.info("Rebooting %s..." % ip_addr)
        stdin, stdout, stderr = ssh_connection.exec_command(CMD_REBOOT)

    return hw_info<commit_msg>Adding SSH Port to connection<commit_after>############################################################################
#                                                                          #
# This Source Code Form is subject to the terms of the Mozilla Public      #
# License, v. 2.0. If a copy of the MPL was not distributed with this      #
# file, You can obtain one at http://mozilla.org/MPL/2.0/.                 #
#                                                                          #
# Copyright (c)2017 Digi International Inc. All Rights Reserved.           #
#                                                                          #
############################################################################

import csv
import fileinput
import logging
import os.path
import paramiko
import re
import socket
import sys
import traceback

from time import gmtime, localtime, strftime


CSV_FIELDNAMES = ['devId', 'installCode']
DEVID = '00000000-00000000-00000000-00000000'
DRM_HOSTNAME = "my.devicecloud.com"
HR = "----------------------------------------------------------------"
IP_FILENAME = "iplist.txt"
PASSWORD = "password"
SSH_TIMEOUT = 20  # seconds
USERNAME = "username"
SSH_PORT = 22

CMD_ENABLE_DRM = "cloud 0 clientconn ON"
CMD_SET_SERVER = "cloud 0 server %s" % DRM_HOSTNAME
CMD_SAVEALL = "config 0 save"
CMD_HW_INFO = "hw ?"
CMD_REBOOT = "reboot"

# Global IP List
ip_addrs = []
csvwriter = None
csvfile = None


def config_logging(filename, console_level, file_level):
    # set up logging to file - see previous section for more details
    logging.basicConfig(level=file_level,
                        format='%(asctime)s %(levelname)-8s %(message)s',
                        datefmt='%Y-%m-%d %I:%M:%S%p',
                        filename='./{0}.log'.format(filename),
                        filemode='w')
    # define a Handler which writes INFO messages or higher to the sys.stderr
    console = logging.StreamHandler()
    console.setLevel(console_level)
    # set a format which is simpler for console use
    formatter = logging.Formatter('%(message)s')
    # tell the handler to use this format
    console.setFormatter(formatter)
    # add the handler to the root logger
    logging.getLogger('').addHandler(console)


def connect_to_router(ip_addr):
    logging.info("Connecting to %s..." % ip_addr)
    ssh = paramiko.SSHClient()
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    ssh.connect(hostname=ip_addr, port=SSH_PORT, username=USERNAME, password=PASSWORD, timeout=SSH_TIMEOUT)
    return ssh


def config_router(ip_addr, reboot=True):
    ssh_connection = connect_to_router(ip_addr)

    logging.info("Enabling Device Cloud Client for %s..." % ip_addr)
    stdin, stdout, stderr = ssh_connection.exec_command(CMD_ENABLE_DRM)

    logging.info("Setting Device Cloud hostname for %s..." % ip_addr)
    stdin, stdout, stderr = ssh_connection.exec_command(CMD_SET_SERVER)

    logging.info("Saving config to flash for %s..." % ip_addr)
    stdin, stdout, stderr = ssh_connection.exec_command(CMD_SAVEALL)

    logging.info("Getting hardware information for %s..." % ip_addr)
    stdin, stdout, stderr = ssh_connection.exec_command(CMD_HW_INFO)
    hw_info = stdout.readlines()

    if reboot:
        logging.info("Rebooting %s..." % ip_addr)
        stdin, stdout, stderr = ssh_connection.exec_command(CMD_REBOOT)

    return hw_info
{'fraction_non_alphanumeric': 0.1559171597633136, 'fraction_numerical': 0.014201183431952662, 'mean_word_length': 2.571579503433703, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 8, 'word_list_counts': {'cursed_substrings.json': 18, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1039783', 'n_tokens_mistral': 2015, 'n_tokens_neox': 1855, 'n_words': 510}