LINUX.ORG.RU

История изменений

Исправление Qwentor, (текущая версия) :

Скрипт просто запускает кучу впн и прописывает маршруты

import netifaces
import subprocess
import os
import glob
from pprint import pprint
from time import sleep, time


class cd:
	"""Context manager for changing the current working directory"""
	def __init__(self, newPath):
		self.newPath = os.path.expanduser(newPath)

	def __enter__(self):
		self.savedPath = os.getcwd()
		os.chdir(self.newPath)

	def __exit__(self, etype, value, traceback):
		os.chdir(self.savedPath)

def get_taps():
	interfaces = netifaces.interfaces()
	taps = []
	for interface in interfaces:
		if interface[:3] == 'tap':
			taps.append(interface)
	return taps



 

with cd("~/vpn"):
	ovpns = glob.glob('*.ovpn')
	print(ovpns)
	i = 0
	for ovpn in ovpns:
		print(i)
		subprocess.call(['sudo', 'screen', '-d', '-m', '-S', ovpn[:-5], 'openvpn','--route-noexec', '--config', ovpn])
		StartTime = time()
		while True:
			CurTime = time()
			if CurTime - StartTime > 7:
				subprocess.call('sudo', 'screen', '-X', '-S', ovpn[:-5], 'quit')
				break
			elif(len(get_taps()) > i):
				sleep(1)
				ip = netifaces.ifaddresses('tap'+str(i))[2][0]['addr']
				print(ovpn, " - ", ip)
				subprocess.call(['sudo', 'ip', 'rule', 'add', 'from', ip, 'lookup', 'viatap'+str(i)])
				subprocess.call(['sudo', 'ip', 'route', 'add', 'default', 'dev', 'tap'+str(i), 'table', 'viatap'+str(i)])
				i+=1
				break

Подскажешь как лучше?

Исправление Qwentor, :

Скрипт просто запускает кучу впн и прописывает маршруты

import netifaces
import subprocess
import os
import glob
from pprint import pprint
from time import sleep, time


class cd:
	"""Context manager for changing the current working directory"""
	def __init__(self, newPath):
		self.newPath = os.path.expanduser(newPath)

	def __enter__(self):
		self.savedPath = os.getcwd()
		os.chdir(self.newPath)

	def __exit__(self, etype, value, traceback):
		os.chdir(self.savedPath)

def get_taps():
	interfaces = netifaces.interfaces()
	taps = []
	for interface in interfaces:
		if interface[:3] == 'tap':
			taps.append(interface)
	return taps



 

with cd("~/vpn"):
	ovpns = glob.glob('*.ovpn')
	print(ovpns)
	i = 0
	for ovpn in ovpns:
		print(i)
		subprocess.call(['sudo', 'screen', '-d', '-m', '-S', ovpn[:-5], 'openvpn','--route-noexec', '--config', ovpn])
		StartTime = time()
		while True:
			CurTime = time()
			if CurTime - StartTime > 7000:
				subprocess.call('sudo', 'screen', '-X', '-S', ovpn[:-5], 'quit')
				break
			elif(len(get_taps()) > i):
				sleep(1)
				ip = netifaces.ifaddresses('tap'+str(i))[2][0]['addr']
				print(ovpn, " - ", ip)
				subprocess.call(['sudo', 'ip', 'rule', 'add', 'from', ip, 'lookup', 'viatap'+str(i)])
				subprocess.call(['sudo', 'ip', 'route', 'add', 'default', 'dev', 'tap'+str(i), 'table', 'viatap'+str(i)])
				i+=1
				break

Подскажешь как лучше?

Исправление Qwentor, :

Скрипт просто запускает кучу впн и прописывает маршруты

import netifaces
import subprocess
import os
import glob
from pprint import pprint
from time import sleep, time


class cd:
	"""Context manager for changing the current working directory"""
	def __init__(self, newPath):
		self.newPath = os.path.expanduser(newPath)

	def __enter__(self):
		self.savedPath = os.getcwd()
		os.chdir(self.newPath)

	def __exit__(self, etype, value, traceback):
		os.chdir(self.savedPath)

def get_taps():
	interfaces = netifaces.interfaces()
	taps = []
	for interface in interfaces:
		if interface[:3] == 'tap':
			taps.append(interface)
	return taps



 

with cd("~/vpn"):
	ovpns = glob.glob('*.ovpn')
	print(ovpns)
	i = 0
	for ovpn in ovpns:
		print(i)
		subprocess.call(['sudo', 'screen', '-d', '-m', '-S', ovpn[:-5], 'openvpn','--route-noexec', '--config', ovpn])
		StartTime = time()
		while True:
			CurTime = time()
			if CurTime - StartTime > 7000:
				subprocess.call('sudo', 'screen', '-X', '-S', ovpn[:-5], 'quit')
				break
			elif(len(get_taps()) > i):
				sleep(1)
				ip = netifaces.ifaddresses('tap'+str(i))[2][0]['addr']
				print(ovpn, " - ", ip)
				subprocess.call(['sudo', 'ip', 'rule', 'add', 'from', ip, 'lookup', 'viatap'+str(i)])
				subprocess.call(['sudo', 'ip', 'route', 'add', 'default', 'dev', 'tap'+str(i), 'table', 'viatap'+str(i)])
				i+=1
				break

Исходная версия Qwentor, :

Скрипт просто запускает кучу впн и прописывает маршруты

import netifaces
import subprocess
import os
import glob
from pprint import pprint
from time import sleep, time


class cd:
	"""Context manager for changing the current working directory"""
	def __init__(self, newPath):
		self.newPath = os.path.expanduser(newPath)

	def __enter__(self):
		self.savedPath = os.getcwd()
		os.chdir(self.newPath)

	def __exit__(self, etype, value, traceback):
		os.chdir(self.savedPath)

def get_taps():
	interfaces = netifaces.interfaces()
	taps = []
	for interface in interfaces:
		if interface[:3] == 'tap':
			taps.append(interface)
	return taps



 

with cd("~/vpn"):
	ovpns = glob.glob('*.ovpn')
	print(ovpns)
	i = 0
	for ovpn in ovpns:
		print(i)
		subprocess.call(['sudo', 'screen', '-d', '-m', '-S', ovpn[:-5], 'openvpn','--route-noexec', '--config', ovpn])
		StartTime = time()
		while True:
			CurTime = time()
			if CurTime - StartTime > 7000:
				subprocess.call('sudo', 'screen', '-X', '-S', ovpn[:-5], 'quit')
				break
			elif(len(get_taps()) > i):
				sleep(1)
				ip = netifaces.ifaddresses('tap'+str(i))[2][0]['addr']
				print(ovpn, " - ", ip)
				subprocess.call(['sudo', 'ip', 'rule', 'add', 'from', ip, 'lookup', 'viatap'+str(i)])
				subprocess.call(['sudo', 'ip', 'route', 'add', 'default', 'dev', 'tap'+str(i), 'table', 'viatap'+str(i)])
				i+=1
				break