{"id":375,"date":"2021-08-18T11:30:37","date_gmt":"2021-08-18T08:30:37","guid":{"rendered":"https:\/\/1.cbm.ua\/?p=375"},"modified":"2021-10-11T23:28:04","modified_gmt":"2021-10-11T20:28:04","slug":"%d0%bf%d1%80%d0%b8%d0%bc%d0%b5%d1%80-%d0%ba%d0%b0%d0%ba-%d0%b2%d1%8b%d0%b3%d0%bb%d1%8f%d0%b4%d0%b8%d1%82-%d0%ba%d0%be%d0%b4-%d1%81-%d0%bf%d0%be%d0%b4%d1%81%d0%b2%d0%b5%d1%82%d0%ba%d0%be%d0%b9","status":"publish","type":"post","link":"https:\/\/1.cbm.ua\/?p=375","title":{"rendered":"\u042d\u0442\u043e \u043f\u0440\u0438\u043c\u0435\u0440 \u043a\u043e\u0434\u0430 \u0434\u043b\u044f \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u043e\u0431\u043e\u0440\u0443\u0434\u043e\u0432\u0430\u043d\u0438\u0435\u043c."},"content":{"rendered":"\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">#!\/usr\/bin\/env python3\n# \u042d\u0442\u043e \u043f\u0440\u0438\u043c\u0435\u0440 \u043a\u043e\u0434\u0430 \u0441\u0434\u0435\u043b\u0430\u043d\u043d\u044b\u0439 \u0444\u0440\u0438\u043b\u0430\u043d\u0441\u0435\u0440\u043e\u043c \u0434\u043b\u044f \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u043e\u0431\u043e\u0440\u0443\u0434\u043e\u0432\u0430\u043d\u0438\u0435\u043c (\u0440\u0430\u0431\u043e\u0442\u0430 \u043d\u0435 \u0434\u043e\u0434\u0435\u043b\u0430\u043d\u0430), \u043d\u043e \u043d\u0435 \u043f\u0440\u043e\u0432\u0435\u0440\u0435\u043d\u043d\u044b\u0439 \u043c\u043e\u0439.\n\n\nimport time\n\nimport pexpect\nimport logging\nfrom config import config\n\n\ndef send_pass(connection, passwd):\n    connection.sendline(passwd)\n    connection.expect('>')\n\n\ndef get_auth_ssh(ip, login, passwd):\n    logging.info('\u041f\u043e\u0434\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435 \u043a %s'.format(ip))\n    logging.debug('\u041b\u043e\u0433\u0438\u043d: \"{}\", \u043f\u0430\u0440\u043e\u043b\u044c: \"{}\"'.format(login, passwd))\n    connect_string = '\/usr\/bin\/ssh {}@{}'.format(login, ip)\n    try:\n        connection = pexpect.spawn(command=connect_string, encoding='utf-8', maxread=1000000)\n        connection.sendline('\/usr\/bin\/ssh {}@{}'.format(login, ip))\n        index = connection.expect(\n            ['Are you sure you want to continue connecting (yes\/no\/[fingerprint])?', '[Pp]assword', '>'])\n        if 0 == index:\n            \"\"\"\u041f\u043e\u0434\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435 \u043f\u0435\u0440\u0432\u044b\u0439 \u0440\u0430\u0437 \u043a \u044d\u0442\u043e\u043c\u0443 \u0443\u0441\u0442\u0440\u043e\u0439\u0441\u0442\u0432\u0443\"\"\"\n            connection.sendline('yes')\n            index = connection.expect(['>', '[Pp]assword'])\n            if 1 == index:\n                send_pass(connection, passwd)\n        else:\n            if 1 == index:\n                \"\"\"\u041f\u043e\u0434\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435 2 \u0438 \u0431\u043e\u043b\u0435\u0435 \u0440\u0430\u0437\"\"\"\n                send_pass(connection, passwd)\n        return connection\n    except Exception:\n        logging.info(connection)\n\n\ndef get_log(connection, count):\n    connection.sendline('\\n')\n    connection.expect('>')\n    connection.sendline('log print\\r')\n    time.sleep(2)\n    connection.expect('>')\n\n    buffer = connection.buffer.split('\\r\\n\\r')\n    print(\"buffer = '{}'\".format(buffer))\n    log = buffer[1].split(' \\r\\n')\n    for s in log:\n        s = s.replace('\\r\\n', '')\n    return log[-count:]\n\n\ndef main():\n    logging.basicConfig(level=logging.DEBUG, filename='log\/app.log', format='%(asctime)s - %(message)s')\n    logging.info('\u041f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0430 \u0437\u0430\u043f\u0443\u0449\u0435\u043d\u0430')\n    username = config['microtik']['username']\n    password = config['microtik']['password']\n    ip = config['microtik']['ip']\n    ssh = get_auth_ssh(ip, username, password)\n    log = get_log(connection=ssh, count=30)\n    for s in log:\n        print(s)\n    logging.info('\u041f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0430 \u043e\u0441\u0442\u0430\u043d\u043e\u0432\u043b\u0435\u043d\u0430.')\n\n\nif '__main__' == __name__:\n    main()\n<\/pre>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p># \u042d\u0442\u043e \u043f\u0440\u0438\u043c\u0435\u0440 \u043a\u043e\u0434\u0430 \u0441\u0434\u0435\u043b\u0430\u043d\u043d\u044b\u0439 \u0444\u0440\u0438\u043b\u0430\u043d\u0441\u0435\u0440\u043e\u043c \u0434\u043b\u044f \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u043e\u0431\u043e\u0440\u0443\u0434\u043e\u0432\u0430\u043d\u0438\u0435\u043c (\u0440\u0430\u0431\u043e\u0442\u0430 \u043d\u0435 \u0434\u043e\u0434\u0435\u043b\u0430\u043d\u0430), \u0438 \u043a\u043e\u0434 \u043d\u0435 \u043f\u0440\u043e\u0432\u0435\u0440\u0435\u043d\u043d\u044b\u0439 \u043c\u043e\u0439.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"neve_meta_sidebar":"","neve_meta_container":"","neve_meta_enable_content_width":"","neve_meta_content_width":0,"neve_meta_title_alignment":"","neve_meta_author_avatar":"","neve_post_elements_order":"","neve_meta_disable_header":"","neve_meta_disable_footer":"","neve_meta_disable_title":"","footnotes":""},"categories":[1,21,69,2,19,23],"tags":[14,25],"class_list":["post-375","post","type-post","status-publish","format-standard","hentry","category-1","category-startup_und_pr","category-69","category-python","category-administration","category-iot","tag-startup","tag-help"],"_links":{"self":[{"href":"https:\/\/1.cbm.ua\/index.php?rest_route=\/wp\/v2\/posts\/375","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/1.cbm.ua\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/1.cbm.ua\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/1.cbm.ua\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/1.cbm.ua\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=375"}],"version-history":[{"count":7,"href":"https:\/\/1.cbm.ua\/index.php?rest_route=\/wp\/v2\/posts\/375\/revisions"}],"predecessor-version":[{"id":931,"href":"https:\/\/1.cbm.ua\/index.php?rest_route=\/wp\/v2\/posts\/375\/revisions\/931"}],"wp:attachment":[{"href":"https:\/\/1.cbm.ua\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=375"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/1.cbm.ua\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=375"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/1.cbm.ua\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=375"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}