Пример #1
0
def add_os(code, descr):
    os = OperatingSystem(
        code=code,
        description=descr,
    )
    os.save()
    return os
Пример #2
0
    def test_top(self):
        dr = _detectionrange.new()
        o = _raveio.open(self.FIXTURE_VOLUME)

        result = dr.top(o.object, 2000, -40.0)

        os = _raveio.new()
        os.filename = self.TEMPORARY_FILE
        os.object = result
        os.save()
Пример #3
0
def osinstallrefresh(request, id):
    from apps.networkcenter.shortcut import get_device_ip, get_osinstall, get_device_mac

    os = OSInstallQueue.objects.get(id=id)
    d = Device.objects.get(sn=os.sn)
    (os.install, os.ilo_ip, os.os_ip, os.eth0_mac) = (get_osinstall(d.sn), get_device_ip(d, "ilo"),
                                                      get_device_ip(d, "bond0"), get_device_mac(d))
    if os.install and os.ilo_ip and os.os_ip and os.eth0_mac:
        os.save()
        return HttpResponse(json.dumps({"code": 1, "message": 1}))
    else:
        return HttpResponse(json.dumps({"code": 0, "message": 1}))
Пример #4
0
def osinstalldeliver(request, id):
    from apps.systemcenter.models import ServerPool
    try:
        os = OSInstallQueue.objects.get(id=id)
        d = Device.objects.get(sn=os.sn)
        os.deliver = True
        d.status = True
        s = ServerPool(sn=os.sn, ip=os.os_ip, oper=os.needs_user, description=os.batch.description)
        os.save()
        d.save()
        s.save()
        return HttpResponse(json.dumps({"code": 1, "message": 1}))
    except Exception, e:
        return HttpResponse(json.dumps({"code": 0, "message": 1}))
Пример #5
0
    def test_analyze_and_write(self):
        dr = _detectionrange.new()
        o = _raveio.open(self.FIXTURE_VOLUME)

        topfield = dr.top(o.object, 2000, -40.0)
        filterfield = dr.filter(topfield)
        result = dr.analyze(filterfield, 60, 0.1, 0.5)

        param = _polarscanparam.fromField(result)
        scan = _polarscan.new()
        scan.addParameter(param)

        os = _raveio.new()
        os.filename = self.TEMPORARY_FILE
        os.object = scan
        os.save()
Пример #6
0
def sync_software(data_datetime, os, data_dict):
    os.name               = son(data_dict['OperatingSystem'][0]['Caption'])
    os.computer_name      = son(data_dict['OperatingSystem'][0]['CSName'])
    os.seen_last          = data_datetime

    # save results
    os.save()

    si_list = [ si.pk for si in os.active_software_installations() ]

    license_keys = get_license_keys(data_dict)

    software_list = {}
    if "Product" in data_dict:
        for product in data_dict['Product']:
            if product['Name'] == "":
                name = "null"
            else:
                name = product['Name']

            version = son(product['Version'])
            si = install_software(data_datetime, os, name, version, license_keys)
            if si.pk in si_list:
                si_list.remove(si.pk)

    # operating system does not appear in in software list, however we should add it if
    # we detect the license key
    for product in data_dict['OperatingSystem']:
        name = product['Caption']
        version = u"%s SP%s.%s"%(
                product['Version'],
                product['ServicePackMajorVersion'],
                product['ServicePackMinorVersion'],
        )
        si = install_software(data_datetime, os, name, version, license_keys)
        if si.pk in si_list:
            si_list.remove(si.pk)

    # delete old software
    models.software_installation.objects.filter(pk__in=si_list, auto_delete=True).update(active=False)
