Advertisement
Guest User

Untitled

a guest
Jan 11th, 2018
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.55 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3.  
  4. # Imports
  5. import sys
  6. import requests
  7. from bs4 import BeautifulSoup
  8. from termcolor import colored, cprint
  9. from version import VERSION
  10. import urllib2
  11. import re
  12.  
  13.  
  14. """
  15. Calling as follows:
  16. ./signin.py tableau01-dev.eastus2.cloudapp.azure.com dpanchal 'PWD' ghin
  17.  
  18.  
  19. """
  20.  
  21.  
  22. def main():
  23.     """
  24.    To automate this script then fill in the values for server, username, etc
  25.    You will be prompted for any values set to ""
  26.    Server and username can be entered on the command line as well.
  27.      users_by_group.py http://localhost username
  28.    """
  29.     # To automate the script fill in these fields
  30.     server = "http://localhost"
  31.     username = ""
  32.     password = ""
  33.     site_id = "Default"
  34.     group_name = "All"
  35.     page_size = 100
  36.  
  37.     # Parse arguments
  38.     if len(sys.argv) == 5:
  39.         server = sys.argv[1]
  40.         username = sys.argv[2]
  41.         password = sys.argv[3]
  42.         site_id = sys.argv[4]
  43.     else:
  44.         raise Exception('USAGE: signin.py SERVER USERNAME PASSWORD SITE_ID')
  45.  
  46.     # Start
  47.     cprint("Starting", "magenta")
  48.     print("{0}".format("-" * 80))
  49.  
  50.     # Creating xml to be used for sign-in
  51.     xml = """
  52.    <tsRequest>
  53.      <credentials name="{0}" password="{1}" >
  54.        <site contentUrl="{2}" />
  55.      </credentials>
  56.    </tsRequest>
  57.    """.format(username, password, site_id)
  58.  
  59.     # Sign-in
  60.     cprint("Initiating sign-in", "yellow", attrs=["bold"])
  61.     print("{0}".format("-" * 80))
  62.     headers = {"Content-Type": "application/xml"}
  63.     resp = requests.post(
  64.         "http://{0}/api/{1}/auth/signin"
  65.         .format(server, VERSION),
  66.         data=xml,
  67.         headers=headers)
  68.     soup = BeautifulSoup(resp.text, "html.parser")
  69.     print soup.prettify()
  70.  
  71.     # Extract token
  72.     cprint("Extracting values", "yellow", attrs=["bold"])
  73.     print("{0}".format("-" * 80))
  74.     token = soup.find("credentials")["token"]
  75.     if token is None:
  76.         raise Exception("Failed to extract token from sign-in post request.")
  77.  
  78.     # Extract site-id
  79.     site_id = soup.find("site")["id"]
  80.     if site_id is None:
  81.         raise Exception("Failed to extract site_id from sign-in post request.")
  82.  
  83.     # ServerInfo
  84.     cprint("ServerInfo", "yellow", attrs=["bold"])
  85.     print("{0}".format("-" * 80))
  86.     headers = {"X-Tableau-Auth": token, "Content-Type": "application/xml"}
  87.     resp = requests.get(
  88.         "http://{0}/api/{1}/serverinfo"
  89.         .format(server, VERSION),
  90.         headers=headers)
  91.     soup = BeautifulSoup(resp.text, "html.parser")
  92.     print soup.prettify()
  93.  
  94.     # Query sites
  95.     cprint("Sites", "yellow", attrs=["bold"])
  96.     print("{0}".format("-" * 80))
  97.     headers = {"X-Tableau-Auth": token, "Content-Type": "application/xml"}
  98.     resp = requests.get(
  99.         "http://{0}/api/{1}/sites"
  100.         .format(server, VERSION),
  101.         headers=headers)
  102.     soup = BeautifulSoup(resp.text, "html.parser")
  103.     print soup.prettify()
  104.  
  105.     # Query User On Site
  106.     cprint("Users On Site", "yellow", attrs=["bold"])
  107.     print("{0}".format("-" * 80))
  108.     headers = {"X-Tableau-Auth": token, "Content-Type": "application/xml"}
  109.     resp = requests.get(
  110.         "http://{0}/api/{1}/sites/{2}/users"
  111.         .format(server, VERSION, site_id),
  112.         headers=headers)
  113.     soup = BeautifulSoup(resp.text, "html.parser")
  114.     users = soup.find("users")
  115.     for user in users.findAll("user"):
  116.         print user["name"]
  117.  
  118.     # End
  119.     cprint("All done", "green", attrs=["bold"])
  120.     print("{0}".format("-" * 80))
  121.  
  122.  
  123. # Launch
  124. if __name__ == "__main__":
  125.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement