The announcement had echoes of the tit-for-tat reprisals that were common during the Cold War, and the government of Vladimir V. Putin announced within hours that there would be a swift response. That seemingly ensured that the Obama administration’s final days would be consumed by escalating accusations and retaliation between Washington and Moscow.
The Obama administration offered no proof on Thursday linking the two compounds or the Russians being expelled to intelligence activities, and veterans of the spy games offered mixed assessments of the administration’s actions.
“I think these sanctions are pretty weak. It’s more perhaps symbolic,” said Steven Hall, a former senior C.I.A. official who ran Russia operations until his retirement in 2015.
But Mr. Hall said that the other measures could cause more irritation in Moscow. He said that he could not recall a previous move similar to the shuttering of the two compounds — one on Long Island and the other on the Eastern Shore of Maryland — and that expelling the 35 officers and their families “will slow down Russia’s activities in the United States.”
One former senior American official said that the group, made up of officials working at the embassy in Washington and the consulate in San Francisco, makes up about a third of the suspected Russian intelligence officers operating in the United States under diplomatic cover. It is unclear how many of the expelled Russians spent time at the two compounds.
The dismissals are believed to be the largest expulsion of Russian officials from the United States since 2001, when about 50 suspected Russian intelligence officers were forced to leave after Robert Hanssen, a senior F.B.I. official, was arrested and charged with spying for Moscow.
“I would be flabbergasted if the Russians didn’t reciprocate and expel 35 American officials from Russia,” Mr. Hall said. The C.I.A. has long posted undercover officers in Russia — and nearly every other country in the world — who pose as diplomats, businessmen or other professionals.
It is unclear whether any of the Russians being expelled played a part in the hacking that interfered with the American election. Law enforcement officials said that the White House and the State Department had come up with the number 35 and had then turned to the F.B.I. to list Russian officials in the United States who it believed were actually intelligence officers.
While the expulsions send a powerful message, they will force the F.B.I. to go back to work trying to identify a new crop of spies the Russians will almost certainly send to the United States.
After the expulsions were announced, officials described a pattern of harassment of American government employees working in Russia. In June, a uniformed officer with Russia’s Federal Security Service, called the F.S.B., attacked an employee of the United States Embassy in Moscow seemingly without provocation.
“Our diplomats have experienced an unacceptable level of harassment in Moscow by Russian security services and police over the last year,” President Obama said in a statement on Thursday.
Before this year, the most notable recent episode to become public was in 2013, when Russian officials arrested Ryan C. Fogle, an employee of the embassy in Moscow who the Russian government had discovered was a C.I.A. officer.
Mr. Fogle, officially posted in Russia as the third secretary of the embassy’s political department, was picked up on the street wearing a blond wig under a baseball cap. He was carrying a knapsack holding a compass, a Moscow street atlas and $130,000 in cash, money that Russian officials said was meant to be used to recruit a Russian security officer as a spy.
The episode’s amusing details garnered much of the attention, but Mr. Fogle’s arrest and expulsion from Russia was a public indicator that the spying activity between the United States and Russia was heating up — and in many ways had never ended. The F.S.B. took the unusual step of releasing a video showing Mr. Fogle facedown on a street as a Russian officer pinned his hands behind his back.
American law enforcement and intelligence agencies have little indication that the compounds in Maryland and New York played any role in the cyberattacks against the Democratic National Committee and other political organizations.
The Long Island retreat is a hulking 49-room house in Glen Cove called Killenworth that was once the home of the industrialist George duPont Pratt. The estate is often nearly empty, according to law enforcement officials, inhabited year-round by just a few Russian caretakers.
Protected by a barbed-wire fence, the mansion has stone peaks that poke into the sky. Thick brush obstructs the view of the bottom half of the house.
Angelina Izzo, who works as a receptionist at a health care center next to the estate, said the mansion was “very quiet.”
The Russian government has owned the estate since the days of the Soviet Union, and relations between the owners and local residents have at times been strained. In 1982, Reagan administration officials said Russians were using the mansion to conduct electronic surveillance of Long Island’s defense and technology industries.
The Glen Cove City Council responded by barring the Russians from obtaining free beach parking stickers and discounted tennis permits.
The Council’s lone dissenting voice described taking away the tennis permits as “petty” and said that “it’s a matter the professionals at the State Department should handle.”
The Maryland compound is a sprawling complex of several buildings fronting the Corsica River in Centreville, including a three-story brick Georgian-style mansion that has long been a retreat for Russia’s ambassador to Washington. It has a swimming pool, a soccer field and lighted tennis courts.
Anatoly Dobrynin, who served as ambassador to Washington from the Kennedy administration to the Reagan administration, was frequently at the estate, and the Russian government held on to the property after the collapse of the Soviet Union.
Unlike on Long Island, relations between the Russians visiting the Maryland compound and local residents have generally been placid — even if the Russians did not always adopt the local folkways.
Julie Patterson, 44, who has lived on a horse farm near the compound for 12 years, said the Russians largely kept to themselves but were cordial neighbors, inviting locals to an annual Labor Day party.
She said that she believed that the property was largely used as a weekend retreat and that she often saw children, families and buses heading to the estate. She said people often held parties there and hosted a sailing club.
In 1992, a Centreville resident told a reporter for The Associated Press that the Russians did not cook crabs the local way, by throwing live crabs into a pot of boiling water.
“They stab them with a screwdriver, break the back shell off, clean them and then boil the body,” she said.