2013年3月13日水曜日

Catalyst 2970でパケットキャプチャ用の設定

> en
# conf t

ポート1で送受信されるパケットを、ポート2につないだPCでキャプチャする場合。

# monitor session 1 source interface FastEthernet0/1
# monitor session 1 destination interface FastEthernet0/2

VLANの場合

# monitor session 2 source vlan 10
# monitor session 2 destination interface Gi0/2


IPv6 disable

ubuntu 12.04でIPv6をdisableにする。

新しくファイルを作る
/etc/sysctl.d/60-ipv6-disable.conf

ファイル編集

net.ipv6.conf.all.disable_ipv6 = 1
net.ipv6.conf.default.disable_ipv6 = 1

user mode linuxのネットワーク設定

apt-getでuml-utilitiesをインストールすると、/etc/init.d/uml-utilitiesができる。これはlinux起動時にuml_switchというアプリを動かす。

このアプリはuser mode linux上のゲストOSにネットワーク(Layer2HUB、Layer2switchとも言う)を提供する。

たとえば、ゲストOS起動時のパラメータで
linux eth0=daemon
のように「daemon」と指定するとuml_switchに繋いでくれる。

ただし、ubuntu 12.04のデフォルトでは閉じたネットワークなので外には出られない。

/etc/init.d/uml-utilitiesを
UML_SWITCH_OPTIONS="-tap tap0"
のように変えると外に出るためのtapデバイスを用意してくれる。

tunctl -u uml-net -t tap0
このようにtap0をuml-netユーザで使用すると予約しておけば、uml_switchをrootで動かさなくてもtapデバイスを作成できる。

#通常、tapデバイスはrootでないと作れない


これでtap0ができるのでホストOSでブリッジなどを作成して繋いでやる。

brctl addbr br0
brctl stp br0 off
brctl addif br0 eth0
brctl addif br0 tap0
ifconfig eth0 up
ifconfig br0 up
ifconfig tap0 up


ゲストOS用のネットワークが1つしかないのは不便なのでいくつか作成する。
ネットワークの識別はunix domian socketで行う。
/etc/init.d/uml-utilitiesの
UML_SWITCH_CTL="$UML_DIR/uml_switch.ctl"がそのファイル名。
このファイル名やpidファイル名を各ネットワークで分ける。

ゲストOS起動時は
linux eth-=daemon,,unix,/var/run/uml-utilities/uml_switch.ctl
とネットワークを指定する。


----------------------------------------
追記
/etc/network/interfaces例

auto lo
iface lo inet loopback

auto br0
iface br0 inet dhcp
bridge_ports eth0 tap0
bridge_stp off
bridge_wait 0
bridge_fd 0
up echo 0 > /sys/devices/virtual/$IFACE/bridge/multicast_snooping
pre-up ifconfig eth0 up
pre-up tunctl -u uml-net -t tap0

libvirtのネットワーク設定

virshコマンドでVM用のネットワークを設定する。

virsh net-define test.xml

test.xmlはこんな感じ
#ホストにbr0のブリッジがある場合の設定
#br0はホストの/etc/network/interfacesとかで作成済み

<network>
  <name>host-bridge-br0-eth0</name>
  <forward mode="bridge"/>
  <bridge name="br0"/>
</network>

virtioとPACKET

kvmで動かしたVM上のアプリで、socket(AF_PACKET)でキャプチャすると、TCP/IPのチェックサムが化けてるという現象に会った。

kvmのvirtio-netにそういう癖があるみたい。

libvirtを使ってゲストOSを扱ってるので設定(XML)を変更して回避する。

<domain type='kvm'>
  <name>guest</name>
  <memory>524288</memory>
  <currentMemory>524288</currentMemory>
  <vcpu>1</vcpu>
  <os>
    <type arch='x86_64' machine='pc-1.0'>hvm</type>
    <boot dev='hd'/>
  </os>

  <features>
    <acpi/>
  </features>

  <on_poweroff>destroy</on_poweroff>
  <on_reboot>restart</on_reboot>
  <on_crash>restart</on_crash>

  <devices>

    <disk type='file' device='disk'>
      <source file='guest.img'/>
      <target dev='hda' bus='virtio'/>
    </disk>

    <disk type='file' device='cdrom'>
      <source file='/vm/iso/ubuntu-12.04-server-amd64.iso'/>
      <target dev='hdc' bus='ide'/>
      <readonly/>
    </disk>

    <interface type='bridge'>
      <mac address='52:54:00:99:00:00'/>
      <source bridge='br99'/>
      <!--  <model type='virtio'/>  ここをコメントにする  -->
    </interface>

    <input type='mouse' bus='ps2'/>

    <graphics type='vnc' port='5900' autoport='no' listen='0.0.0.0' keymap='ja'>
      <listen type='address' address='0.0.0.0'/>
    </graphics>

  </devices>
</domain>

2013年3月12日火曜日

pythonでイメージファイル操作

Python Imaging Library (PIL)
http://www.pythonware.com/products/pil/

準備
sudo apt-get install python-imaging


#!/usr/bin/env python
# -*- coding: utf-8 -*-

import Image

#Image.new('RGBA', (width, height), (R, G, B, A))
img = Image.new('RGBA', (800, 800), (255,255,255,255))

# img.getpixel( (x,y) )

r = 255
g = 0
b = 0

for x in range(100,200):
  for y in range(200,300):
    img.putpixel( (x,y), (r,g,b) )

img.save("image.png")

ubuntuネットワーク設定

/etc/network/interfacesまわりのメモ。

基本
man interfaces
man bridge-utils-interfaces

