Skip to content

Instantly share code, notes, and snippets.

@sky-joker
Last active October 8, 2017 13:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sky-joker/582cd8d1aea0a2ef90c3a556098c7711 to your computer and use it in GitHub Desktop.
Save sky-joker/582cd8d1aea0a2ef90c3a556098c7711 to your computer and use it in GitHub Desktop.
Cisco IMC(CIMC)へのログイン・ログアウトするスクリプト
#!/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