0

I have a bash script to detect HDD connected in USB. I would like to convert it in python script to use it into SSH connection.

#!/bin/bash

for disk in /dev/sd?
do
        detect=$(udevadm info $disk | grep USB)
        if [[ "$detect" == *USB ]]; then
                echo "$disk is a USBDISK.."
        fi
done

I tried that, but it doesn't work. I have a problem with the for loop and with the condition in if statement :

import paramiko

for disk in "/dev/sd*" :
        CMD = 'udevadm info %s | grep USB' % disk
        stdin, stdout, stderr = client.exec_command(CMD, get_pty = True)
        detect = stdout.read()
        if detect == '*USB' :
            print "disk is a USBDISK.."

Thanks.

MisterGod
  • 25
  • 1
  • 4

1 Answers1

0

As Selcuk mentioned, you are looping through a string instead than through the contents of the folder. When you do:

for disk in "/dev/sd*" :

you are looping through the string char by char like in the list ['/','d','e','v','/','s','d','*']

Python works different to bash when referring to strings and paths. Take a look to this answer from ghostdog74 to see how loop through a directory content.

This could fix the problems with the if condition. As a recommendation, you might consider if you want to check in the condition for: Exact string:

if detect == '*USB' :

or for string inclusion:

if '*USB' in detect:
spaniard
  • 541
  • 2
  • 12