JERUSALEM — The sudden escalation of violence between Israel and the Palestinians in Gaza could offer a boon to the leaders of both camps — Prime Minister Benjamin Netanyahu and Hamas militants — who have been struggling and scraping to salvage their political standing.

For Netanyahu, the escalation of fighting brought a last-minute reprieve from what could have been the end of his record run at the top of Israeli politics. Earlier this week, a group of rival political parties were reportedly within days — even hours — of forming Israel’s first government not to include Netanyahu in 12 years.

Just before tensions exploded Monday, a disparate collection of Israeli right-wing, centrist and left-wing parties, united only in their goal of ousting Netanyahu, indicated they were nearly ready to announce they had the support of a majority of parliament members, clearing the way to form a governing coalition.

By the end of the day, the political process had come to a halt. For now, amid the unrest, the prospect of a non-Netanyahu government is frozen and, political observers said, may be out of reach entirely.

“They were just about to call the president and say we have reached a deal, we have a coalition,” said Gayil Talshir, professor of political science at Hebrew University. “The riot came just in time to prevent the change of government in Israel.”

For Hamas, which controls Gaza, the conflict allows the group to position itself as the champion of Palestinians in Jerusalem at the expense of its rival faction, the Fatah party of Palestinian Authority President Mahmoud Abbas that governs in the Israeli-occupied West Bank.

Hamas began firing rockets into Israel on Monday after clashes between Palestinians and Israeli security peaked in Jerusalem. Hundreds of Palestinians were injured during protests at al-Aqsa Mosque, which is the third-holiest site in Islam, and elsewhere in the contested Old City. Israel responded to the initial rockets with withering airstrikes against hundreds of targets in the Gaza Strip.

If Netanyahu is struggling to extend his rule by months, Yahya Sinwar and his fellow Hamas leaders in Gaza are playing a longer game and, according to Palestinian analysts, stand to benefit from the confrontation with Israel.

The escalation of violence began Monday with an ultimatum from the group that Israel withdraw its security forces from al-Aqsa Mosque, where police had battled with worshipers. When Hamas fired seven rockets at the vicinity of Jerusalem, the militants said they were defending the mosque and also standing up for Palestinian families facing evictions by Israelis in the nearby East Jerusalem neighborhood of Sheikh Jarrah.

Political and security analysts said Hamas, which objected when Abbas last month canceled Palestinian elections, will try to use its improved standing against the 85-year-old Palestinian president, who has ruled for 16 years.

Already, according to Palestinian analysts, Hamas’s actions are winning favor among Palestinian voters in Gaza, the West Bank and the Arab neighborhoods of East Jerusalem.

“From preliminary indicators, many people in the West Bank are admiring what Hamas is doing,” said a Ramallah-based pollster and former Palestinian Authority official. “We don’t know their motivations, but it was effective of Hamas to confront Israel on the basis of their support for the Palestinian people of Jerusalem.”

Some Israeli security officials are now questioning whether their own government has been too soft toward Hamas, which both Israel and the United States consider a terrorist group. These officials cite Israel’s decision to let Qatar send money to Gaza in support of the local economy and limiting retaliatory strikes against sporadic rocket launches from the Palestinian enclave.

“I think Israel will have to reevaluate Prime Minister Netanyahu’s policy vis-a-vis Hamas, which was basically giving them priority in our relations with the Palestinian Authority,” said Amos Yadlin, a former air force general and military intelligence chief. “Hamas has decided to position itself as the defender of Palestinian Jerusalem. This is a new policy, something that Israel missed.”

Meanwhile, the clock is ticking for Israel’s parliamentary dealmakers. Under the law, the anti-Netanyahu faction has until early June to form a government.

Israeli party leaders — including former defense minister Naftali Bennett and opposition leader Yair Lapid — are still in private talks, according to local media, but publicly their attention is on the conflict, which has killed dozens of Gazans and seven Israelis, according to health and military officials. Bennett on Tuesday visited a house where an Israeli resident was killed by a rocket. 

The outbreak of violence has also complicated the efforts of Netanyahu’s rivals to make common cause. Assembling a majority coalition probably hinges on support from one of Israel’s small Arab parties, an alliance that would be controversial with many Israelis at any time but even more so during the current mayhem. Jewish and Arab politicians alike must balance their desire to form a government against the potential anger of the electorate if they are seen as dealing with “the enemy.”

Already, Mansour Abbas, the Islamist party leader who was negotiating with Lapid and Bennett, is facing blowback from Arab Israelis protesting nightly against the Israeli police and military.

“He was riding high. Now it’s the exact opposite,” said Diana Buttu, a Haifa-based lawyer active in Arab-Israeli politics. “He hasn’t shown his face at any of these protests.”

There are reports that Netanyahu is trying to persuade reluctant rivals to join him an emergency unity government to confront the Gaza crisis.

But if no coalition can be formed — Netanyahu has already failed to assemble one of his own — he could welcome the prospect of Israel going back to national elections again, its fifth in just more than two years.

“I think that is Netanyahu’s preferred option,” Talshir said. “If we go to a fifth election, he remains the prime minister of Israel for at least the next three or four months, and then maybe the security situation will help him.”