【问题标题】:Connect to server via ssh, check if chef is installed and record servers that it is not installed on通过 ssh 连接到服务器,检查是否安装了 chef 并记录未安装的服务器
【发布时间】:2017-11-23 17:00:08
【问题描述】:

如果他们安装了厨师,我需要检查很多服务器(不要问)我试图自动化它而不是手动执行它。我以前从未使用过python,直到明天我才能真正测试它,所以如果我在正确的轨道上,我正在寻找一些反馈。我想我将所有IP地址转储到一个文本文件中,循环遍历它并保存需要安装 chef 的那些。

import pxssh
import getpass
CheckIT = str("command not found")

for line in open('ServerList.txt','r').readlines():

try:                                                            
s = pxssh.pxssh()
hostname = raw_input('line ')
username = raw_input('username ')
password = getpass.getpass('password: ')
s.login (hostname, username, password)
s.sendline ('sudo su -')   # run a command
s.prompt()             # match the prompt
s.sendline ('chef-client')
s.prompt()
if CheckIT == readline(self,size=-1)
with open("ServersToUpdate.txt", "a") as myfile:
myfile.write(hostname)


except pxssh.ExceptionPxssh, e:
print "pxssh failed on login."
print str(e)

【问题讨论】:

  • 如果没有进一步的步骤,单独检查“是否安装了 Chef”并没有真正意义。您想对已安装或未安装 Chef 的节点做什么?
  • 如果没有安装那么我需要安装它,如果安装了没有进一步的操作。
  • 为什么不直接引导每个节点?

标签: python ssh chef-infra pxssh


【解决方案1】:

好的,所以您可能不想在其中实际运行 Chef。也永远不要使用sudo su -,在过去 15 年中正确的写法是sudo -i,但你甚至不需要那个。只需运行 chef-client --version,如果失败,则假定未安装 Chef。

【讨论】:

    猜你喜欢
    • 2021-03-19
    • 2013-09-26
    • 1970-01-01
    • 2012-04-09
    • 1970-01-01
    • 1970-01-01
    • 2015-12-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多