Coder Social home page Coder Social logo

python-nsenter's Introduction

NSEnter

Travis CI build status

NSEnter is a Python package that enables you to enter Linux kernel namespaces — mount, IPC, net, PID, user and UTS — with a single, simple "setns" syscall. The command line interface is similar to the nsenter C program.

Project Origins

When working with Docker containers, questions usually arise about how to connect into a running container without starting an explicit SSH daemon (which is considered a bad idea). One way is to use Linux Kernel namespaces, which Docker uses to restrict the view from within containers.

The util-linux package provides the nsenter command line utility, but Ubuntu 16.04 LTS unfortunately does not. Jérôme Petazzoni provides a Docker recipe for nsenter on GitHub, or you can compile nsenter from source. As there is only one simple syscall to enter a namespace, we can do the call directly from within Python using the ctypes module. We bundled this syscall to create NSEnter.

  • "Entering Kernel Namespaces from Python," Zalando Tech blog post
  • On PyPi

Requirements

  • Python 2.6 or higher

Installation

From PyPI:

sudo pip3 install nsenter

From git source:

python3 setup.py install

Usage

Example of command line usage:

docker run -d --name=redis -t redis
sudo nsenter --all --target=`docker inspect --format '{{ .State.Pid }}' redis` /bin/bash

Example of usage from Python:

import subprocess
from nsenter import Namespace

with Namespace(mypid, 'net'):
    # output network interfaces as seen from within the mypid's net NS:
    subprocess.check_output(['ip', 'a'])

# or enter an arbitrary namespace:
with Namespace('/var/run/netns/foo', 'net'):
    # output network interfaces as seen from within the net NS "foo":
    subprocess.check_output(['ip', 'a'])

Development Status

This project works as-is. There are currently no plans to extend it, but if you have an idea please submit an Issue to the maintainers.

License

See file.

python-nsenter's People

Contributors

cnelson avatar dalazx avatar hjacobs avatar lappleapple avatar perploug avatar pheanex avatar tanmaykm avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

python-nsenter's Issues

The latest version does not published at pypi

Hi, I tried to use this lib which is published at pypi, but the latest version at pypi is 0.2.
I want this commit to load this lib on Mac.
I workaround by using it from github directly, but I want to freeze the version.
Can someone publish it, please?

No DNS inside namespace

I successfully used this lib for entering my net namespace (for example - myns0), but there is no DNS at all inside it.
Of course I have a valid resolv.conf in /etc/netns/myns0/ and it works well when I call commands in shell like "ip netns exec myns0 ping google.com".

Version on pypi fails to install on python 3.10

$ docker run -it python:3.10 pip install nsenter
Collecting nsenter
  Downloading nsenter-0.2-py3-none-any.whl (12 kB)
Collecting contextlib2
  Downloading contextlib2-21.6.0-py2.py3-none-any.whl (13 kB)
Collecting argparse
  Downloading argparse-1.4.0-py2.py3-none-any.whl (23 kB)
Collecting pathlib
  Downloading pathlib-1.0.1.tar.gz (49 kB)
     |████████████████████████████████| 49 kB 2.6 MB/s 
    ERROR: Command errored out with exit status 1:
     command: /usr/local/bin/python -c 'import io, os, sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-quttjgpr/pathlib_d60a79e90d4a49f48ae63d6cdcbc2493/setup.py'"'"'; __file__='"'"'/tmp/pip-install-quttjgpr/pathlib_d60a79e90d4a49f48ae63d6cdcbc2493/setup.py'"'"';f = getattr(tokenize, '"'"'open'"'"', open)(__file__) if os.path.exists(__file__) else io.StringIO('"'"'from setuptools import setup; setup()'"'"');code = f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' egg_info --egg-base /tmp/pip-pip-egg-info-zlfypxgz
         cwd: /tmp/pip-install-quttjgpr/pathlib_d60a79e90d4a49f48ae63d6cdcbc2493/
    Complete output (13 lines):
    Traceback (most recent call last):
      File "<string>", line 1, in <module>
      File "/usr/local/lib/python3.10/site-packages/setuptools/__init__.py", line 16, in <module>
        import setuptools.version
      File "/usr/local/lib/python3.10/site-packages/setuptools/version.py", line 1, in <module>
        import pkg_resources
      File "/usr/local/lib/python3.10/site-packages/pkg_resources/__init__.py", line 23, in <module>
        import zipfile
      File "/usr/local/lib/python3.10/zipfile.py", line 19, in <module>
        import pathlib
      File "/tmp/pip-install-quttjgpr/pathlib_d60a79e90d4a49f48ae63d6cdcbc2493/pathlib.py", line 10, in <module>
        from collections import Sequence
    ImportError: cannot import name 'Sequence' from 'collections' (/usr/local/lib/python3.10/collections/__init__.py)
    ----------------------------------------
WARNING: Discarding https://files.pythonhosted.org/packages/ac/aa/9b065a76b9af472437a0059f77e8f962fe350438b927cb80184c32f075eb/pathlib-1.0.1.tar.gz#sha256=6940718dfc3eff4258203ad5021090933e5c04707d5ca8cc9e73c94a7894ea9f (from https://pypi.org/simple/pathlib/). Command errored out with exit status 1: python setup.py egg_info Check the logs for full command output.

This is caused by the dependency on pathlib, this was fixed by commit b7fd78f but a new pypi version was not published.

Add documentation

Add more documentation to the project please.
E.g. how could I implement the following command into python using the nsenter module?:
nsenter -t 1 --wd=/usr/local/bin -m -- docker ps

Move requirements inside setup.py gated by version

argparse is not really needed except in older version of Python. How do you feel about moving them into setup.py and gating them for < 2.7 and < 3.3 ?

Context: @ Facebook we build modules into our own wheels and auto generate dependencies. Your modules, even with Python 3.6 today insists on these modules so our automation co-operates.

Many thanks for the module!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.