Source From Here
Preface
what are network namespaces? Generally speaking, an installation of Linux shares a single set of network interfaces and routing table entries. You can modify the routing table entries using policy routing (here’s an introduction I wrote and here’s a write-up on a potential use case for policy routing), but that doesn’t fundamentally change the fact that the set of network interfaces and routing tables/entries are shared across the entire OS. Network namespaces change that fundamental assumption. With network namespaces, you can have different and separate instances of network interfaces and routing tables that operate independent of each other.
This concept is probably best illustrated through some examples. Along the way, I’ll introduce a few new ideas as well. First, though, I need to provide some assumptions.
Creating and Listing Network Namespaces
Creating/Deleting a network namespace is actually quite easy. Just use this command:
For example, let’s say you wanted to create a namespace called “blue”. You’d use this command:
You should see your network namespace listed there, ready for you to use.
Assigning Interfaces to Network Namespaces
Creating the network namespace is only the beginning; the next part is to assign interfaces to the namespaces, and then configure those interfaces for network connectivity. One thing that threw me off early in my exploration of network namespaces was that you couldn’t assign physical interfaces to a namespace. How in the world were you supposed to use them, then?
It turns out you can only assign virtual Ethernet (veth) interfaces to a network namespace. Virtual Ethernet interfaces are an interesting construct; they always come in pairs, and they are connected like a tube—whatever comes in one veth interface will come out the other peer veth interface. As a result, you can use veth interfaces to connect a network namespace to the outside world via the “default” or “global” namespace where physical interfaces exist.
Let’s see how that’s done. First, you’d create the veth pair:
Naturally, you could substitute other names for veth0 and veth1, if you wanted. You can verify that the veth pair was created using this command:
You should see a pair of veth interfaces (using the names you assigned in the command above) listed there. Right now, they both belong to the “default” or “global” namespace, along with the physical interfaces.
Let’s say that you want to connect the global namespace to the blue namespace. To do that, you’ll need to move one of the veth interfaces to the bluenamespace using this command:
If you then run the ip link list command again, you’ll see that the veth1 interface has disappeared from the list. It’s now in the blue namespace, so to see it you’d need to run this command:
Whoa! That’s a bit of a complicated command. Let’s break it down:
Configuring Interfaces in Network Namespaces
Now that veth1 has been moved to the blue namespace, we need to actually configure that interface. Once again, we’ll use the ip netns exec command, this time to configure the veth1 interface in the blue namespace:
In this case, you’re using ifconfig to assign an IP address to the veth1 interface and bring that interface up. (Note: you could use the ip addr, ip route, and ip linkcommands to accomplish the same thing.)
Once the veth1 interface is up, you can verify that the network configuration of the blue namespace is completely separate by just using a few different commands. For example, let’s assume that your “global” namespace has physical interfaces in the 172.16.1.0/24 range, and your veth1 interface is in a separate namespace and assigned something from the 10.1.1.0/24 range. You could verify how network namespaces keep the network configuration separate using these commands:
Connecting Network Namespaces to the Physical Network
This part of it threw me for a while. I can’t really explain why, but it did. Once I’d figured it out, it was obvious. To connect a network namespace to the physical network, just use a bridge. In my case, I used an Open vSwitch (OVS) bridge, but a standard Linux bridge would work as well. Place one or more physical interfaces as well as one of the veth interfaces in the bridge, and—bam!—there you go. Naturally, if you had different namespaces, you’d probably want/need to connect them to different physical networks or different VLANs on the physical network.
This is a blog to track what I had learned and share knowledge with all who can take advantage of them
標籤
- [ 英文學習 ]
- [ 計算機概論 ]
- [ 深入雲計算 ]
- [ 雜七雜八 ]
- [ Algorithm in Java ]
- [ Data Structures with Java ]
- [ IR Class ]
- [ Java 文章收集 ]
- [ Java 代碼範本 ]
- [ Java 套件 ]
- [ JVM 應用 ]
- [ LFD Note ]
- [ MangoDB ]
- [ Math CC ]
- [ MongoDB ]
- [ MySQL 小學堂 ]
- [ Python 考題 ]
- [ Python 常見問題 ]
- [ Python 範例代碼 ]
- [心得扎記]
- [網路教學]
- [C 常見考題]
- [C 範例代碼]
- [C/C++ 範例代碼]
- [Intro Alg]
- [Java 代碼範本]
- [Java 套件]
- [Linux 小技巧]
- [Linux 小學堂]
- [Linux 命令]
- [ML In Action]
- [ML]
- [MLP]
- [Postgres]
- [Python 學習筆記]
- [Quick Python]
- [Software Engineering]
- [The python tutorial]
- 工具收集
- 設計模式
- 資料結構
- ActiveMQ In Action
- AI
- Algorithm
- Android
- Ansible
- AWS
- Big Data 研究
- C/C++
- C++
- CCDH
- CI/CD
- Coursera
- Database
- DB
- Design Pattern
- Device Driver Programming
- Docker
- Docker 工具
- Docker Practice
- Eclipse
- English Writing
- ExtJS 3.x
- FP
- Fraud Prevention
- FreeBSD
- GCC
- Git
- Git Pro
- GNU
- Golang
- Gradle
- Groovy
- Hadoop
- Hadoop. Hadoop Ecosystem
- Java
- Java Framework
- Java UI
- JavaIDE
- JavaScript
- Jenkins
- JFreeChart
- Kaggle
- Kali/Metasploit
- Keras
- KVM
- Learn Spark
- LeetCode
- Linux
- Lucene
- Math
- ML
- ML Udemy
- Mockito
- MPI
- Nachos
- Network
- NLP
- node js
- OO
- OpenCL
- OpenMP
- OSC
- OSGi
- Pandas
- Perl
- PostgreSQL
- Py DS
- Python
- Python 自製工具
- Python Std Library
- Python tools
- QEMU
- R
- Real Python
- RIA
- RTC
- Ruby
- Ruby Packages
- Scala
- ScalaIA
- SQLAlchemy
- TensorFlow
- Tools
- UML
- Unix
- Verilog
- Vmware
- Windows 技巧
- wxPython
2015年6月29日 星期一
2015年6月28日 星期日
[ 常見問題 ] Get docker container name by PID
Source From Here
Question
I have list of PID's and i need to get their docker container name going the other direction is easy ... Get PID of docker container by image name:
Any idea how to get the name by PID ?
How-To
Supplement
* Docker Doc - Using the command line
* "xargs" All-IN-One Tutorial Guide
Question
I have list of PID's and i need to get their docker container name going the other direction is easy ... Get PID of docker container by image name:
# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
417af6d7dbfa 52366bfd2985:latest "sh /home/root/scrip 19 minutes ago Up 19 minutes 4444/tcp, 5999/tcp insane_ardinghelli
# docker inspect --format '{{.State.Pid}}' insane_ardinghelli
64998
Any idea how to get the name by PID ?
How-To
// -q, --quiet=false Only display numeric IDs
# docker ps -q
417af6d7dbfa
# docker ps -q | xargs docker inspect --format '{{.Name}}'
/insane_ardinghelli
Supplement
* Docker Doc - Using the command line
* "xargs" All-IN-One Tutorial Guide
[Linux 文章收集] "xargs" All-IN-One Tutorial Guide
Source From Here
Preface
xargs is a just like "awk" ,"find" & "grep" commands processes the standard input on all unix flavoured operating sysems. Basically "xargs" is used to remove or do some operation on long list of file names which were produced by "find" & "grep" commands.
Usually many UNIX operating system doesn't accept such a long list of argument.UNIX xargs command divide that list into sub-list with acceptable length and made it work. For example I'd like to find out all *.sh file located in 100s of sub-directories and move them to another directory called ~/back.scripts. How do I use command line args with xargs to achieve the same?
as per man page "xargs" is used to execute a command, passing constructed argument list(s). The arguments are typically a long list of filenames (generated byls or find etc) that are passed to xargs via a pipe.
Some features:
Exit Status
This command returns the following exit values:
Examples
Find all the .conf files under /etc/ and pass to the ls command, -print0 is required if any filenames contain whitespace.:
Find all files in the work folder, pass to grep and search for 'profit':
{} as the argument list marker
{} is the default argument list marker. You need to use {} this with various command which take more than two arguments at a time. For example mv command need to know the file name. The following will find all .sh files in or below the current directory and move them to ~/.old.files directory:
You can rename {} to something else. In the following example {} is renamed as file. This is more readable as compare to previous example:
Where,
10 Popular "XARGS" Command Examples:
1) With& Without "xargs" observation:
In this example of xargs command we will see how output changes with use of xargs command in unix or Linux. Here is the output of find command withoutxargs first and than with xargs, you can clearly see that multiline output is converted into single line:
2) Xargs with grep:
When you use "xargs" in conjusction with find and grep , the grep will look for the specific word in each file in the from the standard input.
In the above example first find all .sh files from current directory or below and than on each .sh file look for word "ksh".
3) Covert muti-line output into single line
Best example of xargs is converting output of one command into one line. For example you can run any command and then combine xargs to convert output into single line. here is an example xargs in unix which does that.
4) To Delete temporary files using xargs & find:
Another common example of xargs command in unix is removing temporary files from system.
This will remove all .tmp file from /tmp or below directory. xargs in unix is very fast as compared to deleting single file at a time which can also be done by usingfind command alone.
5) xargs -0 to handle space in file name
Above example of xargs command in unix will not work as expected if any of file name contains space or new line on it. To avoid this problem we use find -print0to produce null separated file name and xargs -0 to handle null separated items. Here is an example of xargs command in unix which can handle file name with spaces and newline:
6) Counting number of lines/words/characters in each file using xargs & find:
"find" in conjuction with "xargs" and "wc" we can count number of lines/words/characters in each file under a particular directory.
Note: you can use '-c' & '-w' with wc to obtain number of characters and words respectively.
7) xargs and cut command in Unix:
Though most of xargs examples in unix will be along with find and grep command but xargs is not just limited to this two it can also be used with any command which generated long list of input for example we can use xargs with cut command in unix. In below example of unix xargs we will xargs example with cutcommand. for using cut command let's first create a test file with some data e.g.
8) To insert file names into the middle of command lines, type:
This command sequence renames all files in the current directory by adding .old to the end of each name. The -I flag tells the xargs command to insert each line of the ls directory listing where {} (braces) appear. If the current directory contains the files chap1, chap2, and chap3, this constructs the following commands:
// -t, --verbose: Print the command line on the standard error output before executing it.
9) To run a command on files that you select individually, type:
This command sequence allows you to select files to add to the lib.a library. The -p flag tells the xargs command to display each ar command it constructs and to ask if you want to run it. Type y to run the command. Press the any other key if you do not want to run the command.
10) To construct a command that contains a specific number of arguments and to insert those arguments into the middle of a command line, type:
Supplement
* 10 Xargs Command Examples in Linux / UNIX
Preface
xargs is a just like "awk" ,"find" & "grep" commands processes the standard input on all unix flavoured operating sysems. Basically "xargs" is used to remove or do some operation on long list of file names which were produced by "find" & "grep" commands.
Usually many UNIX operating system doesn't accept such a long list of argument.UNIX xargs command divide that list into sub-list with acceptable length and made it work. For example I'd like to find out all *.sh file located in 100s of sub-directories and move them to another directory called ~/back.scripts. How do I use command line args with xargs to achieve the same?
as per man page "xargs" is used to execute a command, passing constructed argument list(s). The arguments are typically a long list of filenames (generated byls or find etc) that are passed to xargs via a pipe.
Some features:
* xargs can execute the command supplying some initial arguments directly, and reading the remaining arguments from standard input (or piped input).
* xargs passes arguments to command in several bundles, this allows command to process more arguments than it could normally handle at once.
* Arguments in the standard input must be separated by unquoted blank characters, or unescaped blank characters or newline characters.
* Characters can be quoted by enclosing them in "double-quotes" (non-double-quote and non-newline chars only).
* Characters can be quoted by enclosing them in 'apostrophes' (non-apostrophe and non-newline chars only).
* Any unquoted character can be escaped by preceding it with a backslash.
Exit Status
This command returns the following exit values:
Examples
Find all the .conf files under /etc/ and pass to the ls command, -print0 is required if any filenames contain whitespace.:
# find /etc/ -name "*.conf" -print0 | xargs -0 ls -l
Find all files in the work folder, pass to grep and search for 'profit':
# find ./work -print | xargs grep "profit"
{} as the argument list marker
{} is the default argument list marker. You need to use {} this with various command which take more than two arguments at a time. For example mv command need to know the file name. The following will find all .sh files in or below the current directory and move them to ~/.old.files directory:
# find . -name "*.sh" -print0 | xargs -0 -I {} mv {} ~/back.scripts
You can rename {} to something else. In the following example {} is renamed as file. This is more readable as compare to previous example:
# find . -name "*.sh" -print0 | xargs -0 -I file mv file ~/back.scripts
Where,
-0 If there are blank spaces or characters (including newlines) many commands will not work. This option take cares of file names with blank space.
-I Replace occurrences of replace-str in the initial-arguments with names read from standard input. Also, unquoted blanks do not terminate input items; instead the separator is the newline character.
10 Popular "XARGS" Command Examples:
1) With& Without "xargs" observation:
In this example of xargs command we will see how output changes with use of xargs command in unix or Linux. Here is the output of find command withoutxargs first and than with xargs, you can clearly see that multiline output is converted into single line:
# find . -name "*sh*"
./.bash_history
./.bash_profile
./.bash_profile.cf-before-edit
./.cshrc
...
# find . -name "*bash*" | xargs
./.bash_history ./.bash_profile ./.bash_profile.cf-before-edit ...
2) Xargs with grep:
When you use "xargs" in conjusction with find and grep , the grep will look for the specific word in each file in the from the standard input.
# find . -name "*.sh" | xargs grep "ksh"
In the above example first find all .sh files from current directory or below and than on each .sh file look for word "ksh".
3) Covert muti-line output into single line
Best example of xargs is converting output of one command into one line. For example you can run any command and then combine xargs to convert output into single line. here is an example xargs in unix which does that.
# ls -1 *.sh
linux_sysinfo.sh
aix_sysinfo.sh
audit_script.sh
chperm_messages.sh
# ls -1 *.sh | xargs
linux_sysinfo.sh aix_sysinfo.sh audit_script.sh chperm_messages.sh
4) To Delete temporary files using xargs & find:
Another common example of xargs command in unix is removing temporary files from system.
# find /tmp -name "*.tmp" | xargs rm
This will remove all .tmp file from /tmp or below directory. xargs in unix is very fast as compared to deleting single file at a time which can also be done by usingfind command alone.
5) xargs -0 to handle space in file name
Above example of xargs command in unix will not work as expected if any of file name contains space or new line on it. To avoid this problem we use find -print0to produce null separated file name and xargs -0 to handle null separated items. Here is an example of xargs command in unix which can handle file name with spaces and newline:
# find /tmp -name "*.tmp" -print0 | xargs -0 rm
6) Counting number of lines/words/characters in each file using xargs & find:
"find" in conjuction with "xargs" and "wc" we can count number of lines/words/characters in each file under a particular directory.
# ls -1 *.sh | xargs wc -l
112 linux_sysinfo.sh
85 aix_sysinfo.sh
35 audit_script.sh
18 chperm_messages.sh
250 total
Note: you can use '-c' & '-w' with wc to obtain number of characters and words respectively.
7) xargs and cut command in Unix:
Though most of xargs examples in unix will be along with find and grep command but xargs is not just limited to this two it can also be used with any command which generated long list of input for example we can use xargs with cut command in unix. In below example of unix xargs we will xargs example with cutcommand. for using cut command let's first create a test file with some data e.g.
# cat fruits.txt
Orange,Greenorange
Mango,Redmango
Banana,Pinkbanana
// Now we will display name of actual fruit from first column using xargs command in one line:
# cut -d, -f1 fruits.txt | sort | xargs
Banana Mango Orange
8) To insert file names into the middle of command lines, type:
This command sequence renames all files in the current directory by adding .old to the end of each name. The -I flag tells the xargs command to insert each line of the ls directory listing where {} (braces) appear. If the current directory contains the files chap1, chap2, and chap3, this constructs the following commands:
// -t, --verbose: Print the command line on the standard error output before executing it.
# ls | xargs -t -I {} mv {} {}.old
# mv chap1 chap1.old
# mv chap2 chap2.old
# mv chap3 chap3.old
9) To run a command on files that you select individually, type:
This command sequence allows you to select files to add to the lib.a library. The -p flag tells the xargs command to display each ar command it constructs and to ask if you want to run it. Type y to run the command. Press the any other key if you do not want to run the command.
// -p, --interactive: Prompt the user about whether to run each command line and read a line from the terminal.
// -n max-args, --max-args=max-args: Use at most max-args arguments per command line.
# ls | xargs -p -n 1 ar r lib.a
ar r lib.a chap1 ?...
ar r lib.a chap2 ?...
ar r lib.a chap3 ?...
10) To construct a command that contains a specific number of arguments and to insert those arguments into the middle of a command line, type:
// If the current directory contains files chap1 through chap10, the output constructed will be the following
# ls | xargs -n6 | xargs -I {} echo {} - some files in the directory
chap1 chap2 chap3 chap4 chap5 chap6 - some files in the directory
chap7 chap8 chap9 chap10 - some file in the directory
Supplement
* 10 Xargs Command Examples in Linux / UNIX
This tutorials explains the usage of xargs command using few simple examples...
2015年6月26日 星期五
[ 常見問題 ] Docker - Stop / remove all Docker containers
Source From Here
One liner to stop / remove all of Docker containers:
One liner to stop / remove all of Docker containers:
// -a, --all=false Show all containers (default shows just running)
// -q, --quiet=false Only display numeric IDs
# echo $(docker ps -a -q)
6ca1c890e3f2 607d3ad2e4c8 67430c23a16d cc86b1ba697c 9ff17926b736 f12b657aa412 ef217701a1b7 33f15b0c22f2 1fb091625c6d
# docker stop $(docker ps -a -q) // Stop all containers
# docker rm $(docker ps -a -q) // Remove all containers
2015年6月25日 星期四
[ Python 常見問題 ] Python speed testing - Time Difference - milliseconds
Source From Here
Question
What is the proper way to compare 2 times in Python in order to speed test a section of code? I tried reading the API docs. I'm not sure I understand thetimedelta thing. So far I have this code:
How-To
datetime.timedelta is just the difference between two datetimes ... so it's like a period of time, in days / seconds / microseconds:
Be aware that c.microseconds only returns the microseconds portion of the timedelta! For timing purposes always use c.total_seconds(). You can do all sorts of maths with datetime.timedelta, eg:
It might be more useful to look at CPU time instead of wallclock time though ... that's operating system dependant though ... under Unix-like systems, check out the 'time' command.
Question
What is the proper way to compare 2 times in Python in order to speed test a section of code? I tried reading the API docs. I'm not sure I understand thetimedelta thing. So far I have this code:
- from datetime import datetime
- tstart = datetime.now()
- print t1
- # code to speed test
- tend = datetime.now()
- print t2
- # what am I missing?
- # I'd like to print the time diff here
datetime.timedelta is just the difference between two datetimes ... so it's like a period of time, in days / seconds / microseconds:
>>> import datetime
>>> a = datetime.datetime.now()
>>> b = datetime.datetime.now()
>>> c = b - a
>>> c
datetime.timedelta(0, 4, 316543)
>>> c.days
0
>>> c.seconds
4
>>> c.microseconds
316543
Be aware that c.microseconds only returns the microseconds portion of the timedelta! For timing purposes always use c.total_seconds(). You can do all sorts of maths with datetime.timedelta, eg:
>>> c / 10
datetime.timedelta(0, 0, 431654)
It might be more useful to look at CPU time instead of wallclock time though ... that's operating system dependant though ... under Unix-like systems, check out the 'time' command.
2015年6月24日 星期三
[ Python 常見問題 ] Python list function argument names
Source From Here
Question
Is there a way to get the argument names a function takes? For example:
I can use magical_way this way:
How-To
Use the inspect method from Python's standard library (the cleanest, most solid way to perform introspection).
inspect.getargspec(f) returns the names and default values of f's arguments:
A testing example:
If you only want the names and don't care about special forms *a, **k,
Supplement
* Getting method parameter names in python 5 answers
* Getting list of parameter names inside python function 4 answers
* Python *args and **kwargs?
Question
Is there a way to get the argument names a function takes? For example:
- def foo(bar, buz):
- pass
- magical_way(foo) == ["bar", "buz"]
Use the inspect method from Python's standard library (the cleanest, most solid way to perform introspection).
inspect.getargspec(f) returns the names and default values of f's arguments:
Get the names and default values of a Python function’s arguments. A tuple of four things is returned: (args, varargs, keywords, defaults). args is a list of the argument names (it may contain nested lists). varargs and keywords are the names of the * and ** arguments or None. defaults is a tuple of default argument values or None if there are no default arguments; if this tuple has n elements, they correspond to the last n elements listed in args.
A testing example:
>>> import inspect
>>> def f(a, b='b', c=3, *args, **kwards):
... pass
...
>>> inspect.getargspec(f)
ArgSpec(args=['a', 'b', 'c'], varargs='args', keywords='kwards', defaults=('b', 3))
>>> inspect.getargspec(f)[0] # If you are only interested in the defined argument names
['a', 'b', 'c']
If you only want the names and don't care about special forms *a, **k,
- import inspect
- ef magical_way(f):
- return inspect.getargspec(f)[0]
Supplement
* Getting method parameter names in python 5 answers
* Getting list of parameter names inside python function 4 answers
* Python *args and **kwargs?
訂閱:
文章 (Atom)
[Git 常見問題] error: The following untracked working tree files would be overwritten by merge
Source From Here 方案1: // x -----删除忽略文件已经对 git 来说不识别的文件 // d -----删除未被添加到 git 的路径中的文件 // f -----强制运行 # git clean -d -fx 方案2: 今天在服务器上 gi...
-
前言 : 為什麼程序管理這麼重要呢?這是因為: * 首先,本章一開始就談到的,我們在操作系統時的各項工作其實都是經過某個 PID 來達成的 (包括你的 bash 環境), 因此,能不能進行某項工作,就與該程序的權限有關了。 * 再來,如果您的 Linux 系統是個...
-
屬性 : 系統相關 - 檔案與目錄 語法 : du [參數] [檔案] 參數 | 功能 -a | 顯示目錄中個別檔案的大小 -b | 以bytes為單位顯示 -c | 顯示個別檔案大小與總和 -D | 顯示符號鏈結的來源檔大小 -h | Hum...
-
來源自 這裡 說明 : split 是 Perl 中非常有用的函式之一,它可以將一個字串分割並將之置於陣列中。若無特別的指定,該函式亦使用 RE 與 $_ 變數 語法 : * split /PATTERN/,EXPR,LIMIT * split /...