FORT BRAGG, N.C. (AP) — Army Sgt. Bowe Bergdahl, who was captured and held by the Taliban for five years after walking away from his post in Afghanistan, pleaded guilty Monday to desertion and misbehavior-before-the-enemy charges that could put him behind bars for the rest of his life.

“I understand that leaving was against the law,” said Bergdahl, who admitted guilt without striking a deal with prosecutors, meaning his punishment will be up to a military judge when he is sentenced later this month.

READ MORE: Former Baltimore County Youth Pastor Charged In 30-Year-Old Rape, Police Looking For Other Victims

The guilty plea brings the highly politicized saga closer to an end eight years after Bergdahl vanished.

President Barack Obama brought him home in 2014 in a swap for five Taliban prisoners at Guantanamo Bay, saying the U.S. does not leave its service members on the battlefield. Republicans roundly criticized Obama, and Donald Trump went further while campaigning for president, repeatedly calling Bergdahl a “dirty, rotten traitor” who deserved to be executed by firing squad or thrown out of a plane without a parachute.

Bergdahl, 31, has said he walked away from his remote post in 2009 with the intention of reaching other commanders and drawing attention to what he saw as problems with his unit.

He told the judge, Col. Jeffrey R. Nance, that he now understands that his actions prompted an intensive search during which some of his comrades were seriously wounded.

“At the time, I had no intention of causing search and recovery operations,” he said in court. “I believed they would notice me missing, but I didn’t believe they would have reason to search for one private.”

Bergdahl was a private first-class when he vanished and received the promotions due all soldiers missing in action while he was in captivity.

The misbehavior charge carries a maximum penalty of life in prison, while the desertion charge is punishable by up to five years.

Bergdahl’s answers to the judge’s questions represented some of his most extensive public comments yet.

He told the judge that he tried to escape from his captors 12 to 15 times with varying degrees of success. Once, he was on his own for about a week — hoping U.S. drones would spot him — before he was recaptured. He said he also tried to escape on his first day in captivity.

READ MORE: School Of Magic Arts In Baltimore Gives Students Life Lessons Along With A Few Tricks

“As I started running there came shouts, and I was tackled by people. That didn’t go so well,” he said.

He also reflected on what he thought were questionable tactics by U.S. soldiers and their Afghan allies to guard a remote crossroads that could be bypassed by the Taliban on other routes. He said the setup “seemed to be a bit of a joke.”

Pressed by the judge about his actions, Bergdahl acknowledged endangering his fellow service members.

“I left my platoon in a battlefield … a situation that could easily turn into a life-or-death situation,” he said.

At his sentencing, his years in captivity could be factored in, but the hearing is also likely to feature damning testimony from his comrades.

A Navy SEAL who suffered a career-ending leg wound and an Army National Guard sergeant whose head wound put him in a wheelchair would not have been hurt in firefights had they not been searching for Bergdahl, the judge has ruled.

Earlier this year, the defense was rebuffed in an effort to prove that Trump had unfairly swayed the case. The judge ruled in February that the new president’s comments were “disturbing and disappointing” but did not constitute unlawful influence by the soon-to-be commander in chief.

Bergdahl, who is from Hailey, Idaho, has been assigned to desk duty at a Texas Army base while his case unfolds.

Follow @CBSBaltimore on Twitter and like WJZ-TV | CBS Baltimore on Facebook

MORE NEWS: Baltimore Mayor Brandon Scott Among Leaders To Join President Joe Biden For Crime Prevention Roundtable

(© Copyright 2017 The Associated Press. All Rights Reserved. This material may not be published, broadcast, rewritten or redistributed.)