SlideShare a Scribd company logo
Roberto Polli - roberto.polli@babel.it
Will iPython replace Bash?
s/bash/ipython/g
EuroPython 2013, 3th
July - Firenze
Babel Srl P.zza S. Benedetto da Norcia, 33 0040, Pomezia (RM) – www.babel.it
Roberto Polli - roberto.polli@babel.it
What? Who? Why?
How ipython can speed up your daily bash work, improve
reusability and your python confidence...without entirely
replacing bash.
Roberto Polli - Community Manager @ Babel.it. Loves writing in C,
Java and Python. Red Hat Certified Engineer and Virtualization
Administrator.
Babel – Proud sponsor of this talk ;) Delivers large mail
infrastructures based on Open Source software for Italian ISP and
PA. Contributes to various FLOSS.
Roberto Polli - roberto.polli@babel.it
Agenda - 1
Bash Rocks
Simple bash patterns
Python vs Bash: strenght and limits
Can python save the day?
SysAdmins can gently learn python!
Roberto Polli - roberto.polli@babel.it
Agenda - 2
Real life scenarios
●
using fields
●
parsing /proc/ files
●
reusing stuff
●
flaskize
●
test
You can manage it faster ;)
Roberto Polli - roberto.polli@babel.it
Python vs Bash
–Bash is "The" shell
–Python is the widespreading system
language
–A reunion?
# bash
for f in ${files[@]}; do
name=${f%.*}; ext=${f##*.}
mv "$f" "${name,,}.${ext}
done
# python
for f in files:
name, ext = f.splitext()
shutil.move(f,name.lower()+"."+ext)
Roberto Polli - roberto.polli@babel.it
Bash rocks - 1
Job Control
Simple pipelines and streams
# zipinfo -t just_files.zip | xargs rm --
Clean I/O redirection
# kinit user <<< 'secret' # kerberos login
# script.py 2>py.err < /etc/hosts # redirect stdin
for x in $(seq 1 10); do
sleep $x &
done
jobs -l;
wait %1
Roberto Polli - roberto.polli@babel.it
Bash rocks - 2
Braces and Globbing
# du -ms .[a-z]*
# touch {01..20}.out
...bash3 behaves differently from bash2
History and readline substitution
# mv foo.out foo-$(date -I).out
# !!:gs/foo/bar # same as above with "bar"
Long commands
# fc;
Roberto Polli - roberto.polli@babel.it
Wishes - 1
Safe system scripts
# touch -- -rf; rm * #ouch!
Readability
# mv "$file" "${file,,}"
# [ x$1 != x ] || exit
# ${WTF[@]}
Reusability
# . /etc/init.d/functions
# are you sure
# bash scripts
# are short?
RM=$(which rm)
? ${RM:?Missing rm}
WTF=(world taekwondo fed)
for file in ${A[@]}; do
$RM -- "$file";
done
Roberto Polli - roberto.polli@babel.it
Wishes - 2
Code consistency
●
bash3 != bash4
One language
●
perl
●
awk, gawk, mawk
●
grep, egrep, fgrep
●
sed, gsed,
●
gnuplot, bc
#bash4
if [[ $file =~ ^p ]]
then
strace $file |& 
grep inputrc
fi
# bash3
if echo "$file" | 
egrep -q "^p"
then
strace 2>&1 $file | 
grep inputrc
fi
Roberto Polli - roberto.polli@babel.it
Interactive Python
●
CPython delivers an interactive interpreter
●
Limited Readline and History support
●
Requires parenthesis and string quoting
●
Batteries included (math, telnetlib, shutil, ...)
$ telnet localhost 80
telnet: command not found
$ python -m telnetlib 0 80
Present by default
in almost
all Linux distros!
Roberto Polli - roberto.polli@babel.it
Interactive Python
Cpython shell can autocomplete!
import readline as rl
import rlcompleter
rl.parse_and_bind('tab: complete')
...but you can use iPython!
Roberto Polli - roberto.polli@babel.it
iPython: interactivity += 1
Easy to install
●
pip install ipython
Various flavours!
●
vanilla#ipython
●
math# ipython --pylab
●
shell# ipython --profile=[py]sh
Customizable
~/.ipython/profile_XX/ipython_config.py
Customize:
prompt
spacing
functions
shotcuts
modules
...
Roberto Polli - roberto.polli@babel.it
iPython
/bin/sh passthru:
●
ls -ld ~/python; # run via sh
●
ll /tmp; # alias pointing to ls -l
Capture both stdout and stderr
●
ret = !cat /etc/hosts #run via sh
●
ret = !ls /MISSING
Get exit status
●
print(_exit_code, "resembles $?")
ipython uses
os.system
# output is
# in a flexible
# list (SList)
type(ret)
_exit_code may
not be exactly
as expected
Roberto Polli - roberto.polli@babel.it
iPython features
Use SList as:
●
list or string
●
w or w/o newlines
Various Magics
%autocall
%edit
%debug
Automagic string-ify
and parenthesis
#ret = !cat /etc/resolv.conf
[ "search babel.it",
"nameserver 127.0.0.1" ]
#ret.s == ' '.join(ret)
True
#ret.nlstr == 'n'.join(ret)
True
#, ret.grep babel.it
Roberto Polli - roberto.polli@babel.it
iPython expansion
Pass python stuff to the shell
# import os
# ret = !cat /proc/{os.getpid()}/status
Consider the following steps during expansion
1. ipython parses the line and expands {contents}
2. prepare the command to be executed to the shell
3. fork/exec the shell
4. the shell interprets the command!
# from os import getpid as PID
# ret = !cat /proc/{PID()}/* # guess what?
Roberto Polli - roberto.polli@babel.it
Parsing system files
Get ipython Memory usage
# ret = !cat /proc/{os.getpid()}/status
Replace AWK
# ret.fields(0) # awk '{print $1;}'
# ret.fields(2,1,3) # awk '{print $2, $1, $3;}'
# ret.fields(*range(1,10)) # print 2..9th fields
# ret.fields(-1) # awk '{print $NF;}'
VmPeak: 27308 kB
VmSize: 27244 kB
VmHWM: 7080 kB
VmRSS: 7072 kB
Roberto Polli - roberto.polli@babel.it
Unpack data
SList.fields() just splits
by white-space
fieldize() is more
generic!
iPython profiles loads
custom files under
./startup/ directory.
# 99-fieldize.py
# save under the ./startup/
# of the rofile directory:
# ~/.ipython/profile_xx/
def fieldize(ret, sep=":"):
"""Let's be generic"""
return dict([
map(str.strip,
x.split(sep,1))
for x in ret ])
Roberto Polli - roberto.polli@babel.it
monitoring.ipy
#!/usr/bin/ipython --profile=pysh
# Important: save as .ipy to run with ipython
from time import sleep
from os import getpid
fields = '{VmSize},{VmRSS},{VmSwap}'
while sleep(1) == None:
ret = !grep ^Vm /proc/{getpid()}/status
d = fieldize(ret)
print(fields.format(**d))
Roberto Polli - roberto.polli@babel.it
$PITFALLS - 1
Beware of bash expansion/substitution
ret = !echo /proc/$$/cmdline
1.ipython replaces $fn with its
python value - eg. "cwd"
2.then uses os.system
3.fork() happens before bash
expansion
4.the shell interprets $$ as the
pid of the current process (the
forked one!)
Roberto Polli - roberto.polli@babel.it
$PITFALLS - 1
Expand Early and in Python
unless you know what you're doing!
GOOD: ! echo /proc/{os.getpid()}/cmdline
EVIL: ! echo /proc/$$/cmdline
Roberto Polli - roberto.polli@babel.it
$PITFALLS - 2
os.system|subprocess use /bin/shell
# this = !{echo does; echo >&2 not; } |& grep work
We can work it out ;)
edit _process_common.py
add the `executable` argument to
subprocess.Popen (
executable= '/bin/bash',
shell=True, ...)
IMHO: Don't you trust os.environ.get('SHELL')? Don't trust
os.system too!
quick &
dirty
Roberto Polli - roberto.polli@babel.it
Plotting system data
A sysadmin must plot...
# from matplotlib import pyplot
Collect data...
#ret = !ping -t 100 -w 100 foo.com
#ret = ret.fields(7).grep('time')
Mangle it... (get just millisecs)
#ret = [ x.split("=")[1]
for x in ret ]
And show!
# pyplot.hist(map(float, ret))
# pyplot.show()
Roberto Polli - roberto.polli@babel.it
Flaskize
Scripting in python you'll collect a
lot of useful snippets in a very
short time.
%history #use history magic
Flask is a web microframework
you can use to convert them in
webservices!
Can't use the !command syntax
with Flask...(can I?)
"""Things are easy even without
the !command stuff
"""
from flask import Flask
import simplejson
from fieldize import fieldize
app = Flask(__name__)
@app.route('/r/mem.view/<pid>')
def monitor(pid):
"""Return some process info"""
int(pid) # trigger ValueError
fpath = '/proc/%s/status' % pid
# a file is iterable ;)
with open(fpath) as ret
d = fieldize(fpath, ":")
return simplejson.dumps(d)
app.run(debug=True)
Roberto Polli - roberto.polli@babel.it
Nosetests: speed up your tests!
Three simple steps
●
put your test files in ./test/
●
run #nosetests ./test/
●
nose discovers and runs
them all
...really! That's all!
"""Test is easy with nose.
Docstrings are used in reports!
"""
from telnetlib import Telnet
def mping(ip):
# ICMP requires root privileges ;)
cmd = '/bin/ping -w 2 -t 2 '+ip
return os.system(cmd)
def test_ping_gw():
"ping default gw"
assert mping('192.168.1.254')==0
def test_ping_remote():
"ping remote host"
assert mping('10.0.11.1')==0
def test_tcp_remote():
"port 80 remote"
port, timeout = 80, 1
Telnet('10.0.11.1', port, timeout).close()
Roberto Polli - roberto.polli@babel.it
Bonus tracks: Ovirt and 389ds
iPython auto-complete helps learning new API
●
Ovirt open-source virtualization infrastructure
●
389 LDAP directory server
#!sudo pip install ovirt-engine-sdk
from ovirtsdk.api import API
client = API(**{'url':.., 'username':..})
for vm in client.vm.list():
name = vm.get_name()
try: vm.stop() # PEP8 who? ;)
except: log.exception("Error stopping " + name)
Roberto Polli - roberto.polli@babel.it
Bonus tracks: Ovirt and 389ds
389 API are under development: feedback is important!
#git clone https://github.com/ioggstream/dsadmin
from dsadmin import DSAdmin
# connect to the server
ds = DSAdmin(**{'binddn':..., 'bindpw': ...})
# configure 389 to use SSL
ds.setupSSL()
# weird name? iPython will auto-complete!
ds.getReplStatus()
Development releases includes method name refactoring!
ds.replica.status()
ds.replica.agreements()
Roberto Polli - roberto.polli@babel.it
Will iPython replace Bash?
Not in the near future ;)
Can lead to a different use of bash
Compete with weakly interactive
toos like perl, awk, sed
It is a tool that you shouldn't miss!
Roberto Polli - roberto.polli@babel.it
Thank You!
roberto.polli@babel.it
Code
http://ipython.org/
http://flask.pocoo.org/
http://nose.readthedocs.org/
https://github.com/ioggstream/dsadmin/
Babel
http://www.babel.it
http://vaunaspada.babel.it/blog

