Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- # -*- coding: utf-8 -*-
- # Imports
- import sys
- import requests
- from bs4 import BeautifulSoup
- from termcolor import colored, cprint
- from version import VERSION
- import urllib2
- import re
- """
- Calling as follows:
- ./signin.py tableau01-dev.eastus2.cloudapp.azure.com dpanchal 'PWD' ghin
- """
- def main():
- """
- To automate this script then fill in the values for server, username, etc
- You will be prompted for any values set to ""
- Server and username can be entered on the command line as well.
- users_by_group.py http://localhost username
- """
- # To automate the script fill in these fields
- server = "http://localhost"
- username = ""
- password = ""
- site_id = "Default"
- group_name = "All"
- page_size = 100
- # Parse arguments
- if len(sys.argv) == 5:
- server = sys.argv[1]
- username = sys.argv[2]
- password = sys.argv[3]
- site_id = sys.argv[4]
- else:
- raise Exception('USAGE: signin.py SERVER USERNAME PASSWORD SITE_ID')
- # Start
- cprint("Starting", "magenta")
- print("{0}".format("-" * 80))
- # Creating xml to be used for sign-in
- xml = """
- <tsRequest>
- <credentials name="{0}" password="{1}" >
- <site contentUrl="{2}" />
- </credentials>
- </tsRequest>
- """.format(username, password, site_id)
- # Sign-in
- cprint("Initiating sign-in", "yellow", attrs=["bold"])
- print("{0}".format("-" * 80))
- headers = {"Content-Type": "application/xml"}
- resp = requests.post(
- "http://{0}/api/{1}/auth/signin"
- .format(server, VERSION),
- data=xml,
- headers=headers)
- soup = BeautifulSoup(resp.text, "html.parser")
- print soup.prettify()
- # Extract token
- cprint("Extracting values", "yellow", attrs=["bold"])
- print("{0}".format("-" * 80))
- token = soup.find("credentials")["token"]
- if token is None:
- raise Exception("Failed to extract token from sign-in post request.")
- # Extract site-id
- site_id = soup.find("site")["id"]
- if site_id is None:
- raise Exception("Failed to extract site_id from sign-in post request.")
- # ServerInfo
- cprint("ServerInfo", "yellow", attrs=["bold"])
- print("{0}".format("-" * 80))
- headers = {"X-Tableau-Auth": token, "Content-Type": "application/xml"}
- resp = requests.get(
- "http://{0}/api/{1}/serverinfo"
- .format(server, VERSION),
- headers=headers)
- soup = BeautifulSoup(resp.text, "html.parser")
- print soup.prettify()
- # Query sites
- cprint("Sites", "yellow", attrs=["bold"])
- print("{0}".format("-" * 80))
- headers = {"X-Tableau-Auth": token, "Content-Type": "application/xml"}
- resp = requests.get(
- "http://{0}/api/{1}/sites"
- .format(server, VERSION),
- headers=headers)
- soup = BeautifulSoup(resp.text, "html.parser")
- print soup.prettify()
- # Query User On Site
- cprint("Users On Site", "yellow", attrs=["bold"])
- print("{0}".format("-" * 80))
- headers = {"X-Tableau-Auth": token, "Content-Type": "application/xml"}
- resp = requests.get(
- "http://{0}/api/{1}/sites/{2}/users"
- .format(server, VERSION, site_id),
- headers=headers)
- soup = BeautifulSoup(resp.text, "html.parser")
- users = soup.find("users")
- for user in users.findAll("user"):
- print user["name"]
- # End
- cprint("All done", "green", attrs=["bold"])
- print("{0}".format("-" * 80))
- # Launch
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement