找到自己Mac的ssh公钥,如果没有ssh密钥的话,需要生成一下
cat ~/.ssh/id_rsa.pub# 在~/.ssh路径下面执行命令生成ssh密钥
ssh-keygen -t rsa
然后找到要免密登陆的远程服务器
~/.ssh/authorized_keys
把自己Mac上面的公钥拷贝到这里面即可
Python脚本
def test_scp(self):# 远程服务器的地址和目标路径remote_host = "root@10.10.10.222"remote_path = "/home/ps/Code/ROS/robot_audio/test/WhisperAudio/person"# 循环遍历i的范围(6到19)for i in range(6, 20):# 生成文件路径local_file = f"/opt/audio/test/WhisperAudio/person/b1max_{i}.wav"# 构建scp命令scp_command = f"scp {local_file} {remote_host}:{remote_path}"try:# 执行scp命令subprocess.run(scp_command, shell=True, check=True)print(f"文件 b1max_{i} 传输成功")except subprocess.CalledProcessError as e:print(f"文件 b1max_{i} 传输失败:{e}")