More Related Content

PDF
Operating System Practice : Meeting 2-basic commands linux operating system-s...
PDF
Ry pyconjp2015 karaoke
PPTX
How to deliver a Python project
PDF
Extending Python - Codemotion Milano 2014
PDF
Extending Python, what is the best option for me?
PDF
Tracking huge files with Git LFS - LinuxCon 2016
PDF
Linux Kernel 개발참여방법과 문화 (Contribution)
PDF
Digital RSE: automated code quality checks - RSE group meeting
Operating System Practice : Meeting 2-basic commands linux operating system-s...
Ry pyconjp2015 karaoke
How to deliver a Python project
Extending Python - Codemotion Milano 2014
Extending Python, what is the best option for me?
Tracking huge files with Git LFS - LinuxCon 2016
Linux Kernel 개발참여방법과 문화 (Contribution)
Digital RSE: automated code quality checks - RSE group meeting

What's hot (18)

PDF
Tracking large game assets with Git LFS
PDF
PEARC17: Modernizing GooFit: A Case Study
PPT
101 3.2 process text streams using filters
PPT
101 3.2 process text streams using filters
PDF
Git and Unity
PPT
3.2 process text streams using filters
PDF
[Forward4 Webinar 2016] Building IoT Prototypes w/ Raspberry Pi
PDF
Deconstruct 2017: All programmers MUST learn C and Assembly
PDF
Python入門 : 4日間コース社内トレーニング
PPTX
Licão 06 process text streams with filters
TXT
System log
PDF
CMake best practices
PDF
Holger Krekel: Re-inventing packaging and testing with python
DOC
Filelist
PPTX
Installing tensorflow object detection on raspberry pi
PDF
Raspberry Pi + ROS
PDF
All of Your Network Monitoring is (probably) Wrong
PPTX
Large scale nlp using python's nltk on azure
Tracking large game assets with Git LFS
PEARC17: Modernizing GooFit: A Case Study
101 3.2 process text streams using filters
101 3.2 process text streams using filters
Git and Unity
3.2 process text streams using filters
[Forward4 Webinar 2016] Building IoT Prototypes w/ Raspberry Pi
Deconstruct 2017: All programmers MUST learn C and Assembly
Python入門 : 4日間コース社内トレーニング
Licão 06 process text streams with filters
System log
CMake best practices
Holger Krekel: Re-inventing packaging and testing with python
Filelist
Installing tensorflow object detection on raspberry pi
Raspberry Pi + ROS
All of Your Network Monitoring is (probably) Wrong
Large scale nlp using python's nltk on azure
Ad

