Share the script for verifying IP address legality implemented by python and shell, pythonshell
I. Check IP address validity in python
Execution result:
Python code:
Copy codeThe Code is as follows:
[Root @ yang python] # vi check_ip.py
#! /Usr/bin/python
Import OS, sys
Def check_ip (ipaddr ):
Import sys
Addr = ipaddr. strip (). split ('.') # Cut the IP address into a list
# Print addr
If len (addr )! = 4: # The list after cutting must have four parameters
Print "check ip address failed! "
Sys. exit ()
For I in range (4 ):
Try:
Addr [I] = int (addr [I]) # each parameter must be a number; otherwise, verification fails.
Except t:
Print "check ip address failed! "
Sys. exit ()
If addr [I] <= 255 and addr [I]> = 0: # each parameter value must be between 0 and.
Pass
Else:
Print "check ip address failed! "
Sys. exit ()
I + = 1
Else:
Print "check ip address success! "
If len (sys. argv )! = 2: # The input length must be 2
Print "Example: % s 10.0.0.1" % sys. argv [0]
Sys. exit ()
Else:
Check_ip (sys. argv [1]) # The IP address verification function is called to meet the condition.
2. verify the validity of IP addresses using shell
Execution result:
The value 0 is valid, and the value 0 is invalid.
Shell code:
Copy codeThe Code is as follows:
[Root @ yang python] # vi check_ip.sh
#! /Usr/bin/sh
CheckIPAddr ()
{
Echo $1 | grep "^ [0-9] \ {1, 3 \}\. \ ([0-9] \ {1, 3 \}\. \) \ {2 \} [0-9] \ {1, 3 \} $ ">/dev/null;
# The IP address must be a full number.
If [$? -Ne 0]
Then
Return 1
Fi
Ipaddr = $1
A = 'echo $ ipaddr | awk-F. '{print $1}' # separated by "." To retrieve the values of each column
B = 'echo $ ipaddr | awk-F. '{print $2 }''
C = 'echo $ ipaddr | awk-F. '{print $3 }''
D = 'echo $ ipaddr | awk-F. '{print $4 }''
For num in $ a $ B $ c $ d
Do
If [$ num-gt 255] | [$ num-lt 0] # each value must be between 0 and.
Then
Return 1
Fi
Done
Return 0
}
If [$ #-ne 1]; then # determine the parameter quantity
Echo "Usage: $0 ipaddress ."
Exit
Else
CheckIPAddr $1
Fi
Python calls the shell script to obtain the output value in the middle of the shell script.
You try to read it with a while loop. I can see it only once from your code ..
How does python call shell scripts with parameters?
Example: shell script: t. sh Content: echo "this is a test shell with arguments" echo "arg1 = $1; arg2 = $2;" run the script. /t. sh zhao result: [noncode @ gnode108 knockdown_workflow] $. /t. sh zhao1 zhao2this is a test shell with argumentsarg1 = zhao1; arg2 = zhao2; python script: [noncode @ gnode108 knockdown_workflow] $ cat t. py #! /Usr/bin/env pythonimport osimport sysdef main (): print 'Hello world! 'If len (sys. argv) <2: print "usage: % s config log" % (sys. argv [0]) sys. exit (1) arg0 = sys. argv [0] arg1 = sys. argv [1] print "arg0 = % s; arg1 = % s" % (arg0, arg1) print "test. /t. sh: "OS. system ('. /t. sh' + arg0 + ''+ arg1) print" test method of replacing: "t ='t. sh'm = 'zhao' n = 'zhao' cmd = ". /% s "% (t, m, n) print" t = % s; m = % s; n = % s; cmd = % s "% (t, m, n, cmd) OS. system (cmd) if _ name _ = '_ main _ ': Main () running script: python t. py t. sh execution result: [noncode @ gnode108 knockdown_workflow] $ python t. py t. shHello world! Arg0 = t. py; arg1 = t. shtest. /t. sh: this is a test shell with argumentsarg1 = t. py; arg2 = t. sh; test method of replacing: t = t. sh; m = zhao; n = zhao; cmd =. /t. sh zhao zhaothis is a test shell with argumentsarg1 = zhao; arg2 = zhao; [noncode @ gnode108 knockdown_workflow] $ cat t. sh echo "this is a test shell with arguments" echo "arg1 = $1; arg2 = $2;" [noncode @ gnode108 knockdown_workflow] $. /t. sh zhao1 zhao2this is a test shell with ar ...... remaining full text>