How to run pycrate corenet - python

I want to run the core simulator of https://github.com/P1sec/pycrate.
And I want to run SERVER_ENB only, so configure like below and create a server.
from pycrate_corenet import Server
Server.CorenetServer.SERVER_HNB = {}
Server.CorenetServer.SERVER_ENB['IP'] = '127.0.0.1'
Server.CorenetServer.SERVER_ENB['GTPU'] = '127.0.0.1'
epc = Server.CorenetServer()
But, I got following error.
$ sudo /usr/local/anaconda3/bin/python EPC.py
CorenetServer: loading all ASN.1 and NAS modules, be patient...
Exception in thread Thread-1:
Traceback (most recent call last):
File "/usr/local/anaconda3/lib/python3.6/threading.py", line 916, in _bootstrap_inner
self.run()
File "/usr/local/anaconda3/lib/python3.6/threading.py", line 864, in run
self._target(*self._args, **self._kwargs)
File "/usr/local/anaconda3/lib/python3.6/site-packages/pycrate-0.3-py3.6.egg/pycrate_corenet/Server.py", line 345, in start
self.GTPUd = self.__class__.GTPUd()
File "/usr/local/anaconda3/lib/python3.6/site-packages/pycrate-0.3-py3.6.egg/pycrate_corenet/ServerGTPU.py", line 466, in __init__
sk.bind((gtpip, self.GTP_PORT))
OSError: [Errno 99] Cannot assign requested address
How can I run the server?
Could you give a usage for this pycrate corenet?

this port is probably occupied by something else (including the other instance on this server).
If you're on Linux - you can check if it's already listen with netstat -anp | grep 36412. Then if something listed there you have to kill the app or change server's port in SERVER_ENB structure

I got a solution.
from pycrate.pycrate_corenet import Server, ServerGTPU
Server.CorenetServer.SERVER_HNB = {}
Server.CorenetServer.SERVER_ENB['IP'] = '127.0.0.1'
Server.CorenetServer.SERVER_ENB['GTPU'] = '127.0.0.1'
ServerGTPU.GTPUd.GTP_IF = ['127.0.0.1'] # set GTP IP
epc = Server.CorenetServer()

Related

Python docker module: How can I create a terminal shell to a docker container in python?

