2

hello i have made a python script that tells me my usage of my disk on my nas over ssh but everytime i run the program i get this.

size b'442G' used b' 401M' available b'419G'

i want to get rid of the b and the quotes around the 422G and the others.

here is my code

import sys, paramiko
import os
import time



username = "alex"
hostname = "192.168.1.91"
password = "alex"
command = "df /dev/sda3 -h"
port = 22
client = paramiko.SSHClient()

def updateTimeMenu():
    time_menu.delete(0, "end")
    time_menu.add_command(label=time.ctime())


def ssh():
    client.load_system_host_keys()
    client.set_missing_host_key_policy(paramiko.WarningPolicy())
    client.connect(hostname, port=port, username=username, password=password)
    menu()




def menu():
    stdin, stdout, stderr = client.exec_command(command)
    output = stdout.read()
    print("Size", output[65:69], ", Used", output[70:75], ", Available", output[77:81]) #should output hdd info (size, used, avalable)
    time.sleep(4)
    ssh()
ssh()
alex stq
  • 39
  • 1
  • 10

1 Answers1

1

That is because you retrieve a binary string, since a program can send all types of bytes through a channel, we can decode it to a string with .decode('ascii'):

print("Size",
      output[65:69].decode('ascii'),
      ", Used",
      output[70:75].decode('ascii'),
      ", Available",
      output[77:81].decode('ascii'))

Note that we here thus use the ASCII "codec", since that seems the one used, but there are of course several ways to encode a string in raw bytes.

Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555
  • 1
    thank you this has helped me so much. i have been working on this code for hours trying to decode it – alex stq Feb 15 '18 at 10:41