Skip to content

Instantly share code, notes, and snippets.

@sgml
Forked from getkub/SSOByPasser.ksh
Created April 1, 2020 23:28
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 sgml/2a2c0915f5f4ce6f89d27891daf708c9 to your computer and use it in GitHub Desktop.
Save sgml/2a2c0915f5f4ce6f89d27891daf708c9 to your computer and use it in GitHub Desktop.
CURL snippet to do commands on SSO (Siteminder etc) protected systems
#!/usr/bin/ksh
#################################################################################
#
# CURL snippet to do commands on SSO (Siteminder etc) protected systems
# This script will set cookies in one server
# Copies the cookie information into another server
# Imports the cookie information to maintain seemless CURL commands
#
#################################################################################
page_name="ABC_Test"
dataFile="page.xml"
########################## Server 1 Variables
server1host="myServer1"
server1domain="https://myserver1.com/"
server1request="/page/export/name/${page_name}"
server1loginPath="location_of_SSO/"
server1logoutPath="mypage/logout"
server1user="getkub"
server1pass="mypassServer1"
# echo ${server1domain}${server1request}
########################## Server 2 Variables
server2host="myserver2"
server2domain="https://myserver2.com/"
server2request="some_request_path"
server2loginPath="location_of_SSO/"
server2logoutPath="mypage/logout"
server2user="getkub"
server2pass="mypassServer2"
# echo ${server2domain}${server2request}
##########################
######################### Main Script ###########################
#Request url - this is important as it returns the url we must return to
loginUri=`curl -s -I --cookie-jar tmpCookieFile --cookie tmpCookieFile --insecure ${server1domain}${server1request} | grep Location | sed "s/Location: //g"`
#Grab the service
service=`echo $loginUri | sed 's/^.*\?service=/\?service=/'`
#Perform the login
# Create your webURL dynamically
processLoginUri="${server1domain}${server1loginPath}${service}"
#Get the page
curl -s --insecure --cookie-jar tmpCookieFile --cookie tmpCookieFile --location --data "user=${server1user}&pass=${server1pass}" ${processLoginUri} > ${dataFile}
#If you wish to do any other commands you can do them here, ensure to keep the cookies in place
#Logout
curl -s --insecure -I --cookie-jar tmpCookieFile --cookie tmpCookieFile ${server1domain}${server1logoutPath} > /dev/null
scp ${dataFile} ${server2host}:`pwd`
#Probably not needed, but tidy up before we communicate with server2
rm tmpCookieFile
############################################################
# Start communication with server 2
#Request server 2's root, as we need to post the page, rest as before
loginUri=`curl -s -I --cookie-jar tmpCookieFile --cookie tmpCookieFile --insecure ${server2domain}${server2request} | grep Location | sed "s/Location: //g"`
service=`echo $loginUri | sed 's/^.*\?service=/\?service=/'`
processLoginUri="${server2domain}${server2loginPath}${service}"
curl -s --insecure --cookie-jar tmpCookieFile --cookie tmpCookieFile --location --data "user=${server2user}&pass=${server2pass}" ${processLoginUri} > /dev/null
#Delete the page first
curl -s -L --insecure --cookie-jar tmpCookieFile --cookie tmpCookieFile "${server2domain}${server2request}"page/delete/name/${page_name} >/dev/null
#Upload the page - title gets set from command line
curl -s --insecure --cookie-jar tmpCookieFile --cookie tmpCookieFile -F uploadFile=@${dataFile} "${server2domain}${server2request}"page/save
#Perform any other actions against server 2
#Logout
curl -s --insecure -I --cookie-jar tmpCookieFile --cookie tmpCookieFile ${server2domain}${server2logoutPath} > /dev/null
#Clean up
rm ${dataFile}
rm tmpCookieFile
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment