An FBI agent who secretly entered the homes of friends and relatives of the Weather Underground testified at length in federal court yesterday about the radical group's foreign connections but said he did not give that information to a grand jury investigating the searches because he thought his oath as an agent prohibited such disclosures without court approval.

Defense lawyers for two former top FBI aides who are on trial in U.S. District Court for authorizing those entries in 1972 and 1973 are trying to show that the searches were legal because they were carried out for national security purposes.

The FBI agent, James Vermeersch, who is also a lawyer, testified that he thought the searches were legal although he believed that the information collected could not be used to prosecute the Weathermen in court because it was obtained through a "trepass" and was therefore "tainted."

"Mr. Vermeersch, you're just flat wrong about that," special prosecutor John W. Nields Jr. said when the agent testified that he thought evidence obtained through a legal wiretap or microphone surveillance would also be prohibited in court.

In an extraordinary series of questions to his own witness, Nields then quizzed Vermeersch about Fourth Amendment prohibitions against unreasonable searches, federal rules for prosecution or criminal cases, and Supreme Court decisions on the legality of searches and surveillance.

The government is trying to show that the searches, approved by the defendants, W. Mark Felt and Edward S. Miller, were illegal because they targeted innocent persons. The government also argues that the searches could not be justified for national security reasons as Felt and Miller contend.

Vermeersch finally agreed that he knew of no court case which said that evidence collected legally was tainted and thus could be kept out of court. He said he thought that he was collecting intelligence information for the bureau's use only and not evidence to be brought to court.

"My job was investigation, not prosecution," Vermeersch testified.

Vermeersch told Nields he "never thought" about the possibility that the searches could create problems for Weatherman prosecutions until Nields raised the question in court yesterday. Nevertheless, the agent testified, "If I stopped the bombings it would have been worth it. . . .'

Vermeersch was a member of Squad 47, agents from the New York bureau who he testified carried out surreptitious entries looking for clues to the whereabouts of Weathermen fugitives and leads that might help stop the bombings for which the Weathermen were claiming credit.

During cross-examination by defense lawyer Mark Cummings, Vermeersch testified that he thought the Weather Underground had more extensive contracts abroad than other known subversive organizations. Vermeersch told Cummings he thought his employment agreement with the bureau barred him from telling a grand jury two years ago about the Weathermen's foreign connections.

Later, Vermeersch told prosecutor Nields that Justice Department lawyers at the grand jury sessions did not ask him about the Weather Underground's foreign connections.

Vermeersch consistently defended the searches, and at one point he pounded his finger on the witness stand as he described the purpose of the Weather Underground to the jury.

"That organization . . . considered themselves . . . as a secret group of Marxist-Leninist communists who were going to undertake acts of terrorism . . . arson, bombings, assassinations, with a goal of the violent overthrow of this country's democratic system of government with the end objective of the establishment of world communism. That's what they were," Vermeersch testified.

"These people were as organized as we were. Perhaps more so," Vermeersch declared.