Example of expect with ftp & telnet

Posted on June 7th, 2011 by GhettoBSD  |  No Comments »

In the past I’ve had the need to use expect, but it wasn’t exactly easy to get going, so I went back to something I was more familiar with. Eventually I had another need and finally got it going. Here are two examples of how to use expect.

Access an FTP server to upload a file. This is a daily job that was setup as follows:

#!/bin/sh

uname= h39dj31

passwd= jd83812

ip=172.16.247.36

# Opens ftp with auto-login turned off, and take input from the program until it sees EOF.

ftp -n << EOF

# Connects to IP address specified.

open $ip

# Turns passive mode off.

passive off

# Sends the user name and password in plain text!

user $uname $passwd

# ls’ a directory (you should be logged in by now.)

ls

# cd’s to specified directory.

cd daily-reports

# Uploads daily report.

send daily-report.csv

# Terminates ftp script/session.

EOF

Here’s the same script but using expect instead:

#!/bin/sh

uname= h39dj31

passwd= jd83812

ip=172.16.247.36

# Launches expect.

expect << EOF

# Starts ftp.

spawn ftp

# Connects to ftp server.

send “open $ip\r”

# Tells the program what to expect.”

expect :

# Sends the username.

send $uname\r

expect Password:

# Sends the password.

send $passwd\r

expect ftp>

# Sends a command.

send ls\r

expect ftp>

send “cd daily-reports\r”

expect ftp>

# Uploads daily report.

send daily-report.csv

send EOF\r

Here’s another example using expect, this is to connect to all cisco devices and get some info:

#!/bin/sh

uname= h39dj31

passwd= jd83812

ip=172.16.124.37

expect << EOF

spawn telnet $ip

expect Username:

send $uname\r

expect Password:

send $passwd\r

expect #

send “show version\r”

expect #

send quit\r

Some notes:

-          You need to add the return character “\r” at the end of each command you send in expect.

-          You only need to use double quotes “” in expect when you have a space in what’s send. For example, if you send ls you don’t need double quotes, but if you send “rm filename” then that will need to have double quotes.

Good luck!

10+ years and still going

Posted on April 20th, 2011 by GhettoBSD  |  No Comments »

GhettoBSD.org is now 10 years old, and boy have things changed. Originally my first experience with FreeBSD was in the late 90′s. I ran it on junk hardware and that is how I ended getting and naming my systems GhettoBSD. BSD has been such a great asset in my life in both personal and professional functions. From setting up my first website/mail/DNS server to writing programs that do 3 weeks of work in about 5 minutes. One of the most useful programs I’ve written was to backup my audio CDs. Something that would have taken weeks or months was done in about a week. Most think and would be correct that opening the cd/dvd drive, inserting the disc, closing it, ripping it, ejecting the CD and starting over would take a lot of time. But when all you do is write a small script and let it run, this is heaven compared to what you’d have to do otherwise. Thanks to those examples and many, many more, BSD has been something that I’ve recommended to countless people. More recently I’ve been using PC-BSD. I first tried them out early in their development and liked what they were aiming for, but always had some issue. I recently (more than 6 months ago) checked in to see how they were doing and wow, what a difference! I run their system on a computer at work and am loving most of it. Sure I have some issues with it, but it was the same with Win7. But with BSD as my workstation I now have the added benefits of everything else that comes along with it. And more importantly, I can test out my programs on it before implementing them on my server.

Almost at the same time, GhettoBSD.org has completed 1 year of uptime. That might not sound like a big deal to most people, but it is for me. I knew it was possible, but when you’ve dealt with junk only, it was almost impossible. As stated in the about section, the junk that BSD ran on wasn’t too unreliable, but whether something went bad or the power went out or even the machines over heated, I never reached one year of uptime. I would be up against my friends on Win98 machines who would also run into the same problems. For the most part, the Win machine won! It was a Win98 SE that wasn’t used for anything, just left idle while my BSD’s would keep on chugging along with many websites, services and a few users.

But now with backup power, very nice low power consuming hardware and a little TLC I’ve easily reached 1 year. What’s next? 2 years – as long as the hardware holds up!

The one thing I try to tell everyone about, is the ports collection. It is simply one of the most useful things that can come on a system `by default.` (It can be installed or not at first install or later on). Many people don’t know about it, and more importantly, many in the linux world. The ports collection is a system of categorized directories that contain a lot of commonly used programs. For example, in /usr/ports/irc you can find many IRC and IRC related programs such as Irssi and BitchX. To install, you just cd /usr/ports/irc/irssi and make install clean. When that’s done, you should be able to rehash and fire up Irssi just like that.

