通过ssh连接到服务器,检查是否安装了chef,并记录未安装chef的服务器

2024-04-28 22:17:45 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要检查很多服务器,如果他们已经安装了厨师(不要问)我试图自动化,而不是手动做它。我以前从未使用过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)

Tags: importtxtrawlineusernamepasswordopenprompt
1条回答
网友
1楼 · 发布于 2024-04-28 22:17:45

好吧,所以你可能不想把厨师作为其中的一部分。也永远不要用sudo su -来纠正过去15年来一直是sudo -i的写作方式,但你甚至不需要这样。只要运行chef-client version,如果失败,就假设没有安装Chef。你知道吗

相关问题 更多 >