-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheck_tshoot.sh
More file actions
102 lines (79 loc) · 3.47 KB
/
check_tshoot.sh
File metadata and controls
102 lines (79 loc) · 3.47 KB
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
95
96
97
98
99
100
101
102
#!/bin/bash
# Output CSV file
OUTPUT_FILE="tshoot_results.csv"
# SSH options
SSH_OPTIONS="-o ConnectTimeout=10 -o GSSAPIAuthentication=no -o PasswordAuthentication=no -o StrictHostKeyChecking=no"
# Initialize CSV header
echo "ip,user_tshoot,keterangan" > "$OUTPUT_FILE"
# Create a temporary IP file without any potential issues
TMP_IP_FILE=$(mktemp)
grep -v "^$" ip.txt | tr -d '\r' > "$TMP_IP_FILE"
# Count total IPs
total_ips=$(wc -l < "$TMP_IP_FILE")
echo "Found $total_ips IPs to process"
# Process each IP one by one using a counter
counter=1
# Debug - show IP file content
# echo "First 5 IPs from file:"
# head -n 5 "$TMP_IP_FILE"
# Loop through each IP explicitly
for ip in $(cat "$TMP_IP_FILE"); do
echo "[$counter/$total_ips] Checking IP: $ip"
# Flag to indicate if we can SSH to the server
ssh_success=false
# Users to try
users=("ubuntu" "cloud-user" "centos")
# For each user
for user in "${users[@]}"; do
# Try without key first
echo " Trying $user without key..."
ssh_output=$(ssh $SSH_OPTIONS "$user@$ip" "id tshoot 2>/dev/null || echo 'User tshoot not found'" 2>&1)
ssh_status=$?
# If SSH was successful
if [ $ssh_status -eq 0 ]; then
ssh_success=true
# Check if tshoot user exists and get detailed info
if [[ "$ssh_output" == *"uid="* && "$ssh_output" == *"tshoot"* ]]; then
tshoot_details=$(echo "$ssh_output" | tr -d '\n' | sed 's/,/;/g')
echo "$ip,\"FOUND: $tshoot_details\",Connected as $user without key" >> "$OUTPUT_FILE"
else
echo "$ip,\"NOT FOUND: User tshoot does not exist\",Connected as $user without key" >> "$OUTPUT_FILE"
fi
# Break the user loop since we found a working user
break
fi
# Try with key
echo " Trying $user with key..."
ssh_output=$(ssh $SSH_OPTIONS -i devops.pem "$user@$ip" "id tshoot 2>/dev/null || echo 'User tshoot not found'" 2>&1)
ssh_status=$?
# If SSH was successful
if [ $ssh_status -eq 0 ]; then
ssh_success=true
# Check if tshoot user exists and get detailed info
if [[ "$ssh_output" == *"uid="* && "$ssh_output" == *"tshoot"* ]]; then
tshoot_details=$(echo "$ssh_output" | tr -d '\n' | sed 's/,/;/g')
echo "$ip,\"FOUND: $tshoot_details\",Connected as $user with key" >> "$OUTPUT_FILE"
else
echo "$ip,\"NOT FOUND: User tshoot does not exist\",Connected as $user with key" >> "$OUTPUT_FILE"
fi
# Break the user loop since we found a working user
break
fi
done
# If no SSH connection was successful
if [ "$ssh_success" = false ]; then
echo "$ip,\"UNKNOWN: Cannot verify\",Cannot establish SSH connection with any user" >> "$OUTPUT_FILE"
fi
echo "-----------------------------------"
# Increment counter
((counter++))
done
# Clean up
rm -f "$TMP_IP_FILE"
# Check how many IPs were actually processed
processed_ips=$(grep -v "^ip" "$OUTPUT_FILE" | wc -l)
echo "Script completed. Processed $processed_ips out of $total_ips IPs."
echo "Results saved to $OUTPUT_FILE"
# Show a sample of results
echo "Sample results:"
head -n 3 "$OUTPUT_FILE"