He menaced cops with a machete — and the city paid him for it.

A Brooklyn thug who was shot by officers while waving an 18-inch blade at them and later sued the department for $3 million got a $5,000 settlement from the city — even though his own attorney said the shooting was probably justified.

Ruhim Ullah, 24, had pleaded guilty to menacing a police officer after the 2010 confrontation, in which he was shot once in the leg by a cop trying to stop him from attacking ­officers with the machete, according to his lawyer.

Despite his admission of guilt, Ullah still filed a $3 million lawsuit, accusing the officers of wrongdoing.

But despite witnesses who described him as armed, the city offered a $5,000 settlement to get rid of the flimsy suit, despite the message their action sends — that crime, and potentially deadly force against cops, can pay.

“In light of all of the circumstances, the city concluded that this agreement to settle was in the city’s best interest,” said city Law Department spokesman Nick Paolucci.

Police sources weren’t so sure the payout was in their best interest. “It’s sickening,” said a law enforcement source.

“This man pleaded guilty to a serious crime. That should have been enough to erase the civil suit.”

After the payout, even ­Ullah’s lawyer admitted he thought the officer may have done the right thing. “This may have been a justified shooting,” said attorney Scott Cerbin, who had argued in court papers that Ullah dropped the blade ­before he was shot.

Patrolmen’s Benevolent Association spokesman Al O’Leary blasted the payout.

“This is one of the many frivolous lawsuits that the city prefers to settle because it’s cheaper than defending it in court,” he said.

“It’s bad for our officers, who often don’t even know that there is a lawsuit.”

The settlement was one in a series of payouts the city Law ­Department has quickly made under Mayor Bill de Blasio.

City lawyers signed off on a $98 million settlement last March over a longstanding class-action discrimination case brought by minority FDNY applicants over unfair hiring practices.

The de Blasio administration also settled the notorious Central Park Five case for $41 million last September.

De Blasio also ponied up $18 million in taxpayer cash to protesters who sued the city after being arrested at the 2004 Republican National Convention.

Additional reporting by Jamie Schram and Erin Calabrese