使用命令行功能蟒蛇复制日志(copying logs in python using the command line function)

.NET Framework IT屋
问 题

I had a doubt with my code which i think I can verify here . My requirement is to copy the apache log and error log from two different servers . Iv written down a python program, using a for loop.

My code:

def copylogs(Appache,Errorlog, folder_prefix) :

    root_path = '/home/tza/Desktop/LOGS/'
    folders = ['Appache','Errorlog']
    for folder in folders:


      folder_name = folder_prefix + "_" + folder + str(int(time.time()))
      mkdircmd = "mkdir -p " + root_path + "/" + folder_name
      os.system(mkdircmd)

      filePath = root_path + folder_name
      serverPath = "/var/log/apache/*"

      cmd = "scp " + "symentic@60.62.1.164:" + serverPath + " " + filePath
      cmd = cmd.replace("60.62.1.164" ,myip1)
      cmd = os.system(cmd)
      print "Logs are at:",root_path+folder_name
      time.sleep(10)

      filePath = root_path +  folder
      serverPath = "/var/log/errorlog/*"

      cmd = "scp " + "symentic@10.95.21.129:" + serverPath + " " + filePath
      cmd = cmd.replace("10.95.21.129" ,myip2)
      cmd = os.system(cmd)
      print "Logs are at:",root_path+folder_name

now Im calling the function at the end of my program :

folder_prefix = "Fail Case-1"
copylogs(Appache,Errorlog, folder_prefix)

I have an issue here . Programm executes successfully but the logs get overwritten .what i mean is first Appache folder gets created ,logs are copied and then once again it gets overwritten .

What i require is : create a folder Appachelogs[with the timestamp as defined ] ,copy the logs from machine one , next copy error logs from machine2 , and continue the program

How can this be achieved?

解决方案

scp by default overwrites if a same file name exists in the target computer.

I would suggest using a combination of a error file name + the timestamp for naming the error logs. It's always a good convention for logs to have a timestamp in the name and they also prevent the overwriting problem you are experiencing.

本文地址:IT屋 » copying logs in python using the command line function

问 题

我有一个疑问,我的code,我认为我可以验证在这里。我的要求是到Apache日志和错误日志从两个不同的服务器上复制。 Ⅳ书面的python程序,用一个for循环。

我的code:

高清copylogs(Appache,错误日志,folder_ preFIX):    root_path ='/家庭/ TZA /桌面/日志/“
    夹= ['Appache','错误日志']
    在文件夹中的文件夹:
      FOLDER_NAME = folder_ preFIX +“_”+文件夹+ STR(INT(选定了time.time()))
      mkdircmd =“的mkdir -p”+ root_path +“/”+ FOLDER_NAME
      使用os.system(mkdircmd)      文件路径= root_path + FOLDER_NAME
      SERVERPATH =“/无功/日志/阿帕奇/ *”      CMD =“SCP”+“symentic@60.62.1.164:”+ SERVERPATH +“”+文件路径
      CMD = cmd.replace(“60.62.1.164”,myip1)
      CMD =使用os.system(CMD)
      打印“日志是在:”root_path + FOLDER_NAME
      time.sleep(10)      文件路径= root_path +文件夹
      SERVERPATH =“/无功/日志/错误日志/ *”      CMD =“SCP”+“symentic@10.95.21.129:”+ SERVERPATH +“”+文件路径
      CMD = cmd.replace(“10.95.21.129”,myip2)
      CMD =使用os.system(CMD)
      打印“日志是在:”root_path + FOLDER_NAME

现在林调用该函数在我的程序的结尾:

  folder_ preFIX =“失败案例1”
copylogs(Appache,错误日志,folder_ preFIX)

我在这里有一个问题。 PROGRAMM成功执行,但该日志被覆盖。什么我的意思是第一Appache文件被创建,日志被复制,然后再一次它被覆盖。

我的要求是:创建一个文件夹Appachelogs [与所定义的时间戳],日志从一台机器之一,下副本错误日志从机2复制,继续运行程序

如何才能实现这一目标?


解决方案

如果一个相同的文件名在目标计算机中存在SCP默认覆盖。

我会建议使用一个错误的文件名+命名错误日志的时间戳的组合。它总是一个很好的约定日志的文件名中的时间戳,他们也prevent您所遇到的覆盖问题。

本文地址:IT屋 » 使用命令行功能蟒蛇复制日志