Skip to content

Instantly share code, notes, and snippets.

@niski84
Created January 6, 2023 07:19
Show Gist options
  • Save niski84/424b31c0a93dafb3c77e57af22d7e06d to your computer and use it in GitHub Desktop.
Save niski84/424b31c0a93dafb3c77e57af22d7e06d to your computer and use it in GitHub Desktop.
fetch jenkins login JSESSIONID
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