Пример #7
0
def osinstall(request):
    index = 'cobbler'
    uname = request.user.username
    if request.method == "POST":
        form = OSInstallQueueForm(request.POST)
        if form.is_valid():
            instance = form.save()
            desc = request.POST.get("description")
            unavailable_device = []
            from apps.networkcenter.shortcut import get_device_ip, get_osinstall, get_device_mac
            batch = "%s%s%s" % (time.strftime("%Y%m%d%H%M%S"), uname.upper(), instance.needs_user.username.upper())
            batch_instance = OSInstallTask.objects.get_or_create(batch_name=batch, description=desc)[0]
            for sn in [sn for sn in instance.sn.split("\r\n") if sn != ""]:
                try:
                    d = Device.objects.get(sn=sn)
                except Device.DoesNotExist:
                    d = None
                try:
                    #判断装机任务是否已经存在 and 是否有人已经提交
                    os = OSInstallQueue.objects.get(Q(sn=d.sn) & ~Q(status="11"))
                except Exception, e:
                    os = None
                if not d.status and not os:
                    os = OSInstallQueue(sn=d.sn, tp_type=instance.tp_type, install=get_osinstall(d.sn),
                                        create_user=uname, ilo_ip=get_device_ip(d, "ilo"),
                                        os_ip=get_device_ip(d, "bond0"), eth0_mac=get_device_mac(d),
                                        needs_user=instance.needs_user, batch=batch_instance)
                    os.save()
                    d.status = True
                    d.save()
                else:
                    unavailable_device.append(sn)
            return HttpResponse(json.dumps({"code": 1, "message": {"unavailable_device": unavailable_device}}))
        else:
            return HttpResponse(json.dumps({"code": 0, "message": {"sn": form['sn'].errors,
                                                                   "needs_user": form["needs_user"].errors,
                                                                   "description": form["description"].errors}}))