Similar to Will iPython replace bash? (20)

PDF
Introduction to IPython & Jupyter Notebooks
PPTX
Python on pi
PDF
Python for Linux System Administration
PDF
PyCon 2013 : Scripting to PyPi to GitHub and More
ODP
Learn python
PDF
How to write a well-behaved Python command line application
PPTX
Introduction to Python for Security Professionals
PDF
PythonNotes1.pdf
PDF
Introduction to python 3
PDF
Python for System Administrators
PPT
Python in telecommunications (in 7 minutes)
PDF
Introduction to python 3 2nd round
PDF
Wait, IPython can do that?! (30 minutes)
PDF
Pyhton-1a-Basics.pdf
PDF
Python for DevOps Learn Ruthlessly Effective Automation 1st Edition Noah Gift
PPTX
How Python Empowers Ethical Hackers by Supriya Kumar Mitra
PDF
Build and deploy scientific Python Applications
PDF
جلسه اول پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲
PDF
1203 ipython pycon
Introduction to IPython & Jupyter Notebooks
Python on pi
Python for Linux System Administration
PyCon 2013 : Scripting to PyPi to GitHub and More
Learn python
How to write a well-behaved Python command line application
Introduction to Python for Security Professionals
PythonNotes1.pdf
Introduction to python 3
Python for System Administrators
Python in telecommunications (in 7 minutes)
Introduction to python 3 2nd round
Wait, IPython can do that?! (30 minutes)
Pyhton-1a-Basics.pdf
Python for DevOps Learn Ruthlessly Effective Automation 1st Edition Noah Gift
How Python Empowers Ethical Hackers by Supriya Kumar Mitra
Build and deploy scientific Python Applications
جلسه اول پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲
1203 ipython pycon
Ad

