linuxiscsilinux-scst

Get target IP address of an iscsi device


Suppose I have an iSCSI device /dev/sdat, how do I know the IP address of it's target?

The target driver is SCST, and the initiator is iSCSI. All I know is a device named /dev/sdat and nothing more. So how to get the IP address of it's target?


Solution

  • Well, I'm not proud of this, but it gets the job done. At least for some definitions of getting the job done.

    The basic idea is this. You can get the target IQN from the output of lsscsi -t. (You'll need the lsscsi program if you don't already have it. I think you'll find it's essential in any kind of SCSI environment.)

    # lsscsi -t
    [2:0:0:0]    disk    iqn.2009-12.com.blockbridge:t-pjxfzufjkp-illoghjk,t,0x1  /dev/sda
    [3:0:0:0]    disk    iqn.2009-12.com.blockbridge:t-pjxfzuecga-eajejghg,t,0x1  /dev/sdb
    [4:0:0:0]    disk    iqn.2009-12.com.blockbridge:t-pjxfzufjjo-pokqaja,t,0x1  /dev/sdd
    [5:0:0:0]    disk    iqn.2009-12.com.blockbridge:t-pjxfzufnfg-cqikkgl,t,0x1  /dev/sdc
    

    Then, you can feed the target IQN into iscsiadm and grep around in the output for the target address.

    # iscsiadm -m node -T iqn.2009-12.com.blockbridge:t-pjxfzufjkp-illoghjk | egrep 'node.conn.+address'
    
    node.conn[0].address = 172.16.5.148
    

    Putting it all together, you get a script like this. Of course, this is absent all kinds of error handling, and probably doesn't handle about 23 different cases. But, hey... It works in my environment!

    #!/usr/bin/bash
    
    if [[ -z $1 ]]; then
        >&2 echo "Usage: devip.sh <device>"
        exit 1
    fi
    
    iqn=$(sudo lsscsi -t | grep "$1" | grep iqn | awk '{print $3}' | awk -F , '{print $1}')
    if [[ -z "$iqn" ]]; then
        >&2 echo "IQN not found for \"$1\"."
        exit 1
    fi
    
    sudo iscsiadm -m node -T $iqn | egrep 'node.conn.+address' | awk -F ' *= *' '{print $2}'
    exit $?