Пример #8
0
    def handle(self, *args, **options):
        logger = logging.getLogger('sync_orders')
        time_started = time.time()
        n = 0
        while True:
            time.sleep(1)
            n = n + 1
            time_elapsed = time.time() - time_started
            if time_elapsed > 57.0:
                break

            for user in User.objects.filter(is_active=True):
                logger.info('bitbank注文の動機を開始します')
                prv_bb = python_bitbankcc.private(user.bb_api_key,
                                                  user.bb_api_secret_key)
                for pair in Relation.PAIR:
                    time.sleep(0.1)
                    to = datetime.now()
                    since = to - timedelta(seconds=10)
                    option = {
                        'count': 10,
                        'since': int(since.timestamp() * 1000),
                        'end': int(to.timestamp() * 1000)
                    }
                    # アクティブ注文の反映
                    try:
                        active = prv_bb.get_active_orders(pair, option)
                    except Exception as e:
                        logger.error('アクティブ注文の取得に失敗しました.{}'.format(str(
                            e.args)))
                        pass
                    else:
                        for o in active['orders']:
                            time.sleep(0.3)
                            # DBを検索
                            try:
                                exist = Order.objects.get(
                                    order_id=o['order_id'])
                            # DBにない場合は反映
                            except Order.DoesNotExist:
                                logger.info('新規注文:{}をDBへ反映します'.format(
                                    str(o['order_id'])))
                                o['market'] = 'bitbank'
                                o['order_type'] = o['type']
                                os = OrderSerializer(data=o,
                                                     context={'user': user})
                                if os.is_valid():
                                    o1 = os.save()
                                else:
                                    logger.error('パラメタの反映に失敗しました.{}'.format(
                                        str(os.errors)))
                                    continue

                                relation = Relation()
                                relation.user = user
                                relation.market = 'bitbank'
                                relation.pair = o['pair']
                                relation.special_order = 'SINGLE'
                                relation.order_1 = o1
                                relation.save()
                            else:
                                # DBに存在する場合は
                                pass
                    # 約定済み注文の反映
                    try:
                        history = prv_bb.get_trade_history(pair, option)
                    except Exception as e:
                        logger.error('注文履歴の取得に失敗しました.{}'.format(str(e.args)))
                        pass
                    else:
                        for o in history['trades']:
                            time.sleep(0.1)
                            try:
                                exist = Order.objects.filter(
                                    order_id=o['order_id'])
                            except Order.DoesNotExist:
                                logger.info('注文履歴 {} をDBに反映します'.format(
                                    str(o['order_id'])))
                                o['market'] = 'bitbank'
                                o['order_type'] = o['type']
                                o['status'] = Order.STATUS_FULLY_FILLED
                                o['ordered_at'] = o['executed_at']
                                o['start_amount'] = o['amount']
                                o['executed_amount'] = o['amount']
                                os = OrderSerializer(data=o,
                                                     context={'user': user})
                                if os.is_valid():
                                    o1 = os.save()
                                else:
                                    logger.error('パラメタエラー'.format(
                                        str(os.errors)))
                                    continue
                            else:
                                pass
                logger.info('coincheckの同期を開始します')
                prv_cc = CoinCheck(user.cc_api_key, user.cc_api_secret_key)
                pair = 'btc_jpy'

                pag = {'limit': 10, 'order': 'desc'}
                ao = json.loads(prv_cc.order.opens({}))
                if ao['success']:
                    for o in ao['orders']:
                        try:
                            exist = Order.objects.filter(order_id=o['id'])
                        except Order.DoesNotExist:
                            logger.info('新規注文 :{} を同期します'.format(str(o['id'])))
                            o['market'] = 'coincheck'
                            o['order_id'] = o['id']
                            o['side'] = 'sell' if 'sell' in o[
                                'order_type'] else 'buy'
                            o['order_type'] = 'market' if 'market' in o[
                                'order_type'] else 'limit'
                            o['price'] = o['rate']
                            o['start_amount'] = o['pending_amount']
                            o['status'] = Order.STATUS_UNFILLED
                            os = OrderSerializer(data=o,
                                                 context={'user': user})
                            if os.is_valid():
                                o1 = os.save()
                            else:
                                logger.error(str(os.errors))
                                continue
                            relation = Relation()
                            relation.user = user
                            relation.market = 'coincheck'
                            relation.pair = o['pair']
                            relation.special_order = 'SINGLE'
                            relation.order_1 = o1
                            relation.save()
                        else:
                            pass
                elif ao['error']:
                    logger.error('coincheckのアクティブ注文の動機に失敗しました.{}'.format(
                        str(ao['error'])))
                co = json.loads(prv_cc.order.transactions(pag))
                if co['success']:
                    for o in co['transactions']:
                        exist = Order.objects.filter(order_id=o['order_id'])
                        if len(exist) == 0:
                            logger.info('注文履歴{}を同期します'.format(
                                str(o[('order_id')])))
                            o['market'] = 'coincheck'
                            o['status'] = Order.STATUS_FULLY_FILLED

                            amount = float(
                                o['funds']
                                ['btc']) if o['side'] == 'buy' else -1 * float(
                                    o['funds']['btc'])
                            for o2 in co['transactions']:
                                if o['id'] != o2['id'] and o['order_id'] == o2[
                                        'order_id']:
                                    amount += float(
                                        o2['funds']['btc']
                                    ) if o2['side'] == 'buy' else -1 * float(
                                        o2['funds']['btc'])

                            o['executed_amount'] = amount
                            o['start_amount'] = amount
                            o['order_type'] = Order.TYPE_LIMIT
                            os = OrderSerializer(data=o,
                                                 context={'user': user})
                            if os.is_valid():
                                o1 = os.save()
                            else:
                                logger.error(str(os.errors))
                                continue
                elif co['errro']:
                    logger.error('coincheckの注文履歴の同期に失敗しました.'.format(
                        str(co['error'])))

        logger.info('done')
Пример #9
0
import os
import Tesseract
import gTTS

print(Tesseract.image_to_string(image.open()))
lang = "english"
spell = gTTS(lang=english, text=mytext, slow=True)
os.save("welcome.mp3")
os.system("mpg321 welcome.mp3")
Пример #10
0
    def do_create(self, args):
        """Create a new operating system model in Talus

		create -n NAME [--type TYPE] [-t TAG1,TAG2,..] [-v VERSION]

		   -n,--name    The name of the new OS model (required, no default)
		   -t,--type    The type of the OS mdoel (default: "windows")
		   -a,--arch	The architecture of the OS (default: "x64")
		-v,--version    The version of the new OS model (default: "")

		Examples:

		To create a new operating system model for an x64 Windows 7 OS:

		    os create -n "Windows 7 x64" -t windows -v 7 -a x64
		"""
        args = shlex.split(args)
        if self._go_interactive(args):
            os = OS()
            self._prep_model(os)
            os.version = ""
            os.arch = "x64"
            while True:
                model_cmd = self._make_model_cmd(os)
                cancelled = model_cmd.cmdloop()
                if cancelled:
                    break

                error = False
                if os.name is None or os.name.strip() == "":
                    self.err("You must give the OS a name")
                    error = True

                if os.type is None:
                    self.err("You must specify an os type (linux/windows)")
                    error = True
                elif os.type not in ["linux", "windows"]:
                    self.err(
                        "Sorry man, os.type must be one of 'linux' or 'windows'"
                    )
                    error = True

                if error:
                    continue

                try:
                    os.save()
                    self.ok("created new os {}".format(os.id))
                except errors.TalusApiError as e:
                    self.err(str(e))
                else:
                    break
            return

        parser = self._argparser()
        parser.add_argument("--name", "-n")
        parser.add_argument("--type", "-t", default="windows")
        parser.add_argument("--version", "-v", default="")
        parser.add_argument("--arch", "-a", default="x64")

        args = parser.parse_args(args)

        new_os = OS(self._talus_host)
        new_os.name = args.name
        new_os.type = args.type
        new_os.version = args.version
        new_os.arch = args.arch

        try:
            new_os.save()
            print("created")
        except talus_client.errors.TalusApiError as e:
            sys.stderr.write("Error saving OS: {}\n".format(e.message))
Пример #11
0
	def do_create(self, args):
		"""Create a new operating system model in Talus

		create -n NAME [--type TYPE] [-t TAG1,TAG2,..] [-v VERSION]

		   -n,--name    The name of the new OS model (required, no default)
		   -t,--type    The type of the OS mdoel (default: "windows")
		   -a,--arch	The architecture of the OS (default: "x64")
		-v,--version    The version of the new OS model (default: "")

		Examples:

		To create a new operating system model for an x64 Windows 7 OS:

		    os create -n "Windows 7 x64" -t windows -v 7 -a x64
		"""
		args = shlex.split(args)
		if self._go_interactive(args):
			os = OS()
			self._prep_model(os)
			os.version = ""
			os.arch = "x64"
			while True:
				model_cmd = self._make_model_cmd(os)
				cancelled = model_cmd.cmdloop()
				if cancelled:
					break

				error = False
				if os.name is None or os.name.strip() == "":
					self.err("You must give the OS a name")
					error = True

				if os.type is None:
					self.err("You must specify an os type (linux/windows)")
					error = True
				elif os.type not in ["linux", "windows"]:
					self.err("Sorry man, os.type must be one of 'linux' or 'windows'")
					error = True

				if error:
					continue

				try:
					os.save()
					self.ok("created new os {}".format(os.id))
				except errors.TalusApiError as e:
					self.err(str(e))
				else:
					break
			return

		parser = self._argparser()
		parser.add_argument("--name", "-n")
		parser.add_argument("--type", "-t", default="windows")
		parser.add_argument("--version", "-v", default="")
		parser.add_argument("--arch", "-a", default="x64")

		args = parser.parse_args(args)

		new_os = OS(self._talus_host)
		new_os.name = args.name
		new_os.type = args.type
		new_os.version = args.version
		new_os.arch = args.arch

		try:
			new_os.save()
			print("created")
		except talus_client.errors.TalusApiError as e:
			sys.stderr.write("Error saving OS: {}\n".format(e.message))
