爬虫配置拨号服务器,云立方拨号服务器使用教程

 https://cuiqingcai.com/3443.html

--------------------

www.yunlifang.cn

云立方购买的服务器 不需要 配置 拨号 ,直接使用命令拨号 

云立方 使用

adsl-start 拨号
adsl-stop 停止

下面不是云立方使用

pppoe-start 拨号
pppoe-stop  断开拨号
pppoe-status 拨号连接状态

----------------

TinyProxy 安装

wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo
yum update
yum install -y tinyproxy
vim /etc/tinyproxy/tinyproxy.conf 
--
  修改配置文件

  Port 8888 #预设是8888 Port,你可以更改
  Allow 127.0.0.1 #将127.0.0.1改成你自己的IP
  #例如你的IP 是1.2.3.4,你改成Allow 1.2.3.4,那只有你才可以连上这个Proxy
  #若你想任何IP都可以脸到Proxy在Allow前面打#注释

启动

service tinyproxy start

查看端口

netstat -tnlp


下面是 使用 python 控制 拨号服务器

import re

import paramiko

from config.config import ADSLHOST, ADSLPORT, ADSLUSER, ADSLPWD


class Monitor(object):
    def __init__(self, server_ip, port, user, pwd):
        """ 初始化ssh客户端 """
        try:
            client = paramiko.SSHClient()
            client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
            self.client = client
            print('------------开始连接服务器(%s)-----------' % server_ip)
            self.client.connect(server_ip, port, username=user, password=pwd, timeout=4)
            print('------------认证成功!.....-----------')
        except Exception:
            print('连接远程linux服务器(ip:{server_ip})发生异常!请检查用户名和密码是否正确!')

    def link_server(self, cmd):
        """连接服务器发送命令"""
        try:
            stdin, stdout, stderr = self.client.exec_command(cmd)
            content = stdout.read().decode('utf-8')
            return content
        except Exception as e:
            print('link_server-->返回命令发生异常,内容:', e)
            # finally:
            self.client.close()

    def close_net(self):
        try:
            self.client.close()
        except Exception as e:
            print ("关闭连接error")


def get_this_ip():
    """
    :return: 返回ip
    """
    m = Monitor(ADSLHOST, ADSLPORT, ADSLUSER, ADSLPWD)
    all = m.link_server("ifconfig")
    m.close_net()
    ip = parseIfconfig(all)
    return ip


def parseIfconfig(data):
    data = data.split('

')
    data = [i for i in data if i and i.startswith('ppp0')]

    # dic = {}
    # re.M 多行模式,改变'^'和'$'的行为
    for line in data:
        # re_devname = re.compile(r'(w+).*Link encap', re.M)
        # re_macaddr = re.compile(r'HWaddrs([0-9A-F:]{17})', re.M)
        re_ipaddr = re.compile(r'inet ([d.]{7,15})', re.M)
        # re_ipaddr = re.compile(r'inet addr:([d.]{7,15})', re.M)
        # devname = re_devname.search(line)
        # mac = re_macaddr.search(line)
        ip = re_ipaddr.search(line)
        # if devname:
        #     devname = devname.group(1)
        # else:
        #     devname = ''
        #
        # if mac:
        #     mac = mac.group(1)
        # else:
        #     mac = ''

        if ip:
            ip = ip.group(1)
        else:
            ip = ''
        # dic[devname] = [mac, ip]
    print ip
    return ip


def change_ip():
    """
    拨号间隔5s
    :return:
    """
    m = Monitor(ADSLHOST, ADSLPORT, ADSLUSER, ADSLPWD)
    m.link_server("adsl-stop")
    m.link_server("adsl-start")
    all = m.link_server("ifconfig")
    m.close_net()
    ip = parseIfconfig(all)
    return ip


if __name__ == '__main__':
    d = change_ip()
    print d

拨号服务器 不稳定 出现 不断的增加 tiny 后台进程 

所以, 定时 杀掉 进程 重启 一下

 vim /etc/crontab

[root@localhost /]# vim /etc/crontab

SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root

# For details see man 4 crontabs

# Example of job definition:
# .---------------- minute (0 - 59)
# |  .------------- hour (0 - 23)
# |  |  .---------- day of month (1 - 31)
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# |  |  |  |  |
# *  *  *  *  * user-name  command to be executed
0 */6 * * * root /restart_tiny.sh

chmod 777 restart_tiny.sh

restart_tiny.sh 放在 跟目录下 
#!/bin/sh

echo '正在停止app应用程序'

app_status=`ps -ef | grep tiny | grep -v grep| awk '{print $2}'`    
    
if [[ $app_status -gt 0 ]];
then    
   kill -9 $app_status
   echo 'app程序已停止运行!'
fi  

echo 'kill完成'
service tinyproxy start &
echo '启动tiny'

重启定时任务 crontab /etc/crontab

原文地址:https://www.cnblogs.com/angdh/p/12482360.html