简体   繁体   English

需要编写一个bash脚本以ssh进入许多UNIX计算机,运行python脚本并将结果推送到GitHub

[英]Need to write a bash script to ssh into many UNIX machines, run a python script and push results to GitHub

I'm trying to write a bash script that does the following for ~200 UNIX machines: 我正在尝试编写一个bash脚本,该脚本对200台UNIX计算机执行以下操作:

In a for loop, ssh in, navigate to the right directory. 在for循环中,ssh输入,导航到正确的目录。

Then, executes a python file in the given directory with a command line argument. 然后,使用命令行参数在给定目录中执行python文件。 This command line argument is specified based off of location in the for loop. 此命令行参数是根据for循环中的位置指定的。 So, the first machine runs the script with a command of '0', then the second machine runs the script with a command of '1', so on and so forth. 因此,第一台计算机使用命令“ 0”运行脚本,然后第二台计算机使用命令“ 1”运行脚本,依此类推。

How would I go about doing this? 我将如何去做呢? I'm looking into shell scripting but I've never written one before. 我正在研究shell脚本,但是我以前从未写过。

Also, I have to use a password to sign into the machines, so I need to account for that in a script. 另外,我必须使用密码登录机器,因此我需要在脚本中进行说明。

Sparrowdo provides a neat API to do this, I wrote a small article on this topic - https://sparrowdo.wordpress.com/2017/02/01/sshscp-commands-with-sparrowdo/ . Sparrowdo提供一个整洁的API来做到这一点,我写了关于这一主题的小文章- https://sparrowdo.wordpress.com/2017/02/01/sshscp-commands-with-sparrowdo/ The advantage is you write scenario in high level language Perl6 and free to implement any complex logic. 好处是您可以使用高级语言Perl6编写脚本,并且可以自由实现任何复杂的逻辑。

PS.disclosure - I am the tool author. PS.disclosure-我是工具作者。

First of all I'll assume you're as lazy as me and want to go to the easiest solution. 首先,我假设您像我一样懒惰,并且想要采用最简单的解决方案。 In my opinion it is, in this case, Ansible . 我认为在这种情况下是Ansible It is an automation tool which can ssh for you on all the machines you list on a hosts file. 它是一个自动化工具,可以在hosts文件中列出的所有计算机上使用ssh命令。

hosts file : 主机文件:

[myServers]
myServer1
myServer2
myServer3
...

You can write playbooks, which are series of actions to execute on each machine. 您可以编写剧本,这是在每台计算机上执行的一系列操作。 Here, for example your playbook.yml would look like 在这里,例如您的playbook.yml看起来像

- name: Executing my python program
  shell: "python mypythoncommand"
  args:
    chdir: theDirectoryYouWant

At this point, we still need to increment some part in the command for each machine. 此时,我们仍然需要为每台计算机增加命令中的某些部分。 In order to do this we can use the Jinja2 language, which is basically code interpreted before applying commands. 为此,我们可以使用Jinja2语言,该语言基本上是在应用命令之前对代码进行解释的语言。

I think something like that would do the job : 我认为类似的事情会做的工作:

hosts: myServers
  vars:
    - myVariable: 0
  tasks:
  {% for thehost in play_hosts %}
  {% if inventory_hostname==thehost %}
    - name: Executing my python program
      shell: "python mypythoncommand {{myVariable + loop.index}}"
      args:
        chdir: theDirectoryYouWantToBeIn
  {% endif %}
  {% endfor %}

This playbook ssh on all the servers, iterates on your server list and when it finds the server Ansible is ssh-ing on, it executes your command with the variable incremented for each server it passed already. 该剧本ssh在所有服务器上运行,并在服务器列表上进行迭代,当它发现服务器Ansible处于ssh-ing状态时,它将对已传递的每台服务器执行变量递增的命令。

You will want to get the output of your program in a file. 您将需要在文件中获取程序的输出。 Ansible stores the log where you want, you juste have to : Ansible将日志存储在您想要的位置,您只需要:

export ANSIBLE_LOG_PATH="/path/to/my/log" && ansible-playbook myAwesomePlaybook.yml -k -u username && git commit -a -m "execution log $(date)" -k asks for password and -u username choose the user you want to ssh as. export ANSIBLE_LOG_PATH="/path/to/my/log" && ansible-playbook myAwesomePlaybook.yml -k -u username && git commit -a -m "execution log $(date)" -k要求输入密码, -u username选择您要以ssh身份登录的用户。

Hopes it helps, if you really wanna go bash you may want to read this and take a look at sshpass in order to connect to each server. 希望有帮助,如果你真的想要去,你可能需要阅读的bash 并看看sshpass才能连接到每台服务器。

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

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