Python实现SMTP+Cpolar+Selenium 折腾方式内网穿透Windows远程控制

之前出于一些原因想远程控制一台电脑办公,使用cpolar内网穿透获得公网ip,然后使用邮件传输。但cpolar免费用户公网ip是变化的,所以就想使用web自动化获取一下。

Cpolar

在win10专业版系统中,自带有微软远程桌面可以使用,但在没有进行配置的情况下,只能在局域网中使用。因此,我们可以通过cpolar内网穿透来映射远程桌面3389端口,使用所生成的公网TCP端口地址,实现在外随时随地远程桌面控制家里/公司的电脑。

cpolar官网:https://www.cpolar.com/

  • 注册与安装

cpolar免费注册用户可以提供1个在线cpolar进程,4个隧道/cpoloar进程,40个连接数/分钟。官网注册后直接安装,进行登录。

  • 创建与启动远程桌面隧道

cpolar安装成功后会默认安装两个样例隧道,没有的话可以在仪表盘界面的隧道管理创建一个远程桌面隧道。

远程桌面隧道
在浏览器上访问127.0.0.1:9200,邮箱密码登录cpolar webui管理界面,点击启动来开启隧道。

  • 获取远程桌面公网ip

隧道启动成功后,点击左侧仪表盘状态中的在线隧道列表,可以看到远程桌面隧道已经有生成了相应的公网地址。
公网ip
如果想省事可以在cpolar购买一个会员,公网链接tcp是固定的,但免费账号每天tcp都会变化,所以需要每天更新一遍。

我这边因为不想花钱所以就想用web自动化来获取。

Web自动化Selenium

Spider.py文件使用python的web自动化库selenium,获取网页数据。

from selenium import webdriver
from lxml import etree
import time
from selenium.common import exceptions
from selenium.webdriver.common.by import By
from selenium.webdriver.remote.webelement import WebElement

class Spider:

    def log(self, content):
        now_time = time.strftime(
            "%Y-%m-%d %H:%M:%S", time.localtime()
        )
        print(f'{
      
      now_time}: {
      
      content}')

    def getip(self, url):
        driver = webdriver.Chrome()
        # url = 'http://localhost:9200/#/status/online/'

        driver.get(url)
        html = etree.HTML(driver.page_source)
        print(html)
        content = WebElement

        '''
            这里由于每次打开网页需要登录,所以设置一个登录过程。
            网站默认会在密码栏输入123456,所以我就把密码设置成了123456,就省了输入的功夫了
            如果不想用默认密码,就用clear删掉,然后输入密码
        '''
        try:
            # 你的邮箱
            driver.find_element(By.XPATH, '//*[@placeholder="Email"]').send_keys('[email protected]')
            # driver.find_element_by_xpath('//*[@placeholder="Password"]').clear()
            # 你的密码
            # driver.find_element_by_xpath('//*[@placeholder="Password"]').send_keys('123456')
            driver.find_element(By.XPATH, '//*[@type="button"]').click()

            time.sleep(3)

            # 这里获取公网ip的位置
            content = driver.find_element(By.XPATH, '/html/body/div/div/div[2]/section/div/div/div[3]/table/tbody/tr[2]/td[3]/div')
            # print(content.text)
            self.log("公网ip为"+content.text)
        except BaseException as e1:
            print(e1)
            self.log("无法获取ip,BaseException错误")
        except exceptions as e2:
            print(e2)
            self.log("无法获取ip,SELENIUM错误")

        return content.text

除了需要修改自己的账号密码外,需要自己在cpolar仪表盘按F12找一下公网ip对应的网格。
找到远程桌面ip的网格
然后右键一下右边网格对应的位置,选择full xpath替换掉就可以了。
复制XPATH

邮件自动化

获取到公网ip后就可以使用邮件协议,通过python给邮箱发邮件了。
这里我用的是SMTP协议,可以在自己要用的邮箱设置中找一下。
具体的操作可以看一下教程:https://www.runoob.com/python/python-email.html

SMTP.py文件需要设置自己的邮箱和协议密码。

import time
import yagmail


class Mail:
    """
    邮件相关类
    """

    def log(self, content):
        now_time = time.strftime(
            "%Y-%m-%d %H:%M:%S", time.localtime()
        )
        print(f'{
      
      now_time}: {
      
      content}')

    def sendmail(self, msg, title, receivers):
        """
        发送邮件

        Arguments:
            msg {str} -- 邮件正文
            title {str} -- 邮件标题
            receivers {list} -- 邮件接收者,数组
        """
        now_time = time.strftime(
            "%Y-%m-%d %H:%M:%S", time.localtime()
        )

        # 邮件设置,输入你的邮箱和协议密码,我这里用的126邮箱
        yag = yagmail.SMTP(
            host='smtp.126.com', user='[email protected]',
            password='password', smtp_ssl=True
        )

        try:
            yag.send(receivers, title, msg+"\n"+(f'{
      
      now_time}'))
            self.log("邮件发送成功")

        except BaseException as e:
            print(e)
            self.log("Error: 无法发送邮件")
            

然后就可以使用邮件发送公网ip了。注意需要导入前面两个py文件。

import datetime
import time
import requests
from SMTP import Mail
from Spider import Spider


while True:
    response = requests.get('http://localhost:9200/#/status/online')

    url = 'http://localhost:9200/#/status/online/'
    # 获取公网ip
    OpenIP = Spider().getip(url)

    # 每天5,12点发送
    # if time.strftime("%H", time.localtime()) == ("5" or '10' or "23"):
    if datetime.datetime.now().hour == 5 or datetime.datetime.now().hour == 12 or datetime.datetime.now().hour == 20:
        Mail().sendmail(
            OpenIP, '公网ip', ['[email protected]', '[email protected]']
        )

    # 根据状态码判断网站是否正常
    if response.status_code != 200:
        Mail().sendmail(
            OpenIP+"\n"+"Cpolar崩溃了", 'Cpolar崩溃', ['[email protected]']
        )

    # time.sleep(600)  # 等待10分钟
    time.sleep(3300)  # 等待55分钟

最后在邮件中得到的公网ip就可以实现远程桌面了。
连接远程桌面
点击连接,输入被控端电脑的账号及密码,就可远程桌面登录了。

登录系统
最后为了数据安全,可以在用户中新建一个远程桌面用户组,给予部分权限。

ps:是真的折腾

猜你喜欢

转载自blog.csdn.net/cbx0916/article/details/130565386
今日推荐