#! /usr/bin/env perl use strict; use warnings; # change following to be a list of 1 or more iSCSI targets to be queried my @servers = ( '10.10.10.10','10.10.10.9' ); my %targets; foreach my $server ( @servers ) { print "\n" . '-'x40 . "\nGetting targets on server $server\n" . '-'x40 . "\n"; my @list = `iscsiadm -m discovery -t st -p $server`; chomp @list; # @list contains lines of type # 10.19.209.2:3260,1 iqn.2014-11.net.dailydata.castor:simon0 # split them apart and add them to the hash foreach my $entry ( @list ) { my ( $portal, $targetName ) = split( ' ', $entry ); # $portal has some extra info after a comma, so clean it up $portal =~ m/^([0-9:.]+)/; $portal = $1; # some targets return multiple IP's for a given name, so # only add them if they are in this IP $targets{ $targetName } = $portal if $portal =~ m/^$server/; print "$targetName\t$targets{ $targetName }\n"; } } print "\n" . '-'x40 . "\nGetting active sessions\n". '-'x40 . "\n"; # now, get active sessions so we can filter them my @activeSessions = `iscsiadm -m session`; chomp @activeSessions; foreach my $session ( @activeSessions ) { $session =~ m/^.*[^0-9:.]([0-9,:.]+).*(iqn\S*)/; my ( $portal,$targetName ) = ( $1,$2 ); print "$portal\t$targetName"; if ( exists( $targets{$targetName} ) ) { print "\tNOT updating\n"; delete $targets{ $targetName }; } else { print "Needs to be added\n"; } } # check if we have any new entries and bail if not if ( scalar keys %targets ) { # We have new entries, so run them; foreach my $targetName ( sort keys %targets ) { my $portal = $targets{$targetName}; print "Adding $targetName\n"; `iscsiadm -m node --targetname '$targetName' --portal '$portal' --login`; } } else { print "No new entries\n"; } # print `ls /dev/disk/by-path/`;