Archive

Archive for September, 2012

Connecting to an IRC server behind a firewall? I feel your pain!

September 29, 2012 Leave a comment

So I use Xchat daily and connect to a private IRC server to talk with my colleagues. I also have a BIP server in the office to record all of the IRC transcripts, this way I never miss any conversations regardless of the time of day. Because the BIP server is behind a firewall on the companies network I can’t access it from the outside.  For the past year I’ve been working around this by connecting to my companies firewall via ssh and creating a SOCKS tunnel then simply directing xchat to talk through my local SOCKS proxy.

To do this ,  open a terminal and issue:

ssh -CND <LOCAL_IP_ADDRESS>:<PORT> <USER>@<SSH HOST>

Ex: ssh -CND 192.168.1.44:9999 sfeole@companyfirewall.com

Starting ssh with -CND:

‘D’ Specifies a local “dynamic” application-level port forwarding. This works by allocating a socket to listen to port on the local side, optionally bound to the specified bind_address. It also adds compression to the datastream ‘C’ and the ‘N’ is a safeguard which protects the user from executing remote commands.

192.168.1.44 is my  IPv4 address

9999 is the local port i’m going to open and direct traffic through

After the SSH tunnel is open I now need to launch xchat, navigate to Settings -> Preferences -> Network Setup and configure xchat to use my local IP (192.168.1.44) and local port (9999) then press OK then Reconnect.

I should now be able to connect to the IRC server behind the firewall. Usually I run through this process a few times a day, so it becomes somewhat of a tedious annoyance after a while.

Recently I finished a cool python3 script that does all of this in quick command.

The following code will do the following:

1.) identify the ipv4 address of the interface device you specify

2.) configure xchat.conf to use the new ipv4 address and port specified by the user

3.) open the ssh tunnel using the SSH -CND command from above

4.) launch xchat and connect to your server (assuming you have it set to auto connect)

To use it simply run

$./xchat.py -i <interface> -p <port>

ex: $./xchat.py -i wlan0 -p 9999

the user can select wlan0 or eth0 and of course their desired port. When your done with the tunnel simply issue <Ctrl-C> to kill it and wala!

https://code.launchpad.net/~sfeole/+junk/xchat

#!/usr/bin/env python3
#Sean Feole 2012,
#
#xchat proxy wrapper, for those of you that are constantly on the go:
#   --------------  What does it do? ------------------
# Creates a SSH Tunnel to Proxy through and updates your xchat config
# so that the user does not need to muddle with program settings

import signal
import shutil
import sys
import subprocess
import argparse
import re
import time

proxyhost = "myhost.company.com"
proxyuser = "sfeole"
localusername = "sfeole"

def get_net_info(interface):
    """
    Obtains your IPv4 address
    """

    myaddress = subprocess.getoutput("/sbin/ifconfig %s" % interface)\
                .split("\n")[1].split()[1][5:]
    if myaddress == "CAST":
        print ("Please Confirm that your Network Device is Configured")
        sys.exit()
    else:
        return (myaddress)

def configure_xchat_config(Proxy_ipaddress, Proxy_port):
    """
    Reads your current xchat.conf and creates a new one in /tmp
    """

    in_file = open("/home/%s/.xchat2/xchat.conf" % localusername, "r")
    output_file = open("/tmp/xchat.conf", "w")
    for line in in_file.readlines():
        line = re.sub(r'net_proxy_host.+', 'net_proxy_host = %s'
                 % Proxy_ipaddress, line)
        line = re.sub(r'net_proxy_port.+', 'net_proxy_port = %s'
                 % Proxy_port, line)
        output_file.write(line)
    output_file.close()
    in_file.close()
    shutil.copy("/tmp/xchat.conf", "/home/%s/.xchat2/xchat.conf"
                 % localusername)

