ssh在远程计算机上运行shell脚本,然后将输出复制到本地计算机

时间:2012-07-27 03:11:44

标签: shell ssh plink

我正在使用plink在远程MachineB上执行shell脚本。 shell脚本位于MachineA(Windows Box)

C:\PLINK>plink uname@MachineB -m test.sh 
Using keyboard-interactive authentication. 
Password: 
Using keyboard-interactive authentication. 
Your Kerberos password will expire in 73 days. 

该shell脚本在MachineB上的text file(aa.txt)生成输出,并存储在/export/home/uname/aa.txt

所以我的问题是 - 一旦脚本完成所有任务,我是否可以将aa.txt fileMachineB复制到MachineA使用ssh。或者我们需要将所有这些东西放在Windows批处理文件中?

所以问题陈述是这样的 -

  1. MachineB。{/ li>执行MachineA上的shell脚本
  2. 然后等待shell脚本完成其任务,在我的情况下,它会将输出写入文本文件。
  3. 在shell脚本完成所有任务后意味着它已将所有内容写入txt文件,然后将该txt文件从MachineA
  4. 复制到MachineB

    对于如何实现上述方案,我们将不胜感激任何建议?

    更新: -

    那么假设这是test.sh shell脚本文件中的内容,并且在脚本末尾添加pscp后,它应该是这样的吗?

    #!/bin/bash 
    export HIVE_OPTS="$HIVE_OPTS -hiveconf mapred.job.queue.name=hdmi-technology" 
    hive -S -e 'SELECT count(*) from testingtable2' > aa.txt 
    pscp uname@MachineB:/export/home/uname/aa.txt c:\documents\foo.txt
    

    所以我在上面的脚本中执行一个hive查询,其输出存储在aa.txt文件中,一旦查询完成并且输出存储在aa.txt文件中,它就会出现到fourth line of pscp,它会将aa.txt文件传输到文档文件夹中的本地Windows机器。我对吗?这将是整个过程吗?

    如果上述过程是我所理解的,那么我可以简单地转到windows cmd提示符,并在下面执行,它将执行完全相同的过程。正确?

    C:\PLINK>plink uname@MachineB -m test.sh 
    Using keyboard-interactive authentication. 
    Password: 
    Using keyboard-interactive authentication. 
    Your Kerberos password will expire in 73 days. 
    

    再次更新: -

    所以我需要创建一个bat文件,并假设这是下面的test.bat文件,所以内容应该是test.bat文件中的内容 -

    plink uname@MachineB -m test.sh 
    pscp  uname@MachineB:/export/home/uname/aa.txt c:\documents\foo.txt
    

2 个答案:

答案 0 :(得分:1)

执行后,您可以使用scp下载文件。如果您在Windows机器上设置winsshd并在test.sh文件中设置append复制命令:

scp /export/home/uname/aa.txt user@windowsmachine.com:/homedir

文件将在完成后传输。

同样,您可以在Windows机器上使用像pscp这样的Windows scp客户端,您可以从linux机器中提取文件:

pscp uname@MachineB:/export/home/uname/aa.txt c:\documents\foo.txt

所以在Windows机器上,有一个批处理脚本:

plink uname@MachineB -m test.sh 
pscp  uname@MachineB:/export/home/uname/aa.txt c:\documents\foo.txt

答案 1 :(得分:0)

只是添加已经说过的内容,我运行的内容非常相似,并使用以下格式(来自批处理脚本):

plink -ssh user @ -pw password -m yourScript.sh

pscp -sftp -pw密码user @ IP-Address:/ path / to / remote / file C:\ path \ to \ local \ save \ directory

第二个命令在一行上,但它可能不会在这里呈现。当然,请使用适当的登录值替换IP地址,用户和密码: - )

希望有所帮助!

编辑 - 抱歉,刚刚意识到已经给出了 EXACT 的答案。奖励!