如何在由 VNC-viewer 远程控制的 Ubuntu 服务器上启动 VPN?

如何在由 VNC-viewer 远程控制的 Ubuntu 服务器上启动 VPN?

如何在由 VNC-viewer 远程控制的 Ubuntu 服务器上启动 VPN(以更改服务器 IP 地址)?

我使用 Expressvpn 和本指南:https://www.expressvpn.com/support/vpn-setup/linux-openvpn-terminal/在 Ubuntu 14.04.3 LTS 上设置 VPN。当我尝试连接到所需位置(指南中的第 5 步)时,我从 VNC 查看器中注销。我相信 IP 地址已更改,但现在我必须通过在 VNC 查看器中输入新 IP 地址来重新连接到我的服务器。我不知道新生成的 IP 地址,因为每次启动 VPN 时 VPN IP 地址都不同。Expressvpn 支持团队不知道如何解决这个问题。我花了好几天时间尝试自己解决这个问题,但没有可行的解决方案。我将非常感激任何帮助。

答案1

我通过创建一个来解决这个问题python 脚本每次检查 Linux 机器上的外部 IP,如果不同就通过 gmail 发给我。我以前cron每 3 分钟运行一次。这真的很笨重,但效果很好。

#! /usr/bin/env python


# Check for internet connection

import urllib2
import time
import string

def internet_on():
    try:
        response=urllib2.urlopen('http://www.google.com/',timeout=1)
        return True
    except urllib2.URLError as err: pass
    return False

#loop_value = 1
#while (loop_value == 1):
#    try:
#   urllib2.urlopen("http://www.google.com")
#    except urllib2.URLError, e:
#   time.sleep( 10 )
#    else:
#   loop_value = 0

# Commands to be run if internet connection is present:
if internet_on():
    # Get external IP from canyouseeme.org by searching html page for an IP address
    import urllib
    import re 
    f = urllib.urlopen("http://myip.dnsdynamic.org/")
    html_doc = f.read()
    f.close()
    m = re.search('(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)',html_doc)
    # print html_doc
    #print m.group(0)
    current_ipaddress = m.group(0)

    # Look at last line of ip log file
    IPlog = open ('iplog.txt')
    lineList = IPlog.readlines()
    IPlog.close()
    # print (lineList)
    # print ("The last line is:")
    # print (lineList[len(lineList)-1])
    # or simply
    # print (lineList[-1])
    # print current_ipaddress

    # See if current IP has changed from last logged
    if current_ipaddress + "\n" <> lineList[-1]:
        #Append ip address to ip log file
        f = open('iplog.txt','a')
        localtime = time.asctime( time.localtime(time.time()) )
        f.write(localtime + "\n" + current_ipaddress + "\n")
        # python will convert \n to os.linesep
        f.close()

        # Mail new ip    
        import smtplib
        smtp_server = 'smtp.gmail.com'
        smtp_port = 587
        sender = '****@gmail.com'
        # recipients = ["*****[email protected]", "john@***.com"]
        recipients = ["*****@***.com"]


        subject = 'SERVER ALERT'
        body = 'Server IP address has changed to: ' + current_ipaddress
        password = '**********************'
        smstext = body 
        body = "" + body + ""

        headers = ["From: " + sender,
               "Subject: " + subject,
               #"To: " + "Fred and John",
               "To: " + "John",
               "MIME-Version: 1.0",
               "Content-Type: text/html"]
        headers = "\r\n".join(headers)

        session = smtplib.SMTP(smtp_server, smtp_port)

        session.ehlo()
        session.starttls()
        session.ehlo
        session.login(sender, password)

        session.sendmail(sender, recipients, headers + "\r\n\r\n" + body)
        session.quit()


        # Google Voice SMS send
        import pygvoicelib
        conn = pygvoicelib

        username="**************"
        apppass="****************************"
        auth_token="***********************************"
        client = conn.GoogleVoice(username,apppass,auth_token,rnr_se)
        #phone number below
        client.sms('1212*******',smstext)

相关内容