pythonpython-2.7sshparamiko

Pass Python variable inside command with Paramiko


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)

Solution

  • 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?