Last active
October 8, 2017 13:10
-
-
Save sky-joker/582cd8d1aea0a2ef90c3a556098c7711 to your computer and use it in GitHub Desktop.
Cisco IMC(CIMC)へのログイン・ログアウトするスクリプト
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import xmltodict | |
import requests | |
from requests.packages.urllib3.exceptions import InsecureRequestWarning | |
requests.packages.urllib3.disable_warnings(InsecureRequestWarning) | |
def login(url="", user="", password=""): | |
""" | |
:rtype: str | |
:return: cookie | |
""" | |
# Create Header. | |
headers = { 'content-type': 'application/x-www-form-urlencoded' } | |
# Create Request Body. | |
request_body = {} | |
request_body['aaaLogin'] = {} | |
request_body['aaaLogin']['@inName'] = user | |
request_body['aaaLogin']['@inPassword'] = password | |
# Login | |
r = requests.post(url, headers=headers, data=xmltodict.unparse(request_body), verify=False) | |
# return Cookie | |
return xmltodict.parse(r.text)['aaaLogin']['@outCookie'] | |
def logout(url="", cookie=""): | |
""" | |
:rtype: str | |
:return: logout result | |
""" | |
# Create Header. | |
headers = { 'content-type': 'application/x-www-form-urlencoded' } | |
# Create Request Body. | |
request_body = {} | |
request_body['aaaLogout'] = {} | |
request_body['aaaLogout']['@cookie'] = cookie | |
request_body['aaaLogout']['@inCookie'] = cookie | |
# Logout | |
r = requests.post(url, headers=headers, data=xmltodict.unparse(request_body), verify=False) | |
# <aaaLogout cookie="cookie" response="yes" outStatus="success"> </aaaLogout> | |
if(r.status_code == 200): | |
return xmltodict.parse(r.text)['aaaLogout']['@outStatus'] | |
if __name__ == "__main__": | |
url="https://192.168.1.56/nuova" | |
user="admin" | |
password="password" | |
# Get Cookie. | |
r = login(url=url, user=user, password=password) | |
print("cookie: %s" % r) | |
# logout | |
r = logout(url=url, cookie=r) | |
print("Logout: %s" % r) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment