Showing posts with label UNIX. Show all posts
Showing posts with label UNIX. Show all posts

May 15, 2010

Reading plist files

When developing for the mac platform, app preferences are supposed to be stored in ~/Library/Preferences/.  While finding and reading a preference file is easy enough, reading usable information from it is another matter.  This is because in the last couple of system updates (10.5+) the xml based plist files used for preferences are now saved in a binary format, not the UTF-8 format you would suspect.  The tricky part of this is that if you are using the NSDictonary class in Objective-C you will not encounter any problems, but when using any other programing language you suddenly get a mess of garbage text.  Luckily OSX had a built in command to help remedy the issue.  The plutil command converts plists between the text based XML and binary based.

As an example in Java you can use the Runtime class to invoke the command on the fly.

Runtime.getRuntime().exec(new String[]{plutil", "-convert", "xml1", filepath});

To convert a file back once you have read or edited it use this.
Runtime.getRuntime().exec(new String[]{plutil", "-convert", "binary1", filepath});

It is also good check for the lastIndexOf("bplist") as it will alert you if the file is currently in binary form.

January 18, 2010

SSH Messenger


As a request, I have finished my second full Objective C project. If someone is on a computer wearing headphones and is not in convenient shouting range this app makes it simple to still get their attention.  It would also make a good admin tool, or a awesome way to prank/annoy someone. It is a simple application that will connect to a remote computer via SSH and then use the built in osascript libraries to cause a dialog prompt to appear. However instead of requiring some terminal skills, instead just enter the IP, username and password of the remote computer and then type your message. A handy shell script takes care of all the rest. Click here to check it out. (Universal Binary, Mac OS X 10.5+)

This was an interesting project, not so much on the Objective C side, which I am getting quite good at, instead the shell scripting was a challenge this time. In order to have the SSH command work without setting up key pairs, I needed to delve into the world of Expect. Expect is a handy command set that allows you to set up automation of terminal entry. In short you can have it "expect" some input and then after finding it, send some output to the terminal, which in my case was the password entry for ssh. The ssh command does not have a password argument, so it needed expect to look for the password prompt and then enter a password for the user.  It also led to a nice way to do some general result checking in the case of a prompt with two or more button options.

Expect while a little strange at first was not that hard to figure out and only took about an hour and a half to have down pat and working the way I wanted it to.  Sadly as it is based out of /usr/bin/expect you cannot use echo command, which made it slightly harder to learn when something was not working.

Check out the base script I came up with below...

#!/usr/bin/expect -f
#log_user 0
set addr [lindex $argv 0]
set usr [lindex $argv 1]
set pas [lindex $argv 2]
set message [lindex $argv 3]
set from [lindex $argv 4]
# now connect to remote UNIX box (addr) with given script to execute
spawn ssh $usr@$addr -o StrictHostKeyChecking=no
match_max 100000
# Look for any ssh issue that needs exit
set timeout 4
expect "ssh:" {exit 2}
# Look for password prompt(s)
expect "*?assword:*" {send "$pas\r"}
# Look for password rejection and exit
expect "*?assword:*" {exit 1}
set timeout 10
# send osascript commands for popup
send "osascript -e 'tell application \"Finder\" to activate'\r"
#send "osascript -e 'tell application \"Finder\" to display dialog \"$message\"'\r"
send "osascript -e 'tell app \"Finder\" to display dialog \"$message\" buttons \"Ok\" default button 1 with title \"Message From $from\" with icon caution'\r"
# Look for reply
expect "button returned:Ok" {
 send "logout\r"
 exit 0
}
send "logout\r"
exit;

Another important thing to note is that I was rather annoyed to find out it required the curly braces to be placed how they are, due to it being based on Tcl. However it was only a minor inconvenience until I realized that is why if kept having errors.  The script above takes 5 arguments Address, User, Password, Message and From, and is the general Alert script used in the SSH Messenger.

This makes the first Objective C and Shell combo app that I have done, which is a nice change from the Java Shell combo I am more used to working with.  That and it was a good refresher in shell scripting.

September 30, 2009

Tomcat install on OS X 10.4

I had seen Tomcat on job applications before, however this was the first time I had actually played with it. What is it? "Apache Tomcat is an open source software implementation of the Java Servlet and JavaServer Pages technologies. The Java Servlet and JavaServer Pages specifications are developed under the Java Community Process." So it allows you to run Servlets and JSP on a server. Now I needed to find out how to install it on my Lazurus server running OS X 10.4.11 with the latest Java update.

1. First download tomcat from HERE (I used 5.5, so not sure about 6)

2. Then rename the unarchived tomcat folder to "Tomcat"

3. Drop the folder into /Library/

4. Fix the UNIX permissions recursively to 777 (BatchMod is a simple way if you do not know chmod, just check all of the boxes)

5. Edit the /Library/Tomcat/conf/tomcat-users.xml file, see below for example.


<?xml version='1.0' encoding='utf-8'?>
<tomcat-users>
<role rolename="tomcat"/>
<role rolename="role1"/>
<role rolename="manager"/>
<role rolename="admin"/>
<user username="tomcat" password="tomcat" roles="tomcat,admin,manager"/>
<user username="both" password="tomcat" roles="tomcat,role1"/>
<user username="role1" password="tomcat" roles="role1"/>
</tomcat-users>


6. Start tomcat by using the terminal to run "cd /Library/Tomcat/bin" and then "./startup.sh"

7. In a web browser head to http://127.0.0.1:8080/ and you should see the Tomcat start up page.

If you want to read on further I figured out most of my instructions from the Tomcat Wiki, so head there for more info or if you get stuck.

Just in case you where wondering, as I was, put your jsp and servlet files in the /Library/Tomcat/webaps/ folder. The webaps/ROOT folder being the one displayed at the base url address. So for now I am just using that folder for testing.

JSP is quite simple to pickup at a basic level if you already know Java. However there are a few differences do to it being web based, but so far I like not having to compile manually. I had no problem writing a few simple classes with outputs and will later play with post and get. HAHA, tri-fecto of web programing achieved (PHP, ASP, JSP), hm still need to sit down with Perl one of these days.

January 28, 2009

Easy Symbolic Links in OSX

From time to time you have to rely on symbolic links when Alias is not enough, such as re-mapping folders like your itunes library. You can always open terminal and use "ln -s target destination", however this can be annoying if you do not do it often or need one fast. So there is SymbolicLinker to help out, it is a handy context menu add on that give you a "create link" option when you right click an icon. It is handy to have for that random time you quickly and easily need a symbolic link.

July 20, 2008

iPod Touch 2.0 & Jailbreak

Well the 2.0 firmware has been out for a while now, however I was holding off getting it till the iPhone Dev Team finished their new Jail break tool. As of last night that is finaly available, and I quickly grabed it after seeing comments of not too many people with trouble. Sadly this was not the case for me, I finaly got it working, but it took a bit (aka 3hours) of poking.

UPDATE* There is a newer version out, that hopefully has fixed the problems I had.

NOTE: As with all hack, you assume responsibly if something goes wrong and bricks your touch!

First off grab the 2.0 firmware from apple, yes there are other less costly ways, but I can't help you with that. Then grab the pwnage tool (link above). The firmware will take a while to download, so go get a snack... Ok when that is finished run the tool, it will ask you to click the device, then it will find the firmware you downloaded from apple, other wise it will complain and require you to find it. Then it will build a custom firmware for you to install, yes you will need to restore it again later when it is finished. This also take a few minutes, but has a cool pagkaging animation. Please note that you can stop it from putting the pineapple logo through the expert options, which I did because I like the apple logo better.

Here is where i started to have problems, had my touch pluged in, and the next screen finaly shows up with a failed DFU restore, I go back to the site and low and behold...
"Update 3: If you get Error 1600 from iTunes (or if you see in your log a failure to prepare x12220000_4_Recovery.ipsw), try: mkdir ~/Library/iTunes/”Device Support” ; if that directory already exists, remove any files in it. Then re-run PwnageTool. "
So I add that directory, but still no luck, finaly I figured out that I was in restore mode, not DFU mode, the diffrence being that you hold down the home button on restart to go into restore, but for DFU you hold both power and home till it shut off, then keep holding home till the screen goes black, but iTunes will still pop up a message saying you have done restore, that is the key piece of infommation that was missing. Finaly I ran the tool again and this time instead of saying failed it led me through the DFU steps, though I was already in that mode, then told me to used the custom firmware, that was placed on the desktop, to restore the iPod through itunes. You [option] click restore and locate the custom firmware on the desktop, and let it take its time restoring the jailbroken firmware.

Also as a hint, if you are getting a 1600 error with the custom firmware, try redoing the pwnage tool, but this time use expert mode and actualy click the firmware located in ~/Library/iTunes/iPod Software Updates.

Finaly I had it all up and running however there are still a few things I am missing from the pre 2.0 days.
Update... Everything I wanted is now back and available in 2.0!


Things Fixed or Updated...


  • Winterboard gives you themes again... Avoid Customize 2.0 it has too many issues right now

  • BossPrefs has fixed the Mobile Safari download plugin.

  • More apps are avalable between the App Store and Cydia now.

  • I have grown to like Cydia now that it has had some tme to grow, will not be switching to installer 4

  • The 5 icon dock is back, and works with icon dragging!



The root password is still alpine, so it is a good idea to change it. Terminal or ssh...

login root
password: alpine
passwd root
new password: *****
confirm new password: *****



So my oppinion, the app store is worth updating for, especially as more and more is added. I was happy that I no longer need to install email, maps...etc seprate, which saves me time and iTunes now syncs email and bookmarks, (Is this new? It is to me! Maybe I missed it in the last itunes update). Terminal, makeitmine and OpenSSH are already ported and ready to go, and AIM and VNC now are in the app store so I am all set with that. The remote app is also very cool! Even Cydia has grown on me as they have updated it, I don't think I can live without the changes list now, so I do not plan on updating to Installer 4, unless there is something I can only get on it. Also WInterboard is final out, and blows away Customize 2.0 in simpliciy and ease of use when applying themes on the 2.0 fw. I have my theme back now so I am very happy. If you have not updated yor iPod Touch or iPhone yet, now is the perfect time to do so.

April 9, 2008

Download files from terminal

I had come across a mp3 file online that would only load half way, or so I though. Apparently it wold load the whole thing but Safari thought that it was bigger than that, so it would never give me the option to download it. Frustrated after having it load a few different times I looked for a better solution, the Terminal! I figured that there must be some way to download file from terminal. I knew of wget, however that is not installed by default in 10.4, and I did not feel like downloading and installing the fink packages. Instead I found the cURL command...


curl -o "address"


The -o is for file output, see the curl manpage for more info.

February 7, 2008

Setting up web shares in Apache

One thing I wanted is the ability to share directories online other then having to make folders in my WebServ folder. So off I went to look up how to configure Apache to do this. As I expected this was not all that hard to do, you just have to add a few lines to the apache config file.

Just type " sudo vi /etc/httpd/httpd.conf " or use what ever editor you like, I like VI. then look for the section below...


#
# Aliases: Add here as many aliases as you need (with no limit). The format is
# Alias fakename realname
#
<IfModule mod_alias.c>


Find place you like to put the info for your new shared directory, and add the lines below. The #"stuff" lines are optional, but it is a good idea to comment about what you are adding.


#Share via online
Alias /NAME OF SHARE "/PATH TO DIRECTORY/"

<Directory "/PATH TO DIRECTORY/">
Options Indexes FolowSymLinks MultViews ExecCGI
Order allow,deny
Allow from all
</Directory>


This will set up a new directory that you can access with http:/serveraddress/name of share. This worked quite well, however I still wanted to take it a step further. I could make a php index page that prevented access, however that would not give me quite the level of security that I wanted. Instead I looked for a way to secure a directory with Apache. First of you need to change the directory set up by setting it to...


<Directory "/PATH TO DIRECTORY/">
Options Indexes MultiViews (+ other things you want)
AllowOverride All
</Directory>


This make it look for a .htaccess file that specifies the access rules for the directory. You will now need to add this file to the directory you are sharing. In that file you need to add these lines.


AuthName "Name of Stuff Share"
AuthType Basic
AuthUserFile /PATH TO PASSWORD DIRECTORY/.htpasswd
AuthGroupFile /dev/null
require user ADD USERNAME


This sets up the directory, the last thing you need to to add a password file for it to reference to.
To do this, cd to the password directory you used above. Then type htpasswd -c .htpasswd "USER NAME" this will add a password file to the directory for the use name.

There you have it a new directory in Apache which requires a password to access.

December 23, 2007

Running Lejos on Mac OS X


One of the classes I took last semester was Artificial Intelligence, and after a long and horrible 9 weeks of prolog bootcamp, we finally hit the robotics part of the class. There was only one problem, we where using Lejos, which is a java based programing language for the Lego NXT bricks. This was fine because I am quite proficient with Java, the problem was that Lejos does not have native Mac OS support, and all of the forums that relate to it only give partial help or promises of support eventually. This is not good enough so I set off on an adventure to get it running on my MacBook Pro.

The first thing I did was to get the latest update to Lejos, big mistake, the linux Lejos 0.4.0beta would not work for me. I finally got it to force build, but this was only enough to get a broken firmware on the brick. Realizing this I went back to the 0.2.0alpha that we where using in class and this compiled fine. You just download it from the Lejos Site and unzip it. Place the entire lejos_nxj folder in the Applications folder. Then open terminal and cd to the build directory aka /Applications/lejos_nxj/build. Then type and run ANT and it will make a build for the Mac OS based on the build.xml file in the build folder.

The problem I ran into is that in terminal you have to ether set up a tcsh shell with the environmental variables set or set them in a .profile file in your home folder. Both of these worked but where annoying and took quite a bit of research on my part to figure out what needed to be done. Also you then need to use a text editor and run the build, compile and link command in the tcsh shell on the .java file each time you run it. Sadly this was no better than the PC solution, which was essentially doing the same things through Eclipse and batch files.

In lab I had done the basic PC Eclipse Setup, but quite frankly the whole button setup made me want to scream, not that was was that hard, but instead I found it very pointless for all of that work that was required. So after I got Lejos working on my Mac I started to write a small application that would let me code and then run the code in one simple step, and at the same time make the PC users in my class envious of its simplicity.

The final result I was quite proud of, and only took about a week of using/upgrading, plus a bit of unix scripting on the back end to get it to the point where I was happy enough to just bring my laptop to lab and use it instead. Also now that the class is over I spent a little time to clean it up a bit more and make it more usable for people other than me. If you are looking for a quick solution or are planing on doing something similar I have uploaded my application, which I am calling LejosTools. Get it Here I hope it can be as helpful to someone else as it was for me. Also I have included the source with it, because I have no plans to continue development. All I ask is if someone uses the code send some credit my way as a developer. Otherwise enjoy...

As a side note, I have not tested other versions of Lejos, but from what I have seen the unix commands have not changed, so if you can get something greater then the 0.2.0alpha working, LejosTools should still work. If they do change just update the lejosfull sh script in the resources folder.