Get target IP address of an iscsi device

笑着哭i 提交于 2020-12-13 03:41:26

问题


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?


回答1:


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 $?


来源:https://stackoverflow.com/questions/44154274/get-target-ip-address-of-an-iscsi-device

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!