Created
January 6, 2023 07:19
-
-
Save niski84/424b31c0a93dafb3c77e57af22d7e06d to your computer and use it in GitHub Desktop.
fetch jenkins login JSESSIONID
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
package main | |
import ( | |
"fmt" | |
"net/http" | |
"net/http/cookiejar" | |
"net/url" | |
) | |
func main() { | |
// Create an HTTP client with a cookie jar | |
jar, err := cookiejar.New(nil) | |
if err != nil { | |
panic(err) | |
} | |
client := &http.Client{Jar: jar} | |
// Authenticate to Jenkins | |
username := "your-username" | |
password := "your-password" | |
loginURL := "http://jenkins.example.com/j_acegi_security_check" | |
data := url.Values{ | |
"j_username": {username}, | |
"j_password": {password}, | |
} | |
resp, err := client.PostForm(loginURL, data) | |
if err != nil { | |
panic(err) | |
} | |
defer resp.Body.Close() | |
// Get the session ID from the cookie | |
for _, cookie := range jar.Cookies(resp.Request.URL) { | |
if cookie.Name == "JSESSIONID" { | |
fmt.Println("Session ID:", cookie.Value) | |
break | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment