-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathverify_pi.py
More file actions
60 lines (53 loc) · 1.75 KB
/
verify_pi.py
File metadata and controls
60 lines (53 loc) · 1.75 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
import subprocess
import sys
def run_remote_command(ip, user, password, command):
print(f"Running '{command}' on {user}@{ip}...")
# Create an expect script dynamically
expect_script = f"""
set timeout 20
spawn ssh {user}@{ip} "{command}"
expect {{
"password:" {{
send "{password}\\r"
expect {{
"Permission denied" {{ exit 1 }}
eof {{ exit 0 }}
}}
}}
"yes/no" {{
send "yes\\r"
exp_continue
}}
timeout {{ exit 2 }}
eof {{ exit 1 }}
}}
catch wait result
exit [lindex $result 3]
"""
try:
# Run expect script
p = subprocess.run(['expect', '-c', expect_script], capture_output=True, text=True)
if p.returncode == 0:
# Filter out the SSH login noise if possible, but usually expect captures everything
# We'll just print the stdout
print(f"\n--- Output of '{command}' ---")
# The output usually contains the password prompt and everything, so it might be messy.
# But it proves connection.
print(p.stdout)
return True
else:
print(f"Failed to run command. Exit code: {p.returncode}")
print("Output:", p.stdout)
print("Error:", p.stderr)
return False
except Exception as e:
print(f"Error: {e}")
return False
target_ip = "172.18.161.85"
user = "classnova"
password = "hari1388"
# 1. Check OS Info
if run_remote_command(target_ip, user, password, "uname -a && cat /etc/os-release"):
print("\n✅ Verification Successful! Connected to Raspberry Pi.")
else:
print("\n❌ Verification Failed.")