-1
#!/usr/bin/python
import os
import smtplib

content = str(os.system('df -h /'))
print (content)
mail=smtplib.SMTP('smtp.gmail.com:587')
mail.ehlo()
mail.starttls()
mail.login('*****@gmail.com','****')
mail.sendmail('*******@gmail.com','**********@gmail.com',content)
mail.close()

My issue is that when I got the mail it displayed only 0. It is not printing the output of df -h /. Can anyone help me out in this. I think it is storing the output in var and not in string ...

zeeMonkeez
  • 5,057
  • 3
  • 33
  • 56
sagar malve
  • 1
  • 1
  • 2
  • Return value of the system command is exit code - 0 in this case and it is stored in your content. If you want to catch output of the command find more about subprocess.check_output – poko Feb 23 '16 at 14:21
  • Change the line to: os.popen('df -h /').read() Read more: http://stackoverflow.com/questions/3503879/assign-output-of-os-system-to-a-variable-and-prevent-it-from-being-displayed-on – Javier GB Feb 23 '16 at 14:24

2 Answers2

1

please change the first part of your code as following, and try again:

#!/usr/bin/python
import os
import smtplib
import subprocess

out = subprocess.Popen(['df', '-h'], stdout=subprocess.PIPE).communicate()[0]
print(out)
Deng Haijun
  • 109
  • 1
  • 8
0

According to documentation

On Unix, the return value is the exit status of the process encoded in the format specified for wait(). Note that POSIX does not specify the meaning of the return value of the C system() function, so the return value of the Python function is system-dependent.

One command you should use is is popen

#!/usr/bin/python
import os
import smtplib

with os.popen('df -h /') as cmd:
    content = cmd.read()
print (content)
mail=smtplib.SMTP('smtp.gmail.com:587')
mail.ehlo()
mail.starttls()
mail.login('*****@gmail.com','****')
mail.sendmail('*******@gmail.com','**********@gmail.com',content)
mail.close()

Another approach could be user3001445 solution.

Community
  • 1
  • 1
Mauro Baraldi
  • 6,346
  • 2
  • 32
  • 43