I would like to create a discord/IRC bot to give random people access to a bash console on a server of mine. To make this a bit less insane I decided to use a docker container so hopefully my server doesn't get rm -rf ed. I am unfortunately stuck on getting IO to /bin/bash on the docker container however.
import docker
import time
import asyncio
client = docker.from_env()
c = client.containers.run(
image='disbox:main',
command = "neofetch",
cpu_count = 1,
mem_limit = "1g",
hostname="disbox",
user = "discord",
entrypoint="/bin/bash",
ports = {'80': 8080},
detach = True
)
# wait for container to start
time.sleep(5)
container = client.containers.get(c.id)
while True:
cmd = input("\n:")
res = container.exec_run(cmd, stream=True)
for line in res:
print(line.output)
This gives a Conflict for url error which I am not sure what that means. I have verified it is not already running elsewhere. I am running python with root (otherwise it gives me a perms error).
Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/docker/api/client.py", line 261, in _raise_for_status
response.raise_for_status()
File "/usr/lib/python3/dist-packages/requests/models.py", line 940, in raise_for_status
raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 409 Client Error: Conflict for url: http+docker://localhost/v1.35/containers/f54feee310d0890b751d9544b020279e1ab35e470b98773f4b160b4c0a470d11/exec
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<stdin>", line 3, in <module>
File "/usr/lib/python3/dist-packages/docker/models/containers.py", line 193, in exec_run
resp = self.client.api.exec_create(
File "/usr/lib/python3/dist-packages/docker/utils/decorators.py", line 19, in wrapped
return f(self, resource_id, *args, **kwargs)
File "/usr/lib/python3/dist-packages/docker/api/exec_api.py", line 80, in exec_create
return self._result(res, True)
File "/usr/lib/python3/dist-packages/docker/api/client.py", line 267, in _result
self._raise_for_status(response)
File "/usr/lib/python3/dist-packages/docker/api/client.py", line 263, in _raise_for_status
raise create_api_error_from_http_exception(e)
File "/usr/lib/python3/dist-packages/docker/errors.py", line 31, in create_api_error_from_http_exception
raise cls(e, response=response, explanation=explanation)
docker.errors.APIError: 409 Client Error: Conflict ("Container f54feee310d0890b751d9544b020279e1ab35e470b98773f4b160b4c0a470d11 is not running")
What this code snippet is intended to do is replicate a bash console in the python console. Where did I go wrong?
edit: The image name is 100% correct its a custom ubuntu SE image with neofetch and some other stuff pre-loaded onto it.
If you run a container with an interactive shell as its main process, but don't specify that the container's stdin should stay open, the shell will exit immediately. (This is the same as docker run --entrypoint /bin/bash disbox:main without the -it options.) When you create the container you need to include the relevant options:
c = client.containers.run(
...,
stdin_open=True,
tty=True
)
The container object then has an attach_socket method. This returns a socket-like object that you can send to and recv from; that connects directly to the container's stdin and stdout. In your context you can probably use these to directly relay data back and forth between the client process and the container, knowing that it's line-oriented but not otherwise specifically knowing that it's a shell.
You should not need an "exec" type operation here; you are directly interacting with the container process's stdin and stdout.
(Also remember that there's lots of mischief you can get up to with a shell in a container: launching DoS attacks on the host itself, local privilege-escalation attacks against the host kernel, cryptocurrency miners, etc. You have some protection here from being in a container but it's not "safe" if you have any reason to mistrust your end users.)

An attempt was made to access a socket in a way forbidden by its access permissions (Bottle) (Python)

I've written this code and then ran it via the CMD. However when I ran the code this occurred.
Bottle v0.12.18 server starting up (using WSGIRefServer())...
Listening on http://localhost:80/
Hit Ctrl-C to quit.
Traceback (most recent call last):
File "bottle_01.py", line 7, in <module>
run(host='localhost', port = 80, debug=True)
File "C:\Users\Owner\Desktop\BottleWebApp\bottle.py", line 3137, in run
server.run(app)
File "C:\Users\Owner\Desktop\BottleWebApp\bottle.py", line 2789, in run
srv = make_server(self.host, self.port, app, server_cls, handler_cls)
File "C:\Users\Owner\AppData\Local\Programs\Python\Python38-32\lib\wsgiref\simple_server.py", line 154, in make_server
server = server_class((host, port), handler_class)
File "C:\Users\Owner\AppData\Local\Programs\Python\Python38-32\lib\socketserver.py", line 452, in __init__
self.server_bind()
File "C:\Users\Owner\AppData\Local\Programs\Python\Python38-32\lib\wsgiref\simple_server.py", line 50, in server_bind
HTTPServer.server_bind(self)
File "C:\Users\Owner\AppData\Local\Programs\Python\Python38-32\lib\http\server.py", line 138, in server_bind
socketserver.TCPServer.server_bind(self)
File "C:\Users\Owner\AppData\Local\Programs\Python\Python38-32\lib\socketserver.py", line 466, in server_bind
self.socket.bind(self.server_address)
OSError: [WinError 10013] An attempt was made to access a socket in a way forbidden by its access permissions
Now I asked my professor but he's unfamiliar with the issue himself. I searched stackoverflow and I tried to disable my firewall but that didn't seem to help. Any suggestions? Is it an administration issue perhaps? My original code is below.
from bottle import route, run, static_file
#route('/')
def index():
return static_file('webpage_01.html' , root= 'C:/Users/Owner/Desktop/BottleWebApp')
run(host='localhost', port = 80, debug=True)
Does this problem go away when you try port 8000 instead of 80?
You're trying to bind to port 80. I don't know Windows, but on Linux your code would fail because port 80 (like all ports below 1024) is privileged--only root can bind to them. That's why you'll see most tutorials and web framework defaults use a high port number, typically 8000 or 8080.
References:
https://www.w3.org/Daemon/User/Installation/PrivilegedPorts.html
https://en.wikipedia.org/wiki/Registered_port

Fabric's connection.forward_local fails when going out of scope

I'm trying to get a python script to enable port forwarding from a remote host to the local machine to access an interface.
If I do it manually with
ssh -L 54321:someotherhost:80 user#host (with password prompt) this works just fine and I can access the interface on http://localhost:54321/someinterface just as expected.
Now I'm trying to do it with fabric:
from fabric import Connection
HOST = "somehost"
USER = "someuser"
PASSWORD = "somepassword"
LOCAL_PORT = "54321"
REMOTE_PORT = "80"
REMOTE_HOST = "someotherhost"
kwargs = {
"password": PASSWORD
}
with Connection(HOST, user=USER, connect_kwargs=kwargs).forward_local(
LOCAL_PORT, REMOTE_PORT, REMOTE_HOST, "localhost"
):
pass # access interface e.g. via the requests package
However, 1.) the forwarding doesn't seem to work, reasons unknown and 2.) when the last line within the scope of forward_local is executed, it stops with the following error:
Traceback (most recent call last):
File ".\path\to\script.py", line 67, in <module>
main()
File ".\path\to\script.py", line 35, in main
pass
File "C:\Users\ott\AppData\Local\Programs\Python\Python37\lib\contextlib.py", line 119, in __exit__
next(self.gen)
File "C:\Users\ott\AppData\Local\Programs\Python\Python37\lib\site-packages\fabric\connection.py", line 883, in forward_local
raise ThreadException([wrapper])
invoke.exceptions.ThreadException:
Saw 1 exceptions within threads (TypeError):
Thread args: {}
Traceback (most recent call last):
File "C:\Users\ott\AppData\Local\Programs\Python\Python37\lib\site-packages\invoke\util.py", line 231, in run
self._run()
File "C:\Users\ott\AppData\Local\Programs\Python\Python37\lib\site-packages\fabric\tunnels.py", line 60, in _run
sock.bind(self.local_address)
TypeError: an integer is required (got type str)
It's possible 1.) and 2.) are related, but I'm focusing on 2.) right now. It doesn't matter what I do within the scope of the context manager produced by forward_local, on the last executed statement it stops. I presume this is caused by something when the context manager is closed by python when the interpreter leaves the scope.
According to the documentation parameters like:
local_port
remote_port
have to be integers not string. That's why you got:
TypeError: an integer is required (got type str)
So, changing variables:
LOCAL_PORT = "54321"
REMOTE_PORT = "80"
to
LOCAL_PORT = 54321
REMOTE_PORT = 80
should fix the problem.

Flask, cannot assign requested address [duplicate]

This question already has answers here:
socket.error:[errno 99] cannot assign requested address and namespace in python
(6 answers)
Closed 8 years ago.
I'm trying to run a flask app on a remote server, so I can access it from other computers. The server has a public IP and I configured the flask to run on that IP. But when I run the script I get the following traceback
Note: I've removed the public IP from the traceback and my code.
* Running on **public ip**
Traceback (most recent call last):
File "testServer.py", line 14, in <module>
app.run(host='62.60.19.189',port=5000)
File "/usr/lib/python2.6/site-packages/flask/app.py", line 772, in run
run_simple(host, port, self, **options)
File "/usr/lib/python2.6/site-packages/werkzeug/serving.py", line 710, in run_simple
inner()
File "/usr/lib/python2.6/site-packages/werkzeug/serving.py", line 692, in inner
passthrough_errors, ssl_context).serve_forever()
File "/usr/lib/python2.6/site-packages/werkzeug/serving.py", line 486, in make_server
passthrough_errors, ssl_context)
File "/usr/lib/python2.6/site-packages/werkzeug/serving.py", line 410, in __init__
HTTPServer.__init__(self, (host, int(port)), handler)
File "/usr/lib64/python2.6/SocketServer.py", line 402, in __init__
self.server_bind()
File "/usr/lib64/python2.6/BaseHTTPServer.py", line 108, in server_bind
SocketServer.TCPServer.server_bind(self)
File "/usr/lib64/python2.6/SocketServer.py", line 413, in server_bind
self.socket.bind(self.server_address)
File "<string>", line 1, in bind
socket.error: [Errno 99] Cannot assign requested address
Here is my code
import flask
app = flask.Flask("My app")
#app.route('/myroute', methods=['POST'])
def foobar():
print flask.request.form
return '<br>'.join('{0}: {1}'.format(*pair) for pair in flask.request.form.items())
if __name__ == '__main__':
app.run(host='public IP',port=5000)
You can only directly bind to an IP address that the server has been configured for; behind a router running Network Address Translation (NAT) your internal IP address will be different.
Either bind directly to that internal IP address, or use '0.0.0.0' to listen on all interfaces. You may still need to configure the router to forward a specific port to your internal server.
The IP you want to bind a socket to must be directly available on an interface of the machine. This seems not be the case here.
If you're behind a NAT: use port-forwarding
If you're using a VPN and the VPN adapter of the server is not always up, try using "0.0.0.0" as an address. Beware: It will listen on all interfaces available. Create firewall rules to block access via interfaces you don't want to listen to when using this.

