2

I've written a script to help others run a simple day to day commands on our storage system here at work. The script works fine with commands that return a short and simple output, for example, ls, however, when the script wants to run a command which has a large output, the output isn't returned. It's almost as if it times out but there's no feedback at all, e.g. I thought there might be part of the command output. I've done some research around this and discovered other people with the same problem. The answers they got was to use:

stdin, stdout, stderr = client.exec_command(command)

Which I was already using in my code. I'm wondering if it's something to do with the buffer size, which annoyingly I don't know how to implement that in my code. I've tried adding a time delay using:

time.sleep(10)

But no joy from that. I have also tried using:

print stdout.channel.recv_exit_status()

However, I got a return of 127 so I think I'm way off the mark there! My code is:

def ssh_command(ip, user, passwd, command):
   client = paramiko.SSHClient()
   client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
   client.connect(ip, username=user, password=passwd)
   stdin, stdout, stderr = client.exec_command(command)
   print stdout.read()
   print stderr.read()
   return 
if __name__ == '__main__':
   ssh_command(ip, user, passwd, command)

I've omitted the first few blocks of code which are where a few variables are defined by raw input from the user. It's rather long so I thought it best to omit but naturally, I can post it if needs be.

For those interested in the command I'm trying to run, it's an IBM command unique to their GPFS (Spectrum Scale) storage system. The command is:

mmdf mmfs1 --block-size auto

The command returns the storage space on all the disk pools on the storage system.

UPDATE:

The stderr.read() states the command isn't recognised (bash: mmdf: command not found) despite it working when SSH'd into the storage controller.

pynexj
  • 19,215
  • 5
  • 38
  • 56
sark
  • 71
  • 2
  • 6
  • did u ever check what's in `stderr.read()`? – pynexj Aug 21 '17 at 15:45
  • I hadn't, no, purely because I hadn't got experience with that. I've altered the code to output stderr.red() with `print stderr.read()` For the test I used the command I gave in my question, `mmdf mmfs1 --block-size auto` Interestingly the error message returned was `bash: mmdf: command not found` So I SSH'd into one of the storage servers and ran the command which worked as expected so maybe the script fails due to the gaps in the command? Almost like the script hits return after the first word. That said I did run a long command: `netstat -atnp | grep ESTA | grep 10.0.6.10` which worked! – sark Aug 22 '17 at 08:51

1 Answers1

4

Based on your latest comments you should use the absolute path to mmdf when running the command:

client.exec_command("/the/path/to/mmdf mmfs1 --block-size auto")

To find out where mmdf is, manually login to the server and run:

which mmdf
# or
type -P mmdf
pynexj
  • 19,215
  • 5
  • 38
  • 56
  • !!! that got it! Thank you so much for your help on this one whjm. Really appreciate that. Any idea as to why it requires the absolute path to the mmdf program? – sark Aug 22 '17 at 09:06
  • The command you're using must be in a special dir (other than `/bin`, `/usr/bin`, ...). When you login manually, `bash` would load `~/.bash_profile` or `~/.bashrc` which must have updated the `PATH` var to add that special dir to it. – pynexj Aug 22 '17 at 09:12
  • Interesting. I assume then the company that set the system up must have added symlink in the bash profile. – sark Aug 22 '17 at 09:52
  • see also: https://stackoverflow.com/questions/43937077/ – pynexj Aug 22 '17 at 09:56
  • Thanks for the link. – sark Aug 22 '17 at 10:10