Pakistan has begun cracking down on activities of Soviet diplomats based here in an attempt to stem a Moscow-inspired campaign attacking the government of President Mohammed Zia ul-Haq.

The government ordered the Soviet press and information office in Karachi to close and said the number of Soviet diplomats attached to the embassy here should be halved.

At the same time, with close to 100,000 Soviet troops on its border in Afghanistan, the Pakistanis made an apparent attempt to soften the blow against Moscow by ordering new travel bans on all diplomats stationed in the country, while saying privately the restrictions are aimed solely at the Eastern Bloc nations.

Government officials today refused to give any reason for the travel restrictions, which force diplomats to apply for permission seven days in advance for any trips outside of the city to which they are assigned.

But officials in the Ministry of External Affairs have been telling Western diplomats at receptions this weekend that the restrictions are not aimed at them and will be applied only to the Soviets and other Eastern Bloc nations. According to one European ambassador, government officials said that Westerners can ignore the restrictions and not even bother to request the permission.

Nonetheless, the diplomatic corps here plans to make a formal protest to the Pakistani government about the travel restrictions.

"Pakistan is passing through very troubled times. The Western diplomats should understand that," one Pakistani official said.

The Soviet Union has openly threatened Pakistan for its strong opposition to Moscow's invasion of Afghanistan in late December and its inability to keep rebel forces from moving between the two countries.

Radio Moscow and Tass, the Soviet news agency, regularly assail Pakistan as "a lackey of American imperialists and Chinese chauvinists," and Soviet Foreign Minister Andrei Gromyko warned Islamabad in January against helping the Afghan rebels. According to reports reaching here this week, the Soviet ambassador in the Afghan capital of Kabul told a Western European diplomat that if Pakistan does not close its border to rebel forces, the Soviets have ways of forcing the closure.

As if to underscore those press and diplomatic warnings, Soviet jets have made numerous incursions over the Pakistani border during the past year. In many cases, the Pakistanis did not find out until it was too late.But in a few, according to sources here, they had a chance to scramble their own jets and held back.

Once in the spring, however, Pakistani jets tried to force a slow-moving Soviet transport plane that strayed over its territory to land at the airfield in the Khyber Pass city of Peshawar. But just as the plane was approaching the field, two Soviet Migs flew up and all three planes went back to Afghanistan.

Neither the propaganda attacks nor the violations of its airspace provoked a Pakistani protest.

What did provoke the Pakistanis, according to diplomatic sources here, were the repeated Tass attacks on the stability of the Zia government in magazines and newspapers published by the Soviets in Karachi.

"They were spreading misinformation," said one Pakistani official who would not allow himself to be named.

Moreover, the Pakistani official said, the government feared that the Soviets had embarked on a campaign of subversion here. A series of night letters spread around the politically active city of Lahore in the spring appeared to be a Soviet effort, Pakistani officials and diplomatic sources said.

Today the government announced that it uncovered a longstanding underground secret group in Karachi and the nearby city of Hyderabad that was putting out "clandestine" literature aimed at undercutting the Zia administration.

The announcement said the activities were financed by an unnamed foreign power but it listed the title of one of the publications as Red Banner, an indication to diplomats and Pakistanis that the Soviets were behind the operation.

According to nonaligned Asian and Western diplomats here, the Soviets have been exceptionally heavy-handed in their dealings with Pakistan.The former Soviet ambassador here, S. A. Azimov, was reported to have tried to "bully" Zia and to have made "threatening remarks" to other Pakistani officials. It has been rumored here and in New Delhi that Azimov was asked to leave by the Pakistani government.

The Soviets even attacked Foreign Minister Aga Shahi, who is known to be cool to increased American influence here, as being an agent of the American Central Intelligence Agency.

Pakistan has vacillated since the Soviet invasion of Afghanistan in its relations with the two superpowers -- the United States and the Soviet Union.

At first, President Carter pledged to support Pakistan, and it appeared the former close ties between Washington and Islamabad would be resumed. But Zia rejected a $400 million aid offer from the United States as "peanuts" and the relationship cooled despite a January visit here by Carter's national security adviser Zbigniew Brzezinski.

Zia appeared to try to move closer to the Soviets without giving up his strong opposition to the invasion of Afghanistan. That Soviet card, which some observers here believe was designed to prod the United States into raising the aid offer, soon fell under the barrage of Moscow's anti-Zia campaign.

Nonetheless, Zia has moved carefully to avoid an open confrontation with the Soviets. In confirming that he had ordered Moscow to reduce its embassy staff here, for example, he went out of his way to say that economic relations between the two countries remained normal and that the Soviet Union was continuing to help Pakistan build a steel mill.

The goverment also ordered all other diplomatic missions to stop operating their information and publicity sections from cities other than Islamabad.

According to diplomats here, however, only the Soviets were affected by the order since their entire information effort, except two press officers, was centered in Karachi, a seaport city that is Pakistan's former capital.

The United States maintains International Communication Agency posts in Lahore, Karachi and Peshawar, but, according to U.S. sources here, they are not affected by the order.