|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
import paramiko ssh = paramiko.SSHClient() ssh.load_system_host_keys() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ssh.connect(hostname = '192.168.100.20' ,port = 58422 ,username = 'oldboy' ) stdin, stdout,stderr = ssh.exec_command( 'uptime' ) type (stdout) paramiko.ChannelFile print stderr.readlines() [] print stdout.readlines() [ ' 21:35:05 up 1 day, 55 min, 2 users, load average: 0.00, 0.00, 0.00\n' ] |
|
1
|
ssh -p58422 oldboy@192.168.100.20 -o StrictHostKeyChecking=no 'uptime' |
|
1
2
3
4
5
6
7
|
In [ 6 ]: retcode = subprocess.call( 'ls -l' , shell = True ) total 12 - rw - rw - r - - . 1 oldboy oldboy 239 Jan 19 21 : 13 access.log - rw - rw - r - - . 1 oldboy oldboy 458 Jan 19 20 : 50 arp.txt - rw - r - - r - - . 1 oldboy oldboy 184 Jan 16 12 : 04 hosts In [ 7 ]: print retcode 0 |
|
1
2
|
child1 = subprocess.Popen([ "cat" , "/etc/passwd" ], stdout = subprocess.PIPE) child1.stdout.readlines() |
|
1
2
3
|
child1 = subprocess.Popen( 'sh ' + file + ' ' + um, shell = True , stdout = subprocess.PIPE) status = child1.wait() output = child1.stdout.read().strip() |
|
1
|
status,output = commands.getstatusoutput( 'cat /etc/passwd' ) |
|
1
2
|
In [ 26 ]: random.random() Out[ 26 ]: 0.6289910862564466 |
|
1
2
3
4
|
In [ 27 ]: random.sample( xrange ( 1 , 100 ), 3 ) Out[ 27 ]: [ 94 , 91 , 53 ] In [ 28 ]: random.sample( 'asdfasdf' , 3 ) Out[ 28 ]: [ 'f' , 'a' , 'a' ] |
|
1
2
|
In [ 29 ]: random.randint( 1 , 20 ) Out[ 29 ]: 18 |
|
1
2
|
In [ 49 ]: uuid.uuid1() Out[ 49 ]: UUID( 'cbb8c051-0929-11e6-9ba3-8c2937eebf3a' ) |
|
1
2
|
In [ 50 ]: str (uuid.uuid1()) Out[ 50 ]: 'cf296582-0929-11e6-8bbf-8c2937eebf3a' |
|
1
2
|
In [ 48 ]: hashlib.md5( str (uuid.uuid1())).hexdigest() Out[ 48 ]: 'd4aacc5bb29a24fd9db8e2ea1bf53cb7' |
|
1
2
3
4
5
6
7
8
|
In [ 9 ]: import re In [ 10 ]: s = "10.1.1.223" In [ 11 ]: if re.match(r "10.1" , s): ....: print "为10.1网段" ....: else : ....: print "不在10.1网段" ....: 为 10.1 网段 |
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
In [ 73 ]: from collections import OrderedDict In [ 74 ]: od = OrderedDict() In [ 75 ]: od[ 'key1' ] = 'value1' In [ 76 ]: od[ 'key2' ] = 'value2' In [ 77 ]: od[ 'key3' ] = 'value3' In [ 78 ]: od Out[ 78 ]: OrderedDict([( 'key1' , 'value1' ), ( 'key2' , 'value2' ), ( 'key3' , 'value3' )]) In [ 79 ]: od.keys() Out[ 79 ]: [ 'key1' , 'key2' , 'key3' ] In [ 80 ]: for k,v in od.ite od.items od.iteritems od.iterkeys od.itervalues In [ 80 ]: for k,v in od.items(): ....: print k,v ....: key1 value1 key2 value2 key3 value3 |
|
1
2
3
|
In [ 11 ]: from collections import Counter In [ 12 ]: l = [ 'a' , 'b' , 'a' , 'c' , 'a' , 'd' ] In [ 13 ]: number_rep = Counter(l) |
|
1
2
3
4
|
In [ 14 ]: number_rep Out[ 14 ]: Counter({ 'a' : 3 , 'b' : 1 , 'c' : 1 , 'd' : 1 }) In [ 15 ]: type (number_rep) Out[ 15 ]: collections.Counter |
|
1
2
|
In [ 16 ]: number_rep[ "a" ] Out[ 16 ]: 3 |
|
1
2
|
In [ 18 ]: number_rep.keys() Out[ 18 ]: [ 'a' , 'c' , 'b' , 'd' ] |
|
1
2
|
In [ 19 ]: number_rep.most_common( 1 ) Out[ 19 ]: [( 'a' , 3 )] |
|
1
2
3
4
5
|
In [ 21 ]: s = "efghfgfefda" In [ 22 ]: Counter(s) Out[ 22 ]: Counter({ 'a' : 1 , 'd' : 1 , 'e' : 2 , 'f' : 4 , 'g' : 2 , 'h' : 1 }) In [ 23 ]: number_rep + Counter(s) Out[ 23 ]: Counter({ 'a' : 4 , 'b' : 1 , 'c' : 1 , 'd' : 2 , 'e' : 2 , 'f' : 4 , 'g' : 2 , 'h' : 1 }) |
|
1
|
l_lines = linecache.getlines( 'filename' ) |
|
1
|
s_line = linecache.getline( 'filename' , linenumber).rstrip() |
|
1
|
l_lines = linecache.updatecache( 'filename' ) |
|
1
|
linecache.checkcache() |
|
1
|
linecache.checkcache( 'filename' ) |