Archive for ‘CLI’ Category
Once in a while you might want to dig deep into OSX’s dhcp client and find out what dhcp packet the server sent it. A quick command to spit out this info:
Posted on 12:28, December 15th, 2007 by Many Ayromlou
Here are some notes for those of us working with Macs, who also have to deal with networking gear that usually have a serial console for local control. I recently purchased a IOGEAR USB 1.1 to Serial Converter Cable that uses the PL2303 chipset (pretty much any brand of these usb to serial converters use this chip). I was hoping that this dongle would allow me to access our network switches serial interface (which you kinda need when the switch has gone berserk). So after looking around the net I found that there is a opensource driver out there for it and that it performs better than the driver provided by Prolific (the company that manufactures the chip). So once everything was installed I hooked up the serial cable and realized that I needed a serial console program.
This turned out to be quite a pain since almost all of these programs for the mac are old and/or not free. Well, that was until I found out that our good friend screen can do this just fine:
This will open up a new screen session in your xterm/terminal and from there you can control your serial device. Once you’re done you need to issue the command Ctrl-a Ctrl-\ to get out of screen and back to your shell prompt.
OSX Webmaster special: Shared webserver, bad umask settings, group permissions and filenames with spaces…
Posted on 12:20, October 24th, 2007 by Many Ayromlou
Okay so this all started with our users not being able to share files on our webserver. We use SSH only for upload/download and interactive access (ie: no ftp). Through trial and error we found out that the default umask (under OSX Server) for sftp uploaded files are 0033 (ie: rwxr–r–) and directories are 0022 (ie: rwxr-xr-x). This creates a problem when one user uploads a file and another user downloads/modifies and tries to re-upload it — they simply can’t because the group permissions are wrong.
If we were using ftp (which we are not) there are some solutions on the net that allow you to modify the startup parameters for the ftp server so that the default umask for all files is 0013 — which would allow a group of people to share/overwrite each others files — but we are using ssh only.
So we came up with two other solutions — a shared upload account and/or a cron job that would modify the group permissions on the website directory to allow group sharing. We went with the second solution and that’s where I ran into so many problems that I decided to create this post. You see normally Unix users know that spaces (and strange characters) in filenames are a no-no. Well that’s not true for Windows and Mac users, they use spaces and other odd characters in their filenames/folders all the time.
I started writing — what I thought was — a simple “for loop” script to go through the website folder and change the group permissions. Of course on the first try things didn’t work nicely because of spaces, so I started compensating for that and came up with:
Finally after a latenight RTFM session (and lots of cursing), I think I’ve found the ultimate file handling loop statement:
After trimming and optimizing the script a bit, here is the final product:
Posted on 20:26, October 7th, 2007 by Many Ayromlou
So after hearing from people at work how great the “screen” command was (yeah welcome to gnuland boys and girls), I decided to do a short tutorial on screen. This way I can stop telling them to RTFM and instead tell them to RTFB (Blog). Anyways, What is “screen” first of all….From the pages of wikipedia:
Think of screen as a Virtual Machine (I know it’s not but bear with me). Once you run the command, the ‘virtual machine’ takes over and allows you to create multiple interactive command line sessions. In each of those sessions you can run commands that are either interactive (menu based) or serialized. Once you’re done you can disconnect the session — keeping in mind that the session is actually alive and running, including all the programs that were spawned inside that session — go to another computer and ‘restore’ the session with all the programs still running. By far one of the coolest things about screen is that it automatically allows you to nohup your commands, by just disconnecting the session and reconnecting to it later. So without any further due here is screen:
Obviously you need to run it, so first step is to type screen at the command line. When you do that you get a new shell window and the adventure starts. Remember that pretty much all screen commands start with Ctrl-a followed usually by a character (ie: you press Ctrl button and c together, let go, and follow it with the character).
So now you have a new shell, run a command (ie: pine, vi or something). Okay so now we can simulate you leaving your machine and detaching your session.
– To Detach : Ctrl-a d (this will detach the session but your command is still running inside that screens shell….you’ll see)
So now you’ve got the very basics of screen. Detaching allows you to run commands, leave them halfway, detach and go somewhere else and use Re-attach to restore the session.
Now, how about multiple sessions. Yeah you can do that too, one screen process with multiple sessions inside it.
– Use screen -r to reattach to your process (If you haven’t done so already). Note that your program is still running (say vi). If you now want to run lynx for example you can use the Ctrl-a c command to create another session (c for create). So now you have two sessions inside your “screen virtual machine”.
One last thing before I take away the training wheels, to kill your screen process (and all sessions running inside it) use Ctrl-a Ctrl-\.
Okay, so here is a small list of the many screen options and commands:
– Ctrl-a “ : gives you a full screen list of all your sessions and you can scroll down to the one you want to switch to and press Enter (remember to get “ you have to use Shift-‘ and ESC gets you out of the list).
Here are a couple of more useful startup screen commands:
– screen -ls : will list all the screen processes running under your userid (yes you can run multiple screen processes with multiple sessions inside each).
As usual screen is controlled via .screenrc file for configuration parameters (there is a system wide file in /etc/screenrc and the personal one in your home directory, under ~/.screenrc). You can add the following commands in your personal .screenrc to make life a bit simpler:
As usual there is a lot more to screen, so once you’ve got the basics nailed, take a peek at the man pages for more goodies and don’t forget…..Command line is your friend :-).
Posted on 18:41, August 4th, 2007 by Many Ayromlou
Here is a easy way to copy an entire directory tree from one Unix machine to another, while retaining the permisssions and ownership, using ssh as the middle man. Assuming that you want to copy everything under source_directory to destination_directory on another machine here is the command you would issue on the source machine (first cd to the directory containing source_directory):
One last command which I came across the other day…..If you’re ever in need of a stop watch just use your shell to measure time. Issue the following command and wait a bit. Now interrupt it via Ctrl-C and it will show you how much time has passed. NOTE: Nothing happens when you issue the command, only when you stop it via ctrl-c.
Posted on 23:18, July 28th, 2007 by Many Ayromlou
Okay so how many times have you “deleted” a file on a USB drive under OSX only to find out later that the storage is still tied up in .Trashes directory. Well there is a easy way to fix this. Open terminal, cd to your USB drives root directory (mounted under /Volumes) and issue the following:
Posted on 17:15, July 28th, 2007 by Many Ayromlou
Okay couple of quick ways to find the serial number of your mac.
Okay these are bash goodies, so they’ll work in any environment. If you’re in a situation where you’re switching between two different directory paths over and over again, here is a quick tip
Posted on 22:58, July 27th, 2007 by Many Ayromlou
This is another CLI command, so get your terminal ready. This gives you a quickway to check the DNS settings on OSX. Now one way is to just cat /etc/resolv.conf , but what if you wanted to see what the system is actually using (not just what it was configured for). Well scutil comes to rescue and gives us an interface to the “dynamic store” data maintained by configd. Here is the command:
Posted on 19:42, July 27th, 2007 by Many Ayromlou
New day, new command. dscl is the command in question. It gives you access to Mac OSX’s Directory Services Command Line interface. Very powerful stuff for those of us who like the command line and hate to do the same task a million times. A useful example is the ability to grant Administrator privileges to a user from command line. Normally you would have to pull up System Preferences/Accounts/Click User and check the “Allow user to administrate this computer” box. Well not anymore….Here is how:
There are probably a ton more things you can do with dscl, but that’s beyond the scope of this article (and my knowledge)…..so man dscl and have fun reading.