0

I'm trying to use IPMITOOL to query a bunch of OOB NICs to get their MAC addresses.

When I run:

 ipmitool -I lanplus -H 1.2.3.4 -U USER -P PASS lan print

I get:

MAC Address             : aa:bb:cc:dd:ee:ff

If I try to call it via a shell script that iterates over a CSV file:

#!/bin/bash
INPUT=/home/user/list_of_systems.csv
OLDIFS=$IFS
IFS=','
while read hostname ipaddr
do
        printf "$hostname\t" >> output.txt
        ipmitool -I lanplus -H $ipaddr -U USER -P PASS lan print
done < $INPUT
IFS=$OLDIFS

I get:

 failed lookup for 1.2.3.4
Could not open socket!
Error: Unable to establish IPMI v2 / RMCP+ session

What am I missing here? I have been tearing my hair out over this.

Driftpeasant
  • 3,217
  • 2
  • 22
  • 28
  • 1
    What's the difference between your environment and the sudo environment? – glenn jackman May 26 '21 at 15:21
  • Oh, derp. The sudo was from testing, not when I ran it. I'll delete. Good catch. – Driftpeasant May 26 '21 at 15:34
  • 2
    Does the input file have `\r\n` line endings? Run with `bash -x script.sh` and see if you can spot any unusual values for ipaddr – glenn jackman May 26 '21 at 15:47
  • We have a winner! That did it. I saw the \r, converted the line endings, and blam, it worked! If you want to post this as an actual answer, I'll mark it as complete ASAP. – Driftpeasant May 26 '21 at 16:01
  • 1
    Are you setting `IFS` just to control how `read` splits fields? If so, make the assignment a prefix to the `read` command (`while IFS=',' read hostname ipaddr`) so it only affects that one command and you don't have to deal with `OLDIFS`. I'd also recommend double-quoting variables (e.g. `"$ipaddr"` instead of just `$ipaddr`), and keeping format and data separate (`printf "%s\t" "$hostname"` instead of printf "$hostname\t"`). [shellcheck.net](https://www.shellcheck.net) will spot some of these -- I recommend running all your scripts through it to see what problems it spots. – Gordon Davisson May 27 '21 at 17:53
  • 1
    Oh, and `while IFS=$',\r' read ...` will auto-trim carriage returns from the end of lines as it reads. – Gordon Davisson May 27 '21 at 17:54

1 Answers1

0

Per https://serverfault.com/users/30957/glenn-jackman I had bad line endings in the CSV file. After fixing those, this started to work.

Driftpeasant
  • 3,217
  • 2
  • 22
  • 28