With this command, I want to save an output of the smartctl
command of the sda drive.
Now, since I use RAID, I have multiple drives, not just sda.
The following command works fine:
stdin, stdout, stderr = ssh.exec_command('/bin/su root -c "smartctl -a /dev/sda > /tmp/smartctl_output1"', get_pty=True) # ORIGINAL
I want to achieve that I pass local Python variable "DISK" containing sda, sdb, sdc and so on instead only static value.
The following line produces error:
stdin, stdout, stderr = ssh.exec_command('/bin/su root -c "smartctl -a /dev/" + DISK + " > /tmp/" + DISK', get_pty=True)
edit: tried this as well:
stdin, stdout, stderr = ssh.exec_command('/bin/su root -c "smartctl -a /dev/' + DISK + ' > /tmp/' + DISK, get_pty=True)
stdin.write(ROOTPASS)
stdin.write('\n')
DEBUG1=stdout.read()
print "DEBUG COMMAND= " + DEBUG1
Produces the following error and file in /tmp/ + DISK not created:
DEBUG COMMAND= bash: -c: line 0: unexpected EOF while looking for matching `"'
bash: -c: line 1: syntax error: unexpected end of file
Your question has nothing to do with Paramiko.
It's just a trivial concatenation of strings in Python:
ssh.exec_command(
'/bin/su root -c "smartctl -a /dev/' + DISK + ' > /tmp/' + DISK + '"',
get_pty=True)
For even better options, see How to insert string into a string as a variable?
来源:https://stackoverflow.com/questions/49777888/pass-python-variable-inside-command-with-paramiko