#!/usr/bin/expect -f # Set a 5-second timeout for all network operations set timeout 5 # Define Tor connection details and password set host "127.0.0.1" set port "8050" set password "tor" # Spawn the netcat connection spawn nc $host $port # Step 1: Send the authentication command immediately using the variable send "AUTHENTICATE \"$password\"\r" # Step 2: Expect the authentication confirmation expect { "250 OK" { # Authentication successful, proceed to check status send "GETINFO status/circuit-established\r" } timeout { send_user "ERROR: Authentication timed out.\n" exit 1 } eof { send_user "ERROR: Connection closed by Tor during authentication.\n" exit 1 } } # Step 3: Parse the multi-line response sequentially expect { -re "250-status/circuit-established=(0|1)" { # Capture the 0 or 1 from the regex match group set circuit_status $expect_out(1,string) # Step 4: Clear the trailing 250 OK line from the buffer expect "250 OK" # Step 5: Evaluate the result if {$circuit_status == 1} { send_user "SUCCESS: Tor circuit is established.\n" exit 0 } else { send_user "FAILURE: Tor circuit is NOT established yet.\n" exit 1 } } timeout { send_user "ERROR: GETINFO command timed out.\n" exit 1 } eof { send_user "ERROR: Connection closed by Tor during status check.\n" exit 1 } }