-
Notifications
You must be signed in to change notification settings - Fork 251
/
auto-iscsi.sh
95 lines (71 loc) · 2.06 KB
/
auto-iscsi.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#!/bin/bash
# Display banner
cat << "EOF"
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|P|e|n|t|e|s|t|P|a|c|k|a|g|e|
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Script: auto-iscsi.sh
Author: Leon Teale
Twitter: @leonteale
Website: cyberwolf-security.co.uk
Version: 1.0.5
EOF
# Define color codes
RED='\033[0;31m'
GREEN='\033[0;32m'
NC='\033[0m' # No Color
# Check if iscsiadm is installed
if ! command -v iscsiadm &> /dev/null; then
echo -e "${RED}iscsiadm could not be found. Please install it first.${NC}"
exit 1
fi
USAGE='
Usage: auto-iscsi.sh [IP]
Options:
IP | The IP address for the iSCSI target
Example usage:
./auto-iscsi.sh 10.133.36.191
'
# Check for root privileges
if [ "$(id -u)" != "0" ]; then
echo -e "${RED}This script must be run as root${NC}" 1>&2
exit 1
fi
# Check for correct number of arguments
if [ "$#" -ne 1 ]; then
echo -e "$USAGE"
exit 1
fi
IP=$1
MOUNT_DIR="/tmp/temp-$IP"
echo -e "\n${GREEN}Discovering iSCSI targets at IP: $IP${NC}\n"
# Discover the iSCSI targets
TARGETS=$(iscsiadm -m discovery -t st -p $IP)
# Check if any targets were found
if [ -z "$TARGETS" ]; then
echo -e "${RED}No targets found at IP: $IP${NC}"
exit 1
fi
# Extract the first target name
TARGET_NAME=$(echo "$TARGETS" | awk '{print $2}' | head -n1)
# Attempt to logout first to clear any existing sessions
iscsiadm --mode node --targetname $TARGET_NAME --portal $IP --logout
echo -e "Attempting to login to target: ${GREEN}$TARGET_NAME${NC}\n"
# Login to the target
iscsiadm --mode node --targetname $TARGET_NAME --portal $IP --login
if [ $? -ne 0 ]; then
echo -e "${RED}Failed to login to target${NC}"
exit 1
fi
# Set the device path to /dev/sdb2
DEVICE_PATH="/dev/sdb2"
echo -e "Creating mount directory: ${GREEN}$MOUNT_DIR${NC}"
mkdir -p $MOUNT_DIR
echo -e "\nAttempting to mount ${GREEN}$DEVICE_PATH${NC} to ${GREEN}$MOUNT_DIR${NC}"
mount -o ro $DEVICE_PATH $MOUNT_DIR
if [ $? -ne 0 ]; then
echo -e "${RED}Failed to mount $DEVICE_PATH${NC}"
exit 1
fi
echo -e "${GREEN}Successfully mounted $DEVICE_PATH to $MOUNT_DIR${NC}\n"
cd $MOUNT_DIR