[red@dev1 ~]$ perl -MDateTime -e"print DateTime->VERSION"; echo
0.4501
This is where I post snippets of information about Information Technology. It is mostly for my own reference but I hope that others will find it useful and comments are welcome.
#!/usr/bin/env python
import requests
import json
# curl https://${PEFQDN}:4433/classifier-api/v1/groups \
# -H "Content-Type: application/json" \
# --cert /etc/puppetlabs/puppet/ssl/certs/${PEFQDN}.pem \
# --key /etc/puppetlabs/puppet/ssl/private_keys/${PEFQDN}.pem \
# --cacert /etc/puppetlabs/puppet/ssl/certs/ca.pem | python -m json.tool
#
# if you get this error message:
#
#requests.exceptions.SSLError: [Errno 1] _ssl.c:504: error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed
#
# try executing: # pip install requests[security]
#
# to install pip execute:
#
# yum install python-pip
#
url='https://ppt-001.example.com:4433/classifier-api/v1/groups'
headers = {"Content-Type": "application/json"}data={}
cacert='/etc/puppetlabs/puppet/ssl/certs/ca.pem'
key='/etc/puppetlabs/puppet/ssl/private_keys/ppt-001.example.com.pem'
cert='/etc/puppetlabs/puppet/ssl/certs/ppt-001.example.com.pem'
result = requests.get(url,
data=data, #no data needed for this request
headers=headers, #dict {"Content-Type":"application/json"}
cert=(cert,key), #key/cert pair verify=cacert
)
print json.dumps( result.json(), sort_keys=True, indent=4, separators=(',', ': '))
for i in result.json():
print i['name']
print i['id']
map
function is used for transforming lists element-wise: given a list and a code block, map
builds a new list (or hash) with elements derived from the corresponding elements of the original.@out = map { CODE } @in;
CODE
is some perl code that is given an element of the list as $_
and returns the replacement value for the new list.map
along with the built-in function ucfirst
to do them all at once:#!/usr/bin/perl use strict; use warnings; my @names = qw(bob anne frank jill); my @capitalised_names = map { ucfirst $_ } @names; foreach my $name (@capitalised_names) { print "$name\n"; }
Bob Anne Frank Jill
#!/usr/bin/perl use strict; use warnings; my @names = qw(bob anne frank jill); my @everyone = map { $_, $_ . "'s dog", $_ . "'s cat" } @names; foreach my $name (@everyone) { print "$name\n"; }
bob bob's dog bob's cat anne anne's dog anne's cat frank frank's dog frank's cat jill jill's dog jill's cat
#!/usr/bin/perl use strict; use warnings; my @names = qw(bob anne frank jill); my %lengths = map { $_ => length $_ } @names; while (my ($name, $length) = each %lengths) { print "'$name' has $length characters\n"; }
'bob' has 3 characters 'anne' has 4 characters 'frank' has 5 characters 'jill' has 4 characters
id
.#!/usr/bin/perl use strict; use warnings; my @array = ({ id => 1, name => 'Bob', },{ id => 2, name => 'Anne', },{ id => 3, name => 'Frank' }); my %hash = map { $_->{id} => { name => $_->{name} } } @array; for my $id (1..3) { my $name = $hash{$id}->{name}; print "User $id: $name\n"; }
User 1: Bob User 2: Anne User 3: Frank
perldoc -f map
[root@localhost ~]# sestatusnow execute:
SELinux status: enabled
SELinuxfs mount: /sys/fs/selinux
SELinux root directory: /etc/selinux
Loaded policy name: targeted
Current mode: enforcing
Mode from config file: enforcing
Policy MLS status: enabled
Policy deny_unknown status: allowed
Max kernel policy version: 28
[root@localhost ~]# sed -i 's/enforcing/disabled/g' /etc/selinux/config
$ defaults write com.apple.dock no-bouncing -bool TRUE $ killall Dock