Monthly Archives: December 2011

Paramiko channel hangs

When sending a command via ssh using paramiko, the script would hang. eg:

def ssh_connect(self):
    """ connects to the remote server using paramiko """
    ssh = paramiko.SSHClient()
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    ssh.connect(self.hostname, self.remoteport, self.remoteuser, None, None, self.keypath)
    return ssh

def ssh_command(self, command):
    """ executes long command on remote server """
    ssh = self.ssh_connect()
    channel = ssh.invoke_shell()
    stdin = channel.makefile('wb')
    stdout = channel.makefile('rb')
    stdin.write(command)
    ssh_out = stdout.read()
    stdout.close(); stdin.close(); ssh.close()
    return ssh_out

command = """
    tar -zxvf {rp} || echo 'deploy-copy-untar-error {rp}'
    rm {rp} || echo 'deploy-copy-delete-error {rp}'
    echo 'deploy-copy-success {rp}'
    """.format(rp = remotepath)
ssh_out = self.ssh_command(command)

You need to add ‘exit’ to the end of the command so the channel quits and the script continues. Like so:
command = “””
tar -zxvf {rp} || echo ‘deploy-copy-untar-error {rp}’
rm {rp} || echo ‘deploy-copy-delete-error {rp}’
echo ‘deploy-copy-success {rp}’
exit
“””.format(rp = remotepath)
sshout = self.sshcommand(command)

Paramiko on Ubuntu 10.04: GMP or MPIR library not found

Getting the following error when trying to install paramiko on Ubuntu 10.04 (python 2.6.5) with pip – because of pycrypto dependancy:

warning: GMP or MPIR library not found; Not building Crypto.PublicKey._fastmath

Googling this problem leads to lots of results from Mac users, not to much from Ubuntu users. I don’t have this issue on Ubuntu 11.04 (python 2.7), so I assume it’s the python version.

To fix:

apt-get install python-dev

Then try again!

Ubuntu forward proxy with mod_proxy

  1. apt-get install libapache2-mod-proxy-html
  2. a2enmod proxy_html
  3. cp /etc/apache2/sites-available/default /etc/apache2/sites-available/proxy
  4. put in a custom port, and the following:

    ProxyRequests On
    ProxyVia On

    Order deny,allow
    Deny from all
    Allow from [hostname or IP]

  5. put the following at the bottom of ports.conf:

    Listen [your port]
  6. create some symlinks:
    cd /etc/apache2/mods-enabled/
    ln -s ../mods-available/proxy.load
    ln -s ../mods-available/proxy_http.load
  7. restart apache
    service apache2 restart
  8. you should now be able to use the proxy server

Installing python pip on Ubuntu 10.04 LTS

Just a notice; if you try to apt-get install pip, it will get the wrong package.
If you try to apt-get install python-pip, it will get a very old version of pip.
Best thing to do; download and install manually: http://pypi.python.org/pypi/pip

Ubuntu 11.04 does not have this problem.

 

**UPDATE**

Alternatively, use apt-get install python-pip
Then upgrade it:
pip install –upgrade pip
If you do pip –version, it will probably still show 0.3.1
apt-get puts pip into /usr/bin/pip, and upgrading adds the new version to /usr/local/bin/pip (if I remember correctly), so what you can do:
mv /usr/bin/pip /usr/bin/pip-0.3.1
pip –version again should show you 1.2.1, or whatever the latest version is

Creating custom packages and adding to apt-cacher

To create a custom package:
1. mkdir ~/debian; mkdir ~/debian/DEBIAN
2. vi ~/debian/DEBIAN/control
Add some info to this file:
Package: hello
Priority: optional
Section: devel
Maintainer: My Name myemail@domain.com
Architecture: i386
Version: 0.5
Description: Test package
3. pretend that ~/debian is the root, and put your files inside any subdirectories as appropriate
eg, if you have a script called myscript.sh that you want to put in /usr/bin, you want ~/debian/usr/bin/myscript.sh
4. cd ~
5. dpkg-deb –build debian
6. rename the debian.deb file to your packagename-versionnumber.etc.deb

To get this into apt-cacher, you should put your custom packages into a directory, then run this:
/usr/share/apt-cacher/apt-cacher-import.pl -r /your/directory/path