最近のkernelにはmulticast snoopingの機能が入ってるのだが、私のよく書くテストプログラムに悪い影響があるので(普通はそんなことにはならないもんだが)、snooping機能をOFFにしたい場合はこんな感じで書いとく。

iface br0 inet dhcp
  bridge_ports eth0
  bridge_stp off
  bridge_waitport 0
  bridge_fd 0
  up echo 0 > /sys/devices/virtual/net/$IFACE/bridge/multicast_snooping

ubuntu desctopでマネージャーがいらない場合
service network-manager stop
apt-get purge network-manager

python http or https サーバ(flask編)

flask
http://flask.pocoo.org/

準備
sudo apt-get install python-flask


簡単にhttpsサーバを用意できるのでうれしいですね。

でもバグがあるのかpython 2.7.3では動かず適当に修正しました。

/usr/lib/python2.7/SocketServer.py
の470行目

request.shutdown(socket.SHUT_WR)

とあるのを

if isinstance(request,socket._socketobject):
  request.shutdown(socket.SHUT_WR)
else:
  request.shutdown()

こうしました。
SSLセッションの時はshutdown()は引数無しでないといけないみたい。
きちんとソースを追ってないけど、今のところはこれで回避。


#!/usr/bin/env python
# -*- coding: utf-8 -*-

import flask

app = flask.Flask( __name__ )

@app.route ( '/', methods=['GET', 'POST'] )
def index():

  #ヘッダの取得
  #flask.request.headers['Content-Type']

  #クエリの取得
  #flask.request.values.get('abc',None)


  if flask.request.method=='GET':
    return '<html><body>hello</body></html>'
  
  if flask.request.method=='POST':
    f = request.files['the_file']
    f.save('/tmp/' + f.filename)
    return flask.redirect(flask.url_for('index'))

  flask.abort(404)
  return 'abortしてるのでここは走らない'


@app.route ( '/abc' )
def abc():
  #templatesディレクトリのabc.htmlを使う
  return flask.render_template('abc.html')


@app.route ( '/text' )
def text():
    res = 'hello'
    return flask.Response(res, mimetype='text/plain')



#HTTPサーバの場合はこちら
app.run(host='0.0.0.0',port=80,debug=True)

#HTTPSサーバの場合はこちら
#app.run(host='0.0.0.0',port=443,debug=True,ssl_context='adhoc')

python httpサーバ(wsgi編)

#!/usr/bin/env python
# -*- coding: utf-8 -*-

from wsgiref.simple_server import make_server, WSGIRequestHandler
import urlparse


html_index = '<html><body>hello</body></html>'


def appl(environ,start_response):

  path = environ['PATH_INFO']
  method = environ['REQUEST_METHOD']

  if method=='GET':
    query = dict(urlparse.parse_qsl(environ.get('QUERY_STRING','')))

  if method=='POST':
    ro = environ['wsgi.input']
    length = int(environ.get('CONTENT_LENGTH', 0))
    query = dict(urlparse.parse_qsl(ro.read(length)))

  if path=='/' and method=='GET':
    response_headers = [('Content-Type', 'text/html'),
      ('Content-Length', str(len(html_index)))]
    start_response('200 OK', response_headers)
    return [html_index]

  if path=='/' and method=='POST':
    start_responce('303 See Other', [('Location', '/')])
    return []

  start_response('404 Not Found', [])
  return []


#  DNS逆名前解決しない
class myHandler(WSGIRequestHandler):
  def address_string(self):
    return self.client_address[0]


httpd = make_server('0.0.0.0', 80, appl, handler_class=myHandler)
httpd.serve_forever()

user mode linux(ubuntu 12.04)

以前はkvmを使ってたんですが、kvmに対応していないintel CPUに仮想環境を作らないといけなくなったので、user mode linuxを試してみた。

試してみると、これが案外すばらしい。何がいいかってメモリ使用量が30MBも割り当てれば動くというこの小ささ。

kvmにはVM間で同じメモリデータを共有する仕組みがあってそれもなかなか優秀だけど、もともとの消費サイズが小さいのも素晴らしいよね。

この辺を参考にするが、hardyベースだと古いから新しいのでと思ってpreciseで試してみるとうまく動かなくて、debianのsqueezeを使った。


sudo apt-get install user-mode-linux uml-utilities bridge-utils debootstrap

#1GB
dd if=/dev/zero of=rootfs bs=32k count=32k

mkfs.ext4 rootfs

sudo mount -o loop rootfs.img /mnt

sudo -i

#squeezeベースのルートファイルシステムを作る
debootstrap squeeze /mnt http://ftp.jp.debian.org/debian

#VMのfstab設定
echo "/dev/ubd0  /       ext4    defaults     0 1" > /mnt/etc/fstab
echo "proc       /proc   proc    defaults     0 0" >> /mnt/etc/fstab

#VMのディスクデバイス
mknod --mode=660 /mnt/dev/ubd0 b 98 0
chown root:disk /mnt/dev/ubd0

echo "127.0.0.1 localhost" > /mnt/etc/hosts

#ネットワーク設定
echo "auto lo" > /mnt/etc/network/interfaces
echo "iface lo inet loopback" >> /mnt/etc/network/interfaces
echo "auto eth0" >> /mnt/etc/network/interfaces
echo "iface eth0 inet dhcp" >> /mnt/etc/network/interfaces 

#rootのパスワードを設定しておく
chroot /mnt
passwd
exit

#VM起動時にコンソールがいっぱい表示されるので、ttyのところの2~6を無効にして
#コンソールは1つだけにする
vi /mnt/etc/inittab

#sudoから抜ける
exit

設定は以上。

VMの起動はこんな感じ。
linux ubd0=rootfs eth0=daemon umid=uml1 mem=32m

ネットワーク設定についてはまた今度