def ssh_proxy(ProxyAddress, ProxyPort, ProxyUser, ProxyHost):
    """
    Create SSH Tunnel and Launch Xchat
    """

    ssh_address = "%s:%i" % (ProxyAddress, ProxyPort)
    user_string = "%s@%s" % (ProxyUser, ProxyHost)
    ssh_open = subprocess.Popen(["/usr/bin/ssh", "-CND", ssh_address,
                 user_string], stdout=subprocess.PIPE, stdin=subprocess.PIPE)

    time.sleep(1)
    print ("")
    print ("Kill this tunnel with Ctrl-C")
    time.sleep(2)
    subprocess.call("xchat")
    stat = ssh_open.poll()
    while stat is None:
        stat = ssh_open.poll()

def main():
    """
    Core Code
    """

    parser = argparse.ArgumentParser()
    parser.add_argument('-i', '--interface',
                        help="Select the interface you wish to use",
                        choices=['eth0', 'wlan0'],
                        required=True)
    parser.add_argument('-p', '--port',
                        help="Select the internal port you wish to bind to",
                        required=True, type=int)
    args = parser.parse_args()

    proxyip = (get_net_info("%s" % args.interface))
    configure_xchat_config(proxyip, args.port)
    print (proxyip, args.port, proxyuser, proxyhost)

    ssh_proxy(proxyip, args.port, proxyuser, proxyhost)

if __name__ == "__main__":
    sys.exit(main())

Refer to the launchpad address above for more info.

Simple Python3 Code to parse your ipaddress

September 27, 2012 Leave a comment

I’ve been doing some side projects on my own requiring me to obtain my assigned IPv4 address.  In python3 you can do this by importing socket, which I believe is a better way than how I’m doing it below, however I found that using subprocess solved the issue!

I just put this together tonight and it does exactly what I wanted it to do. Feel free to copy and use it for your own little projects, you will need to add additional logic to the parser for additional network interfaces.

#!/usr/bin/env python3

import sys
import subprocess
import argparse

def get_net_info(interface):
    myaddress = subprocess.getoutput("/sbin/ifconfig %s" % interface)\
                .split("\n")[1].split()[1][5:]
    if myaddress == "CAST":
        print ("Please Confirm that your Network Device is Configured")
        sys.exit()
    else:
        return (myaddress)

def main():
#Parser Code
    parser = argparse.ArgumentParser()
    parser.add_argument('-i', '--interface',
                        help="Select the interface you wish to use",
                        choices=['eth0', 'wlan0'],
                        required=True)
    args = parser.parse_args()

#    print ("%s" % args.interface)
    print (get_net_info("%s" % args.interface))

if __name__ == "__main__":
    sys.exit(main())

Heres the expected output below, in the event the device in question is NOT configured (unplugged/unplumbed), the following should output:

sfeole@sfmadmax:~/pythonscript$ ./sean3.py -i eth0
Please Confirm that your Network Device is Configured

And here is the expected output when the device is configured(plugged in and plumbed)

sfeole@sfmadmax:~/pythonscript$ ./sean3.py -i wlan0
192.168.1.44

And of course , invalid arguements…

sfeole@sfmadmax:~/pythonscript$ ./sean3.py -i www
usage: sean3.py [-h] -i {eth0,wlan0}
sean3.py: error: argument -i/–interface: invalid choice: ‘www’ (choose from ‘eth0’, ‘wlan0’)

Ubuntu 12.10 Amazon Search, HOW TO REMOVE

September 24, 2012 1 comment

So it’s been quite a long time since my last post. Frankly I’ve been busy with work/personal life, spending time with my little girl and catching up on random chores in and out of the house. Everyone can relate I’m sure.

Well , so new info!! Some of you know that Ubuntu 12.10 Quantal Quetzal release is right around the corner. I’ve been using it since Beta1 and I have to say it’s coming along nicely. A new feature that has some users in an uproar is the Amazon Search feature in Dash. Whenever searching for new items using the Search Lens the system will now poll results from Amazon.com.

Here’s an example:

I search for Music and note the results from Amazon which populate in as I wait…

To Remove this, Open a Terminal Window <Ctrl-Alt-T>

$sudo apt-get remove unity-lens-shopping

Logout and LogBack then search for MUSIC again

All Fixed.

Hope this helps!! and Enjoy the new 12.10 Features!<