More from Roberto Polli (20)

PDF
Ratelimit Headers for HTTP
PDF
Interoperability rules for an European API ecosystem: do we still need SOAP?
PDF
Docker - virtualizzazione leggera
PDF
Just one-shade-of-openstack
PDF
Test Drive Deployment with python and nosetest
ODP
Tox as project descriptor.
PDF
Scaling mysql with python (and Docker).
PDF
Orchestrating MySQL with Python and Docker
PDF
Statistics 101 for System Administrators
ODP
Pysmbc Python C Modules are Easy
PDF
Git gestione comoda del repository
PDF
Testing with my sql embedded
PPT
Servizi di messaging & collaboration in mobilità: Il panorama open source
ODP
Funambol al Linux Day 2009
PDF
ICalendar RFC2445 - draft1
PDF
Presenting CalDAV (draft 1)
ODP
Integrating Funambol with CalDAV and LDAP
ODP
ultimo-miglio-v3
ODP
Ultimo Miglio v2
ODP
Ultimo Miglio
Ratelimit Headers for HTTP
Interoperability rules for an European API ecosystem: do we still need SOAP?
Docker - virtualizzazione leggera
Just one-shade-of-openstack
Test Drive Deployment with python and nosetest
Tox as project descriptor.
Scaling mysql with python (and Docker).
Orchestrating MySQL with Python and Docker
Statistics 101 for System Administrators
Pysmbc Python C Modules are Easy
Git gestione comoda del repository
Testing with my sql embedded
Servizi di messaging & collaboration in mobilità: Il panorama open source
Funambol al Linux Day 2009
ICalendar RFC2445 - draft1
Presenting CalDAV (draft 1)
Integrating Funambol with CalDAV and LDAP
ultimo-miglio-v3
Ultimo Miglio v2
Ultimo Miglio

