import requests
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
# Import username and password from op_config.py
from op_config import username, password
# Login and get cookies
login_url = 'http://127.0.0.1/cgi-bin/luci/'
passwall2_url = 'http://127.0.0.1/cgi-bin/luci/admin/services/passwall2/node_list'
# Login using requests
session = requests.Session()
payload = {
'luci_username': username,
'luci_password': password
}
response = session.post(login_url, data=payload, allow_redirects=True)
# Create Selenium WebDriver
options = Options()
options.add_argument("--headless") # Optional: run in headless mode
options.add_argument("--no-sandbox") # For Docker environment
options.add_argument("--disable-dev-shm-usage") # For Docker environment
service = Service('/path/to/chromedriver') # Replace with the actual path to chromedriver
driver = webdriver.Chrome(service=service, options=options)
# Access the base page
driver.get('http://127.0.0.1')
# Set cookies
cookies = session.cookies.get_dict()
for cookie_name, cookie_value in cookies.items():
driver.add_cookie({
'name': cookie_name,
'value': cookie_value,
'domain': '127.0.0.1',
'path': '/' # Ensure path is correct
})
# Access Passwall2 page and extract data
def extract_tcping_values(driver):
driver.get(passwall2_url)
rows = WebDriverWait(driver, 30).until(
EC.presence_of_all_elements_located((By.CSS_SELECTOR, 'tr.tr.cbi-section-table-row'))
)
include_ids = {'cbi-passwall2-node00001', 'cbi-passwall2-node00002', 'cbi-passwall2-node00003',
'cbi-passwall2-node00004', 'cbi-passwall2-node00005', ...}
results = []
for row in rows:
row_id = row.get_attribute('id')
if row_id in include_ids:
tcping_value = extract_tcping_value(row)
print(row_id, tcping_value) # Print raw value for debugging
# Check if tcping_value is a valid ms value
if tcping_value.endswith('ms'):
try:
# Extract value and check if it is less than 200ms
value = int(tcping_value.split()[0])
if value < 200:
continue # Skip if less than 200ms
except ValueError:
print(f"Shunt tcping timeout...")
# Add row_id to results list if it meets the condition
results.append(row_id)
return results
def extract_tcping_value(row):
try:
tcping_element = WebDriverWait(row, 5).until(
EC.presence_of_element_located((By.CSS_SELECTOR, 'div[id$="-tcping"] span'))
)
return tcping_element.text
except Exception as e:
return 'Error: ' + str(e)
# Extract TCPing values and write to file
log_dir = '/app' # Ensure this directory exists
if not os.path.exists(log_dir):
os.makedirs(log_dir)
tcping_ids = extract_tcping_values(driver)
with open(os.path.join(log_dir, 'tcping_ids.txt'), 'w', encoding='utf-8') as file:
for row_id in tcping_ids:
file.write(f"{row_id.replace('cbi-passwall2-', '')}\n")
# Close the browser
driver.quit()
Key Points:
- Exception Handling: Handles potential errors when converting
TCPing
values to integers.
- Filtering Logic: Skips items with
TCPing
values less than 200ms and only includes nodes that meet the condition in the results list.
- Debug Output: Prints the raw
TCPing
value for debugging purposes.