简体   繁体   English

通过SSH在两台计算机之间传输文件

[英]File transmission between two computers via ssh

I have two computers(lets name them A and B) with linux (two raspberry pi 3 ). 我有两台装有linux(两台rapiberry pi 3)的计算机(让它们分别命名为A和B)。 I need to get file from B and see it on A. So I connected them with ssh. 我需要从B获取文件并在A上查看它。因此我将它们与ssh连接。

B code file contains: B代码文件包含:

import rospy
from clever import srv
rospy.init_node('telemetry')  
file = open("telemetry", "w+")
while True:
  get_telemetry = rospy.ServiceProxy('get_telemetry', srv.GetTelemetry) 
  position = get_telemetry(frame_id='aruco_map')
  f.write(get_telemetry)

call here a rospy Service Proxy,then write 叫这里一个红润的服务代理,然后写
necessary info into the file "telemetry",which I created on both raspberries.This part works fine. 必要的信息放入我在两个树莓派上创建的“遥测”文件中。这部分工作正常。

Code file on A contains A上的代码文件包含

import subprocess
while True:
  subprocess.call(["ssh","pi@B", "'cat telemetry'", ">", "telemetry"])

B is raspbbery ip and pi is login B是抢劫IP,而pi是登录名

But when i run code file on A it says "bash: cat telemetry: command not found" What am I doing wrong? 但是,当我在A上运行代码文件时,它说:“打击:猫遥测:未找到命令”我在做什么错? Thanks for help. 感谢帮助。

When using subprocess.call , you don't need to add extra quotes because spaces are not delimiters. 使用subprocess.call ,您不需要添加多余的引号,因为空格不是分隔符。 Also, since the shell is not involved locally, output redirection with > doesn't work. 另外,由于外壳未在本地涉及,所以>输出重定向不起作用。 The > will be sent to remote host and will run remotely. >将发送到远程主机,并将远程运行。 To redirect the actual local command to a file, you must use subprocess PIPE output capturing by passing a parameter named stdout : 要将实际的本地命令重定向到文件,必须通过传递名为stdout的参数来使用子PIPE输出捕获:

with open('telemetry', 'wb') as f:
    subprocess.call(["ssh", "pi@B", "cat telemetry"], stdout=f)

That should fix your issue and answer your question "What am I doing wrong?" 那应该可以解决您的问题,并回答您的问题“我在做什么错了?” - however, as others have pointed out in the comments, you should consider using scp , sshfs or even a paramiko session instead of running a remote cat to copy files. -但是,正如其他人在评论中指出的那样,您应该考虑使用scpsshfs甚至是paramiko会话,而不是运行远程cat复制文件。

May I suggest using Python Fabric ( HERE ). 我可以建议使用Python Fabric( HERE )。 It's very easy to configure. 这很容易配置。 You just make a connection a call a function: 您只需将连接称为调用函数:

from fabric import Connection
C = fabric.Connection('IP', user='pi', connect_kwargs={"password": "raspberry"})
result = C.put('myfile', remote='/home/pi')

I use it for the same thing. 我将它用于同一件事。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM