Somali insurgents disguised as police officers stormed a Mogadishu hotel, killing six lawmakers.

NAIROBI, Kenya — Somali insurgents disguised in government military uniforms stormed a Mogadishu hotel on Tuesday and killed at least 30 people, including 6 lawmakers, laying bare how vulnerable Somalia’s government is, even in an area it claims to control.

The insurgents methodically moved room to room, killing hotel guests who tried to bolt their doors shut, Somali officials said. When government forces finally cornered the insurgents, two blew themselves up with suicide vests.

The attack shows that “operational momentum has shifted to the insurgents, who can go anywhere they want except where the African peacekeepers are deployed,” said J. Peter Pham, senior vice president at the National Committee on American Foreign Policy.

Several Somali politicians said that the government was so thoroughly under siege that it could work only from behind fortified, sandbagged positions, and that the shrinking government enclave in Mogadishu, the capital, could soon vanish altogether.

“The problem is the government is not working hard on security; it’s the same old thing,” said Asha A. Abdalla, a member of Parliament who was in Nairobi during the attack. Like many others in the 550-member Somali Parliament, Mrs. Asha often stays in Kenya because of the dangers in Somalia.

“But I don’t know what the A.U. is doing, either,” she said, referring to the more than 6,000 African Union troops in Mogadishu. “If they are not protecting M.P.’s, who are they protecting?”

The most powerful insurgents are the Shabab, a militant Islamist group that has stoned civilians to death and pledged allegiance to Al Qaeda. The Shabab seem to be constantly two steps ahead of Somalia’s transitional government, analysts say, even though the government receives tens of millions of dollars in security aid from the United States and other Western countries.

American officials have said the government, however weak and disorganized, is the best bulwark against a Shabab-ruled Somalia, though the Shabab already rule much of Somalia.

The battle now seems to be turning to Mogadishu, specifically the few neighborhoods that the government still marginally controls, like the areas around the presidential palace, seaport and airport. This year, Somali government officials promised to sweep the Shabab out of the capital and expand their zone.

But government forces have been plagued by defections and apathy, Somali commanders concede, and it seems that the Shabab are the ones on the offensive. The hotel raid followed intense shelling against government positions on Monday, which killed dozens of people and sent shells crashing into camps for internally displaced people.

“There’s been fierce fighting and the government is getting pushed back,” said Abdirizak Farah, a shopkeeper who fled his home at 4 a.m. Tuesday to seek shelter closer to government troops.

The three-story hotel that was attacked, the Muna, was popular among Somali lawmakers because it was thought to be secure and was located less than a mile away from the presidential palace in a breezy seaside neighborhood. Witnesses said that a group of about three to five insurgents appeared at the gate at 10:30 a.m. wearing government military uniforms, and that as soon as the hotel guards opened the way for them, the gunmen opened fire.

They then rushed into the hotel corridors, shooting everyone in sight. Government forces arrived a few minutes later and battled the insurgents room by room, eventually pushing the gunmen to the upper floor. According to witnesses, several lawmakers tried to lock themselves in their rooms, but they were hunted down and shot at close range with assault rifles.

“They killed everyone they saw inside the hotel and then blew themselves up,” said Abdirahman Omar Osman, Somalia’s information minister. He called the attack “murder” and said it was “against Islamic religion,” especially during the holy month of Ramadan.

Another Somali official, who was not authorized to speak publicly, said the Shabab were “using all tactics.”

“They don’t care about Ramadan,” the official said. “They are criminals. They are terrorists.”

An 11-year-old shoeshine boy and a woman selling tea near the hotel were also killed, African Union officials said.

The hotel raid seemed to have been planned well in advance, and several residents living near the hotel said that Shabab fighters had been renting rooms for weeks in their neighborhood, leading them to expect a major attack.

A Shabab spokesman on Tuesday said that Shabab “special forces” were the ones who stormed the hotel. Earlier on Tuesday, the government claimed to have captured one of the attackers.

The last time the government was dealt such a deadly blow was in December, when the Shabab killed four government ministers in a suicide bombing at a medical school graduation in another hotel in the government zone.

Then in July, the Shabab claimed responsibility for killing dozens of World Cup fans in coordinated bombings in Uganda, saying it was revenge against Ugandan peacekeepers.

Analysts said that Tuesday’s raid on the hotel, though, was something different, with gunmen going toe-to-toe against government forces in an area teeming with government troops, which seemed to be a sign of increasingly brazen and confident insurgents.

Somalia has lurched from crisis to crisis since 1991, when the central government collapsed. Several Somali officials have conceded that if it were not for the African Union peacekeepers, the government would fall, most likely in hours.