需求:某公司因为网站服务经常出现异常,需要你开发一个脚本对服务器上的服务进行监控;检测目标服务器上是否存在nginx软件(提供web服务的软件),如果不存在则安装(服务器可能的操作系统Ubuntu24/RedHat9);如果nginx软件存在则检查nginx服务是否启动,如果没有启动则启动该服务(为了确认是否启动成功,需要在自己浏览器中访问服务器ip地址对应的URL: h t t p : / / 192.168.0.200 http://192.168.0.200 h ttp : //192.168.0.200 是否能看到nginx启动页面)
提示1:检测服务器操作系统,推荐命令:uname -a
,从它里面提取关键字检测 提示2:unbutn安装命令apt install 软件名称
,redhat安装命令yum install 软件名称
提示3:nginx是一个软件,启动之后就会是一个名称为nginx的服务,提供网站服务-启动之后能在80端口访问到一个默认页面http://192.168.0.200:80
等价于http://192.168.0.200
,因为http://
协议默认端口-80端口;需要注意https://
默认端口-443端口
import paramiko
from My_log import Loggerlogger = Logger( )
host = "192.168.5.128"
username = "root"
password = "041021"
logger. info( f"准备连接远程主机 { host} " )
logger. info( f"当前登录账号 { username} " )
logger. info( "准备 SSH 连接" )
ssh = paramiko. SSHClient( )
ssh. set_missing_host_key_policy( paramiko. AutoAddPolicy( ) )
ssh. connect( host, username= username, password= password) try : stdin, stdout, stderr = ssh. exec_command( 'which nginx' ) nginx_output = stdout. read( ) . decode( ) . strip( ) if nginx_output: logger. info( "已安装 Nginx。" ) stdin, stdout, stderr = ssh. exec_command( "systemctl is-active nginx" ) result = stdout. read( ) . decode( ) . strip( ) if result == "active" : logger. info( "Nginx 服务已启用。" ) else : logger. info( "Nginx 服务未启用,正在启动..." ) ssh. exec_command( "systemctl start nginx" ) stdin, stdout, stderr = ssh. exec_command( "curl http://192.168.5.128" ) access_result = stdout. read( ) . decode( ) . strip( ) if "Welcome to nginx!" in access_result: logger. info( "Nginx 服务启动成功。" ) else : logger. info( "Nginx 服务启动失败,请检查相关配置。" ) else : logger. info( "未安装 Nginx,开始下载安装..." ) ssh. exec_command( "yum -y install nginx" ) logger. info( "Nginx 安装完成,继续检查服务状态..." ) stdin, stdout, stderr = ssh. exec_command( "systemctl is-active nginx" ) result = stdout. read( ) . decode( ) . strip( ) if result == "active" : logger. info( "Nginx 服务已启用。" ) else : logger. info( "Nginx 服务未启用,正在启动..." ) ssh. exec_command( "systemctl start nginx" ) stdin, stdout, stderr = ssh. exec_command( "curl http://192.168.5.128" ) access_result = stdout. read( ) . decode( ) . strip( ) if "Welcome to nginx!" in access_result: logger. info( "Nginx 服务启动成功。" ) else : logger. info( "Nginx 服务启动失败,请检查相关配置。" ) except Exception as e: logger. error( f"Error occurred: { e} " )
ssh. close( )
My_log
import datetime
import osclass LogLevel : DEBUG = "DEBUG" INFO= "INFO" WARNING = "WARNING" ERROR = "ERROR" class Logger : def __init__ ( self, log_folder= 'logs' ) : self. log_folder = log_folderif not os. path. exists( log_folder) : os. makedirs( log_folder) self. log_file = os. path. join( log_folder, f'log_ { datetime. datetime. now( ) . strftime( "%Y%m%d%H%M%S" ) } .log' ) def debug ( self, message) : self. log( LogLevel. DEBUG, message) def info ( self, message) : self. log( LogLevel. INFO, message) def warning ( self, message) : self. log( LogLevel. WARNING, message) def error ( self, message) : self. log( LogLevel. ERROR, message) def log ( self, level, message) : timestamp = datetime. datetime. now( ) . strftime( '%Y-%m-%d %H:%M:%S' ) log_message = f"[ { timestamp} ] [ { level} ] { message} \n" with open ( self. log_file, 'a' ) as f: f. write( log_message) if level in [ LogLevel. INFO, LogLevel. WARNING, LogLevel. ERROR] : print ( f"[ { timestamp} ] { message} " ) elif level == LogLevel. DEBUG: print ( f"[ { timestamp} ] [DEBUG] { message} " ) logger = Logger( )