I’ve tried many different systems over the years and have to say that for me, nothing compares to the BSDs. My favorite linux distribution is Suse, though I have tried RedHat in different times, as well as slackware, debian, mandrake and even ubuntu once, just to name a few. They’ve made real advances with their .deb and .rpm’s, but nothing compares to the ports collection. It’s also worth mentioning that the ports collection is not the only way to install programs (they’re compiled from source), you can also use pkg_add which downloads and runs pre-compiled binaries. Thus avoiding having to compile programs on your local machine.

Interestingly enough, PC-BSD is coming out with a system to rival the .exe’s of the Windows world. They’re pre-packaged ports in binaries that install on your system. What makes their way of going about it great is they use a sort of market to deploy them in. Need a graphical editor? Go to their graphics section. Word processing or editing? Head on over to that area. This is a major stride in useability because many people are scared of console (my primary way of working on BSDs). So maybe someday, that could be the default in the BSD OS’.

In the end, whatever works best for you and your situation is the best solution for you. From the beginning, FreeBSD just worked for me (no kernel panics like with RedHat lol). And throughout the years its proven to be just as reliable as they claim, Rock Solid. So head on over to FreeBSD or OpenBSD and give it a try. And with NetBSD, you just never know what you could end up putting it or FINDING it running on.

Setup a tunnel in BSD for Firefox

Posted on December 30th, 2010 by GhettoBSD  |  No Comments »

There are lots of reasons to set up tunnels; mine is to avoid having to click “are you sure you want to proceed to this website” every time I go to a legit site. As a bonus, everything else is allowed as well!

1) Establishing the tunnel to the outside in a terminal.

You want to use the following to connect to your filter free server:

  • ssh -D local-port usernam@ip-or-domain-name -p 00000

The -D is used to specify a dynamic application-level port forwarding. This is the port on your local BSD machine that will be used for passing information through to and from the tunnel.

username@bsd-machine is self explanatory

-p 00000 is a non-default ssh port on the target BSD system. Why would this not be port 22? If you want to make it a little harder for people who don’t know what they’re doing.

So, a working example of this might look like: ssh -D 5923 wizard@pcbsd.org -p 2222

Once you have your tunnel set up, you’re ready to configure firefox!

2) Configure Firefox to use the proxy.

In my current version of Firefox, I go to: Edit -> Preferences -> Advanced -> network -> “Configure how Firefox connects to the Internet” Settings

Now in Connection Settings, you want to select “Manual proxy configuration:”

Go down to SOCKS Host: and enter 127.0.0.1, and for port enter the local -D port you chose earlier, 5923 in  our example. It should look like this: SOCKS Host: 127.0.0.1 Port: 5923

Hit OK, then Close and test it out! You can go to: www.whatsmyip.org to see what IP address you are using! If you set it up properly, you should see your remote BSDs IP.

3) Additional options.

Usually, that’s where most others will end.

Here are some other things you can do with your tunnel and Firefox.

In your tunnel, you can add these flags

  • -f Requests ssh to go to background just before command execution.
  • -N Do not execute a remote command.

So now your command could look like: ssh -f -N -D 5923 wizard@pcbsd.org -p 2222

This is good if you just want to establish the tunnel and forget about it. Without -f & -N, you can actively use the remote BSD machine while establishing a tunnel at the same time. So why not just use one or the other? Well, for example, maybe I have my ‘remote user’ for remote shell login, and I also have a ‘tunnel user’ just for establishing tunnels. This could be for ‘security’ reasons.

If you’re like me, you might need access to local resources. So, say for example, I want to connect to pcbsd.org without having to click “are you sure you want to proceed to this website.” I establish the tunnel and configure Firefox. Later on I want to connect to 192.168.1.35. I will be able to pull up pcbsd.org, but not 192.168.1.35 because that is on a local network, and my home connection isn’t routed back to my local network.

To fix this, go back to Edit -> Preferences -> Advanced -> network -> “Configure how Firefox connects to the Internet” Settings. Towards the bottom there is a field named: “No proxy for:” and it will always have the default populated: “localhost, 127.0.0.1.” So, whatever local address you need, such as 192.168.1.35, you add it there. It should look like this: “localhost, 127.0.0.1, 192.168.1.35″ Now, click ok, close and test it out!

You can specify specific IPs or blocks/ranges of IPs in that field should you need access to MANY IPs (as I do).

Have fun!