Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scripting Python for Linux commands

Tags:

python

linux

I have a question. I have been really trying to learn Python. For a project, I want to make an ncurses GUI for my backup server. My backup server runs rdiff-backup, and I want to have the ncurses take in variable names and plug them into my script. I have been trying to do a lot of reading so I don't ask dumb questions.

Here is my function for running the script:

def runScript():
# Cannot concatenate 'str' and 'list' objects
#script = rdiff + rdiffArgs

script = rdiff + ' ' + rdiffVerbosity + ' ' + rdiffStatistics \
         + ' ' + clientName + '@' + clientHost + '::' + clientDir \
         + ' ' + serverDir

os.system(script)

What I originally thought would be neat was to add all the variables into a list, so I could just run say

script = rdiff + rdiffArgs

Is there a better way to do this without all the space concatenation?

Thanks for your assistance

EDIT: Let me post the whole script so far. I wasn't very clear and I really appreciate your help and patience

  #!/usr/bin/env python



import os
import smtplib


# Global variables
rdiff = '/usr/bin/rdiff-backup'
rdiffVerbosity = '-v5'
rdiffStatistics = '--print-statistics'
emailSmtp = 'smtp.gmail.com'
smtpPort = '465'
emailUsername = 'reports'
emailPassword = '3kc9dl'
emailTo = '[email protected]'
emailFrom = '[email protected]'
serverName = 'root'
serverHost = 'SV-Datasafe'
serverDir = '/srv/backup/SV-Samba01'
clientName = 'root'
clientHost = 'SV-Samba01'
clientDir = '/srv'
rdiffArgs = rdiffArgs = [rdiffVerbosity, rdiffStatistics, \
                         clientName + '@' + clientHost + '::' \
                         +clientDir + ' ' + serverDir]
time = ''
dateStamp = datetime.now()



def sendEmail():
    subject = dateStamp + clientName
    body = clientDir + ' on ' + clientHost + ' backed up to ' + serverName + \
           ' in the directory ' + serverDir + ' on ' + dateStamp
    message = """\
    From: %s
    To: %s
    Subject: %s
    %s
    """ % (emailFrom, emailTo, subject, body)


    deliverEmail = smtplib.SMTP(emailSmtp, port=smtpPort)
    deliverEmail.login(emailUsername, emailPassword)

def runScript():
    # Cannot concatenate 'str' and 'list' objects
    #script = rdiff + rdiffArgs

    script = rdiff + ' ' + rdiffVerbosity + ' ' + rdiffStatistics \
             + ' ' + clientName + '@' + clientHost + '::' + clientDir \
             + ' ' + serverDir

    os.system(script)

    # TODO:: Logging
like image 564
Dan Avatar asked Jul 04 '26 06:07

Dan


2 Answers

you can use format specifiers

def runScript():
    script = "%s %s %s@%s %s::%s %s" %(rdiff,rdiffVerbosity,rdiffStatistics,clientName,clientHost,clientDir,serverDir)    
    os.system(script)

or say your rdiffArgs is already in a list

rdiffArgs = [rdiffVerbosity,rdiffStatistics,clientName,clientHost,clientDir,serverDir]

you can join them with a space

rdiffArgs = ' '.join(rdiffArgs)

lastly, just so you might want to know, you can import rdiff in your script , since rdiff-backup is written in Python

from rdiff_backup.Main import Main as backup
task=['/etc', '/tmp/backup']
backup(task)

the above backs up /etc/ to /tmp/backup. That way, you don't have to make system call to rdiff-backup. Of course, this is up to you. making system call is sometimes easier

like image 94
ghostdog74 Avatar answered Jul 08 '26 21:07

ghostdog74


try to use subprocess module and pass arguments as list e.g.

client = clientName + '@' + clientHost + '::' + clientDir
cmd = [rdiff, rdiffVerbosity, rdiffStatistics, client , serverDir]
p = Popen(cmd ", shell=True)
print os.waitpid(p.pid, 0)[1]

or if have args already as list use something like this

cmd = [rdiff] + args
like image 29
Anurag Uniyal Avatar answered Jul 08 '26 19:07

Anurag Uniyal



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!