41 lines
1.3 KiB
Python
41 lines
1.3 KiB
Python
#!/usr/bin/env python3
|
|
import asyncio
|
|
from telegram import Bot
|
|
import subprocess
|
|
import re
|
|
|
|
def get_eth0_ip():
|
|
try:
|
|
result = subprocess.run(['ip', 'addr', 'show', 'eth0'], capture_output=True, text=True, check=True)
|
|
ip_match = re.search(r'inet (\S+)', result.stdout)
|
|
if ip_match:
|
|
return ip_match.group(1).split('/')[0] # CIDR notation (/20) 제거
|
|
else:
|
|
return None
|
|
except subprocess.CalledProcessError as e:
|
|
print(f"Error while getting eth0 IP: {e}")
|
|
return None
|
|
|
|
async def send_ip_to_telegram(bot_token, chat_id, ip_address):
|
|
bot = Bot(token=bot_token)
|
|
message = f"Current IP Address (eth0): `{ip_address}`"
|
|
|
|
try:
|
|
await bot.send_message(chat_id=chat_id, text=message, parse_mode=None)
|
|
print("Message sent successfully!")
|
|
except Exception as e:
|
|
print(f"Failed to send message. Error: {e}")
|
|
|
|
# Telegram Bot 토큰과 메시지를 전송할 대상 채팅 ID를 설정합니다.
|
|
telegram_bot_token = "6967352916:AAFxPbushvtd7jfY-"
|
|
telegram_chat_id = "6184902907"
|
|
|
|
# eth0의 IP 주소를 얻습니다.
|
|
eth0_ip = get_eth0_ip()
|
|
|
|
if eth0_ip:
|
|
# 얻은 IP 주소를 Telegram으로 보냅니다.
|
|
asyncio.run(send_ip_to_telegram(telegram_bot_token, telegram_chat_id, eth0_ip))
|
|
else:
|
|
print("Failed to retrieve eth0 IP address.")
|