通过SSH执行存储在文件中的Bash脚本

时间:2011-04-14 13:04:16

标签: bash ssh

假设我在文件foo.sh中存储了以下Bash脚本:

#!/bin/bash
echo foo

无需scp文件,如何在远程计算机上执行foo.sh中存储的脚本?

我尝试了以下(有一些变化)但没有成功:

$ ssh root@remote eval `cat foo.sh`

eval `cat foo.sh`似乎扩展到eval #!/bin/bash echo foo

5 个答案:

答案 0 :(得分:34)

ssh root@MachineB 'bash -s' < local_script.sh

我从那个帖子中得到了它:How to use SSH to run a shell script on a remote machine?

答案 1 :(得分:4)

在接受的答案中,我看到:

  

我希望将它作为一个班轮。你能做一个小代码示例吗?

那应该是它:

ssh root@MachineB 'bash -s -- uno' < local_script.sh

或更好,使用此处文档

ssh root@MachineB 'bash -s -- uno' <<\EOF
> date
> echo $1
> EOF
jue sep 18 13:01:25 CEST 2014
uno

答案 2 :(得分:1)

cat foo.sh | ssh -T root@remote将会成功。 -T选项会抑制您可能会收到的警告,因为您正在从文件中输入输入。

答案 3 :(得分:1)

cat foo.sh | ssh HOSTNAME 

现在已经过测试:小心处理! :)
(删除破折号(见评论)和几乎所有东西:))

答案 4 :(得分:0)

您可以使用runoverssh

{'England League 2 - Full Time Result': {'Sat 07 Sep': [{'date': '15:00',
                                                         'last_child': False,
                                                         'teams': 'Bradford v '
                                                                  'Northampton'},
                                                        {'date': '15:00',
                                                         'last_child': False,
                                                         'teams': 'Cambridge '
                                                                  'Utd v '
                                                                  'Forest '
                                                                  'Green'},
                                                        {'date': '15:00',
                                                         'last_child': False,
                                                         'teams': 'Carlisle v '
                                                                  'Exeter'},
                                                        {'date': '15:00',
                                                         'last_child': False,
                                                         'teams': 'Cheltenham '
                                                                  'v '
                                                                  'Stevenage'},
                                                        {'date': '15:00',
                                                         'last_child': False,
                                                         'teams': 'Colchester '
                                                                  'v Walsall'},
                                                        {'date': '15:00',
                                                         'last_child': False,
                                                         'teams': 'Grimsby v '
                                                                  'Crewe'},
                                                        {'date': '15:00',
                                                         'last_child': False,
                                                         'teams': 'Leyton '
                                                                  'Orient v '
                                                                  'Swindon'},
                                                        {'date': '15:00',
                                                         'last_child': False,
                                                         'teams': 'Macclesfield '
                                                                  'v Crawley '
                                                                  'Town'},
                                                        {'date': '15:00',
                                                         'last_child': False,
                                                         'teams': 'Mansfield v '
                                                                  'Scunthorpe'},
                                                        {'date': '15:00',
                                                         'last_child': False,
                                                         'teams': 'Morecambe v '
                                                                  'Salford '
                                                                  'City'},
                                                        {'date': '15:00',
                                                         'last_child': False,
                                                         'teams': 'Newport '
                                                                  'County v '
                                                                  'Port Vale'},
                                                        {'date': '15:00',
                                                         'last_child': True,
                                                         'teams': 'Plymouth v '
                                                                  'Oldham'}]},...
sudo apt install runoverssh

runoverssh -s localscript.sh user host 远程运行​​本地脚本