[英]Connect to server via ssh, check if chef is installed and record servers that it is not installed on
I need to check a lot of servers if they have chef installed (Don't ask) I was trying to automate it instead of doing it manually. 我需要检查很多服务器是否安装了Chef(不要问),我试图使其自动化而不是手动进行。 I have never used python before and I can't really test this until I go in tomorrow so was looking for some feed back if I am on the right track.I figure I dump all the IP address's into a text file, loop through it and save the ones that need to have chef installed.
我以前从未使用过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)
Okay, so you probably won't want to actually run Chef as part of this. 好的,所以您可能不想在其中实际运行Chef。 Also never ever use
sudo su -
, to correct way to write that for the past 15 years has been sudo -i
, but you don't need even that. 同样,永远不要使用
sudo su -
来纠正过去15年以来一直使用sudo -i
书写方式,但是您甚至都不需要。 Just run chef-client --version
and if that fails, assume Chef isn't installed. 只需运行
chef-client --version
,如果失败,则假定未安装Chef。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.