Пример #12
0
    def process(self):
        try:
            logging.debug('parsing scan results...')
            self.results = Parser.Parser(self.xml_results)
            session = self.results.get_session()
            if session is None:
                raise SessionError('Unable to read scan session')

            ''' taken from the original pInsertScan sproc:
            CREATE PROCEDURE pInsertScan(IN v_userid INT, IN v_version TEXT, IN v_args TEXT,
               IN v_startstr TEXT, IN v_endstr TEXT)
            BEGIN
            INSERT INTO scans (userId, version, args, startstr, endstr)
            values ( v_userid, v_version, v_args, v_startstr, v_endstr );
            SELECT @@identity;
            END '''
            logging.debug('building scan object')
            try:
                scan = Scan.objects.get(pk=self.scan_id)
            except Scan.DoesNotExist:
                scan = Scan()
            scan.user = User.objects.get(pk=int(self.user_id))
            scan.nmap_version = session.nmap_version
            scan.nmap_args = session.scan_args
            scan.end_time = datetime.datetime.strptime(session.finish_time, '%a %b %d %H:%M:%S %Y')
            scan.save()
            logging.debug('scanid is {0}'.format(scan.pk))

            # save host information
            for h in self.results.all_hosts():
                try:
                    ''' taken from the original pInsertHost sproc:
                    CREATE PROCEDURE pInsertHost(, IN v_sid INT, IN v_ip4 TEXT, IN v_hostname TEXT,
                        IN v_status TEXT, IN v_mac TEXT, IN v_vendor TEXT, IN v_ip6 TEXT, IN v_distance INT,
                        IN v_uptime TEXT, IN v_upstr TEXT)
                    BEGIN
                    INSERT INTO hosts ( sid, ip4, hostname, status, mac, vendor, ip6, distance, uptime,
                    upstr) VALUES ( v_sid, v_ip4, v_hostname, v_status, v_mac, v_vendor, v_ip6, v_distance,
                    v_uptime, v_upstr);
                    SELECT @@identity;
                    END '''
                    logging.debug('parsing host {0}'.format(h.ipv4))
                    host = Host()
                    host.scan = scan
                    host.ip4 = h.ipv4
                    host.hostname = h.hostname
                    host.status = h.status
                    host.mac = h.macaddr
                    # host.ip6 = h.ipv6
                    host.distance = h.distance
                    host.uptime = h.uptime
                    host.last_boot = h.lastboot
                    host.save()
                    logging.debug('hostid is {0}'.format(host.pk))

                    for os_node in h.get_OS():
                        ''' taken from the original pInsertOS sproc:
                        CREATE PROCEDURE pInsertOS (IN v_hid INT, IN v_name TEXT, IN v_family TEXT,
                            IN v_generation TEXT, IN v_type TEXT, IN v_vendor TEXT, IN v_accuracy INT)
                        BEGIN
                        INSERT INTO os (hid, name, family, generation, type, vendor, accuracy)
                        VALUES ( v_hid, v_name, v_family, v_generation, v_type, v_vendor, v_accuracy);
                        END '''
                        os = OperatingSystem()
                        os.name = os_node.name
                        os.family = os_node.family
                        os.generation = os_node.generation
                        os.os_type = os_node.os_type
                        os.vendor = os_node.vendor
                        os.save()
                        host.operating_system = os
                        host.save()

                    # parse TCP and UDP ports
                    self.parse_ports(h, host, proto='tcp')
                    self.parse_ports(h, host, proto='udp')

                    #parse script output
                    try:
                        nsp = NmapScriptParser()
                        for scr in h.get_scripts():
                            vulnId = nsp.parse_output(scr.scriptId, scr.output, host)
                    except Exception as ex:
                        logging.error('Error parsing script output:\n{0}'.format(ex))

                except Exception as ex:
                    logging.error('Error parsing host information.\n{0}'.format(ex))

            scan.state = Scan.COMPLETE
            scan.save()

            from scanner.tasks import send_scan_report # importing here prevents a circular reference
            send_scan_report.delay(scan.pk)

        except Exception as ex:
            logging.error('Error processing results:\n{0}'.format(ex))