Baghdad (AFP) - Iran has worked to turn sweeping anti-government protests in Iraq from a threat to its hard-earned influence over its neighbour into an opportunity for political gains, analysts say.

In Lebanon too, where similar rallies against corruption and government inefficiency have broken out, Iran's main ally Hezbollah has managed to maintain its influence.

"Very clearly, Iran in both Lebanon and Iraq wants to protect the system and not allow it to fall apart," said Renad Mansour, researcher at London-based Chatham House.

In both countries "it considers the demands of protesters potentially destabilising," he told AFP.

In Iraq, many demonstrators calling for an overhaul to the political system over the past month have pointed at Tehran as its primary sponsor -- a worrying accusation for Iranian officials.

The leaders in Iran "are probably at peak influence and don't want anything to change, because it's exactly where they want to be," said Michael Knights, a senior fellow at the Washington Institute.

For decades, Iran has carefully crafted ties to a vast range of Iraq political and military actors, from Shiite opponents of ex-dictator Saddam Hussein to Kurdish factions in the north and even Sunni tribes in the west.

It therefore can play a crucial mediating role in Iraq's political crises, and Qasem Soleimani, who heads the Iranian Revolutionary Guards' elite Quds Force, often visits Baghdad during such times.

Tehran also backs many of the factions in Iraq's Hashed al-Shaabi paramilitary force, which was formed in 2014 to fight the Islamic State group.

And it sells crucial electricity and natural gas to supplement Iraq's gutted power sector and is Baghdad's second-biggest source of other imports, from fruit to carpets and cars.

- 'Palm of Iran's hand' -

The political and economic sway is perhaps more valuable than ever amid Washington's efforts to isolate and economically handicap Iran.

The leaders in Tehran "have absolutely everything to lose and will do anything to defend it," said Knights.

Story continues

"In the course of that, they are exposing their hand and their allies, and building even greater anger towards them."

Since protests erupted on October 1 in Iraq, many demonstrators have accused Iran of propping up the corrupt, inefficient system they want to overthrow.

One in five Iraqis live below the poverty line and youth unemployment stands at 25 percent, despite the vast oil wealth of OPEC's second-largest crude producer.

"All our leaders are in the palm of Iran's hand," said Azhar, a 21-year-old protester in Baghdad's Tahrir Square.

In unprecedented displays of anti-Iran sentiment, demonstrators chanted "Out, out, Iran! Baghdad will stay free!"

Online footage showed Iraqis hitting pictures of Soleimani with their shoes, a severe insult in the region.

The criticism caught Iran's attention, and Soleimani has visited Iraq multiple times over the past five weeks to "advise" factions on how to respond, sources told AFP.

"He's running the show," said a government official.

"They agreed on a way to deal with protesters that allows the current political leadership to stay," another source with knowledge of Soleimani's visits said.

One such meeting blocked a potential deal between paramilitary chief Hadi al-Ameri and populist cleric Moqtada Sadr to oust Prime Minister Adel Abdel Mahdi, sources said.

But parties appear to have closed rank around the embattled premier again, maintaining the status quo.

And amid the chaos of protests, several military commanders seen as close to the United States have been sidelined.

"Iran has tightened its grip considerably and become much more bold," said Knights.

- Lebanon erupts -

A week before the resumption of Iraq's anti-government rallies on October 24, another Middle Eastern country erupted in anger at systemic corruption: Lebanon.

Lebanon's government is dominated by the allies of Shiite armed movement Hezbollah, through which Iran exerts significant influence.

"Hezbollah has never had it this good," said Amal Saad, a Hezbollah expert and professor at the Lebanese University.

But after rallies unexpectedly erupted across Lebanon, including in the party's strongholds, Hezbollah now has to deal with forming a new government after the previous one resigned.

"Hezbollah and Iran are in a precarious situation", Saad said, "because of the government's resignation and efforts to boot them out."

Criticism of the movement's chief Hassan Nasrallah even aired on the Hezbollah-run Al-Manar TV, which was previously unimaginable for its propaganda arm.

After initially backing the protests, Nasrallah said his party would not back the government's resignation, which he said would lead to a dangerous political vacuum.

Party loyalists have also launched counter-demonstrations, sparking scuffles with protesters and journalists.

Despite the initial threat, said analyst Qassem Qassir, the party is as strong as ever.

"It may have lost some morale or taken a hit in the media, but its strength remains," Qassir told AFP.