Guru Guru - 2 months ago 8
Bash Question

unable to use cmd command in python

I am using this command to find disk usage in my shell script

df -h | awk '$NF=="/"{printf "Disk Usage: %d/%dGB (%s)\n", $3,$2,$5}'


I want to use this command in my python script

ssh_stdin, ssh_stdout, ssh_stderr = ssh.exec_command("df -h | awk '$NF=="/"{printf "Disk Usage: %d/%dGB (%s)\n", $3,$2,$5}'")


but unable to handle string literals.

I tried replacing \ with \\ as suggested here

Can someone throw light on thiS?

This is the error i am getting

ssh_stdin, ssh_stdout, ssh_stderr = ssh.exec_command("df -h | awk '$NF=="/"{printf "Disk Usage: %d/%dGB (%s)\n", $3,$2,$5}'")
^
SyntaxError: invalid syntax

kfb kfb
Answer

You are not escaping your double quotes and they are terminating the string early:

ssh.exec_command("df -h | awk '$NF==\"/\"{printf \"Disk Usage: %d/%dGB (%s)\\n\", $3,$2,$5}'")