Why wont Web.py let me run a server on port 80?

Im trying to create a website with Web.py but its not letting me open a create a socket on port 80 but it works on every other port.
I have port forwarded and all that so that's not the problem.
python main.py 80
but when I do this I get the error:
http://0.0.0.0:80/
Traceback (most recent call last):
File "main.py", line 43, in <module>
app.run()
File "/usr/local/lib/python2.7/dist-packages/web/application.py", line 311, in run
return wsgi.runwsgi(self.wsgifunc(*middleware))
File "/usr/local/lib/python2.7/dist-packages/web/wsgi.py", line 54, in runwsgi
return httpserver.runsimple(func, validip(listget(sys.argv, 1, '')))
File "/usr/local/lib/python2.7/dist-packages/web/httpserver.py", line 148, in runsimple
server.start()
File "/usr/local/lib/python2.7/dist-packages/web/wsgiserver/__init__.py", line 1753, in start
raise socket.error(msg)
socket.error: No socket could be created
my code so far is:
import MySQLdb
import web
import hashlib as h
urls = (
'/', "index", "/register/?", "register", "/login/?", "login", "/thankyou/?", "thankyou"
)
app = web.application(urls, globals())
render = web.template.render("templates/")
db = web.database (dbn="mysql", user="root", pw="461408", db="realmd")
class index():
def GET(self):
return render.index()
class register():
def GET(self):
return render.register()
def POST(self):
i = web.input()
user = h.sha1(i.username).hexdigest()
pw = h.sha1(i.password).hexdigest()
n = db.insert("account", username=user, password=pw)
if __name__ == '__main__':
app.run()
Can someone help please?
You possibly have something else working on port 80. Try the command netstat -ln | grep 80 to check that.
Alternatively, you can try telnet localhost 80, and if the connection is refused then that port should be clear to use.
I successfully start the service by using this command in port 80
sudo python index.py 80
but when I use the shortcut key (control+c) to close the service,there will be an error.
^CTraceback (most recent call last):
File "application.py", line 206, in <module>
app.run()
File "/Library/Python/2.7/site-packages/web.py-0.37-py2.7.egg/web/application.py", line 313, in run
return wsgi.runwsgi(self.wsgifunc(*middleware))
File "/Library/Python/2.7/site-packages/web.py-0.37-py2.7.egg/web/wsgi.py", line 54, in runwsgi
return httpserver.runsimple(func, validip(listget(sys.argv, 1, '')))
File "/Library/Python/2.7/site-packages/web.py-0.37-py2.7.egg/web/httpserver.py", line 159, in runsimple
server.stop()
File "/Library/Python/2.7/site-packages/web.py-0.37-py2.7.egg/web/wsgiserver/__init__.py", line 1932, in stop
self.requests.stop(self.shutdown_timeout)
File "/Library/Python/2.7/site-packages/web.py-0.37-py2.7.egg/web/wsgiserver/__init__.py", line 1471, in stop
worker.join(remaining_time)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/threading.py", line 680, in join
self.__block.release()
thread.error: release unlocked lock
^C^CException KeyboardInterrupt in <module 'threading' from '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/threading.pyc'> ignored
When it happens, I Kill All Python Processes...
killall -9 python
it's can solve the above problems, but not recommended
Visit 127.0.0.1 in a browser. There is likely already a process using port 80, and that port is supposed to be used for http, so that's probably the easiest way to see what's using it.
I ran into the same problem on my RaspberryPi. To fix I just added sudo before the command. Try: sudo python main.py 80
Could it be the fact you're trying to launch web.py as an unprivileged user?
try:
sudo python ./bin/blah.py

Categories

Resources