Recently uploaded (20)

PDF
Network Security Unit 5.pdf for BCA BBA.
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PPTX
Big Data Technologies - Introduction.pptx
PPTX
sap open course for s4hana steps from ECC to s4
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PPTX
Cloud computing and distributed systems.
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Network Security Unit 5.pdf for BCA BBA.
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Digital-Transformation-Roadmap-for-Companies.pptx
Big Data Technologies - Introduction.pptx
sap open course for s4hana steps from ECC to s4
Spectral efficient network and resource selection model in 5G networks
Reach Out and Touch Someone: Haptics and Empathic Computing
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
The Rise and Fall of 3GPP – Time for a Sabbatical?
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
Cloud computing and distributed systems.
Mobile App Security Testing_ A Comprehensive Guide.pdf
Chapter 3 Spatial Domain Image Processing.pdf
Unlocking AI with Model Context Protocol (MCP)
Diabetes mellitus diagnosis method based random forest with bat algorithm
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
MIND Revenue Release Quarter 2 2025 Press Release
20250228 LYD VKU AI Blended-Learning.pptx
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...

Will iPython replace bash?

  • 1. Roberto Polli - roberto.polli@babel.it Will iPython replace Bash? s/bash/ipython/g EuroPython 2013, 3th July - Firenze Babel Srl P.zza S. Benedetto da Norcia, 33 0040, Pomezia (RM) – www.babel.it
  • 2. Roberto Polli - roberto.polli@babel.it What? Who? Why? How ipython can speed up your daily bash work, improve reusability and your python confidence...without entirely replacing bash. Roberto Polli - Community Manager @ Babel.it. Loves writing in C, Java and Python. Red Hat Certified Engineer and Virtualization Administrator. Babel – Proud sponsor of this talk ;) Delivers large mail infrastructures based on Open Source software for Italian ISP and PA. Contributes to various FLOSS.
  • 3. Roberto Polli - roberto.polli@babel.it Agenda - 1 Bash Rocks Simple bash patterns Python vs Bash: strenght and limits Can python save the day? SysAdmins can gently learn python!
  • 4. Roberto Polli - roberto.polli@babel.it Agenda - 2 Real life scenarios ● using fields ● parsing /proc/ files ● reusing stuff ● flaskize ● test You can manage it faster ;)
  • 5. Roberto Polli - roberto.polli@babel.it Python vs Bash –Bash is "The" shell –Python is the widespreading system language –A reunion? # bash for f in ${files[@]}; do name=${f%.*}; ext=${f##*.} mv "$f" "${name,,}.${ext} done # python for f in files: name, ext = f.splitext() shutil.move(f,name.lower()+"."+ext)
  • 6. Roberto Polli - roberto.polli@babel.it Bash rocks - 1 Job Control Simple pipelines and streams # zipinfo -t just_files.zip | xargs rm -- Clean I/O redirection # kinit user <<< 'secret' # kerberos login # script.py 2>py.err < /etc/hosts # redirect stdin for x in $(seq 1 10); do sleep $x & done jobs -l; wait %1
  • 7. Roberto Polli - roberto.polli@babel.it Bash rocks - 2 Braces and Globbing # du -ms .[a-z]* # touch {01..20}.out ...bash3 behaves differently from bash2 History and readline substitution # mv foo.out foo-$(date -I).out # !!:gs/foo/bar # same as above with "bar" Long commands # fc;
  • 8. Roberto Polli - roberto.polli@babel.it Wishes - 1 Safe system scripts # touch -- -rf; rm * #ouch! Readability # mv "$file" "${file,,}" # [ x$1 != x ] || exit # ${WTF[@]} Reusability # . /etc/init.d/functions # are you sure # bash scripts # are short? RM=$(which rm) ? ${RM:?Missing rm} WTF=(world taekwondo fed) for file in ${A[@]}; do $RM -- "$file"; done
  • 9. Roberto Polli - roberto.polli@babel.it Wishes - 2 Code consistency ● bash3 != bash4 One language ● perl ● awk, gawk, mawk ● grep, egrep, fgrep ● sed, gsed, ● gnuplot, bc #bash4 if [[ $file =~ ^p ]] then strace $file |& grep inputrc fi # bash3 if echo "$file" | egrep -q "^p" then strace 2>&1 $file | grep inputrc fi
  • 10. Roberto Polli - roberto.polli@babel.it Interactive Python ● CPython delivers an interactive interpreter ● Limited Readline and History support ● Requires parenthesis and string quoting ● Batteries included (math, telnetlib, shutil, ...) $ telnet localhost 80 telnet: command not found $ python -m telnetlib 0 80 Present by default in almost all Linux distros!
  • 11. Roberto Polli - roberto.polli@babel.it Interactive Python Cpython shell can autocomplete! import readline as rl import rlcompleter rl.parse_and_bind('tab: complete') ...but you can use iPython!
  • 12. Roberto Polli - roberto.polli@babel.it iPython: interactivity += 1 Easy to install ● pip install ipython Various flavours! ● vanilla#ipython ● math# ipython --pylab ● shell# ipython --profile=[py]sh Customizable ~/.ipython/profile_XX/ipython_config.py Customize: prompt spacing functions shotcuts modules ...
  • 13. Roberto Polli - roberto.polli@babel.it iPython /bin/sh passthru: ● ls -ld ~/python; # run via sh ● ll /tmp; # alias pointing to ls -l Capture both stdout and stderr ● ret = !cat /etc/hosts #run via sh ● ret = !ls /MISSING Get exit status ● print(_exit_code, "resembles $?") ipython uses os.system # output is # in a flexible # list (SList) type(ret) _exit_code may not be exactly as expected
  • 14. Roberto Polli - roberto.polli@babel.it iPython features Use SList as: ● list or string ● w or w/o newlines Various Magics %autocall %edit %debug Automagic string-ify and parenthesis #ret = !cat /etc/resolv.conf [ "search babel.it", "nameserver 127.0.0.1" ] #ret.s == ' '.join(ret) True #ret.nlstr == 'n'.join(ret) True #, ret.grep babel.it
  • 15. Roberto Polli - roberto.polli@babel.it iPython expansion Pass python stuff to the shell # import os # ret = !cat /proc/{os.getpid()}/status Consider the following steps during expansion 1. ipython parses the line and expands {contents} 2. prepare the command to be executed to the shell 3. fork/exec the shell 4. the shell interprets the command! # from os import getpid as PID # ret = !cat /proc/{PID()}/* # guess what?
  • 16. Roberto Polli - roberto.polli@babel.it Parsing system files Get ipython Memory usage # ret = !cat /proc/{os.getpid()}/status Replace AWK # ret.fields(0) # awk '{print $1;}' # ret.fields(2,1,3) # awk '{print $2, $1, $3;}' # ret.fields(*range(1,10)) # print 2..9th fields # ret.fields(-1) # awk '{print $NF;}' VmPeak: 27308 kB VmSize: 27244 kB VmHWM: 7080 kB VmRSS: 7072 kB
  • 17. Roberto Polli - roberto.polli@babel.it Unpack data SList.fields() just splits by white-space fieldize() is more generic! iPython profiles loads custom files under ./startup/ directory. # 99-fieldize.py # save under the ./startup/ # of the rofile directory: # ~/.ipython/profile_xx/ def fieldize(ret, sep=":"): """Let's be generic""" return dict([ map(str.strip, x.split(sep,1)) for x in ret ])
  • 18. Roberto Polli - roberto.polli@babel.it monitoring.ipy #!/usr/bin/ipython --profile=pysh # Important: save as .ipy to run with ipython from time import sleep from os import getpid fields = '{VmSize},{VmRSS},{VmSwap}' while sleep(1) == None: ret = !grep ^Vm /proc/{getpid()}/status d = fieldize(ret) print(fields.format(**d))
  • 19. Roberto Polli - roberto.polli@babel.it $PITFALLS - 1 Beware of bash expansion/substitution ret = !echo /proc/$$/cmdline 1.ipython replaces $fn with its python value - eg. "cwd" 2.then uses os.system 3.fork() happens before bash expansion 4.the shell interprets $$ as the pid of the current process (the forked one!)
  • 20. Roberto Polli - roberto.polli@babel.it $PITFALLS - 1 Expand Early and in Python unless you know what you're doing! GOOD: ! echo /proc/{os.getpid()}/cmdline EVIL: ! echo /proc/$$/cmdline
  • 21. Roberto Polli - roberto.polli@babel.it $PITFALLS - 2 os.system|subprocess use /bin/shell # this = !{echo does; echo >&2 not; } |& grep work We can work it out ;) edit _process_common.py add the `executable` argument to subprocess.Popen ( executable= '/bin/bash', shell=True, ...) IMHO: Don't you trust os.environ.get('SHELL')? Don't trust os.system too! quick & dirty
  • 22. Roberto Polli - roberto.polli@babel.it Plotting system data A sysadmin must plot... # from matplotlib import pyplot Collect data... #ret = !ping -t 100 -w 100 foo.com #ret = ret.fields(7).grep('time') Mangle it... (get just millisecs) #ret = [ x.split("=")[1] for x in ret ] And show! # pyplot.hist(map(float, ret)) # pyplot.show()
  • 23. Roberto Polli - roberto.polli@babel.it Flaskize Scripting in python you'll collect a lot of useful snippets in a very short time. %history #use history magic Flask is a web microframework you can use to convert them in webservices! Can't use the !command syntax with Flask...(can I?) """Things are easy even without the !command stuff """ from flask import Flask import simplejson from fieldize import fieldize app = Flask(__name__) @app.route('/r/mem.view/<pid>') def monitor(pid): """Return some process info""" int(pid) # trigger ValueError fpath = '/proc/%s/status' % pid # a file is iterable ;) with open(fpath) as ret d = fieldize(fpath, ":") return simplejson.dumps(d) app.run(debug=True)
  • 24. Roberto Polli - roberto.polli@babel.it Nosetests: speed up your tests! Three simple steps ● put your test files in ./test/ ● run #nosetests ./test/ ● nose discovers and runs them all ...really! That's all! """Test is easy with nose. Docstrings are used in reports! """ from telnetlib import Telnet def mping(ip): # ICMP requires root privileges ;) cmd = '/bin/ping -w 2 -t 2 '+ip return os.system(cmd) def test_ping_gw(): "ping default gw" assert mping('192.168.1.254')==0 def test_ping_remote(): "ping remote host" assert mping('10.0.11.1')==0 def test_tcp_remote(): "port 80 remote" port, timeout = 80, 1 Telnet('10.0.11.1', port, timeout).close()
  • 25. Roberto Polli - roberto.polli@babel.it Bonus tracks: Ovirt and 389ds iPython auto-complete helps learning new API ● Ovirt open-source virtualization infrastructure ● 389 LDAP directory server #!sudo pip install ovirt-engine-sdk from ovirtsdk.api import API client = API(**{'url':.., 'username':..}) for vm in client.vm.list(): name = vm.get_name() try: vm.stop() # PEP8 who? ;) except: log.exception("Error stopping " + name)
  • 26. Roberto Polli - roberto.polli@babel.it Bonus tracks: Ovirt and 389ds 389 API are under development: feedback is important! #git clone https://github.com/ioggstream/dsadmin from dsadmin import DSAdmin # connect to the server ds = DSAdmin(**{'binddn':..., 'bindpw': ...}) # configure 389 to use SSL ds.setupSSL() # weird name? iPython will auto-complete! ds.getReplStatus() Development releases includes method name refactoring! ds.replica.status() ds.replica.agreements()
  • 27. Roberto Polli - roberto.polli@babel.it Will iPython replace Bash? Not in the near future ;) Can lead to a different use of bash Compete with weakly interactive toos like perl, awk, sed It is a tool that you shouldn't miss!
  • 28. Roberto Polli - roberto.polli@babel.it Thank You! roberto.polli@babel.it Code http://ipython.org/ http://flask.pocoo.org/ http://nose.readthedocs.org/ https://github.com/ioggstream/dsadmin/ Babel http://www.babel.it http://vaunaspada.babel.it/blog