Created
January 6, 2023 08:01
-
-
Save niski84/adc7b2af9f14862ac5d5f2bdcb1ed85b to your computer and use it in GitHub Desktop.
jenkins print login error
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" | |
"io/ioutil" | |
"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}, | |
} | |
req, err := http.NewRequest("POST", loginURL, strings.NewReader(data.Encode())) | |
if err != nil { | |
panic(err) | |
} | |
req.Header.Set("Content-Type", "application/x-www-form-urlencoded") | |
resp, err := client.Do(req) | |
if err != nil { | |
panic(err) | |
} | |
defer resp.Body.Close() | |
// Print the response body | |
body, err := ioutil.ReadAll(resp.Body) | |
if err != nil { | |
panic(err) | |
} | |
fmt.Println(string(body)) | |
// 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