You can determine the name of a user who is logged in using a number of different methods. From the command line there are two commands that most commonly get used for this: logname and whoami. Each can be used without any arguments to return the name of the user running the command, a great way to debug scripts, or grab the name of a user. To use logname:
To use whoami:
The ftp command that runs on a Mac is similar to that from any other platform, including Windows – and not much has changed with regard to FTP for a long, long time. When using FTP you will login to an FTP server, then issue some commands, one of which will kill your session to the host. The commands you issue during an FTP session are issued in an interactive mode of the shell, where you are actually running them against the target server
- ls – list the contents of a directory on the FTP server
- cd – change the working directory on the FTP server
- pwd – show the current directory on the FTP server
- get – download files from the FTP server
- put – upload files to the FTP server
- account – include a password with your login information
- bye – terminate an ftp session and close ftp (or use disconnect to simply terminate a session)
- bell – make a cute sound after each file transfer is done
- chmod – change permissions
- delete – your guess is as good as mine (OK, you got me, it’s to delete a file off the server)
- glob – enable globbing
- hash – only functional in Amsterdam
- help – get help
- lpwd – print the local working directory for transfers
- mkdir – create folders on the FTP server
- rmdir – delete folders from the FTP server
- newer – only get a file if it’s newer (great for scripting synchronizations)
- nmap – use positional parameters to set filenames
- passive – use FTP passive mode
- prompt – allows the use of letters to automate answers to prompts
- rate – limit the speed of an upload or download
Notice the similarity between FTP commands and Mac OS X commands! This continues into repetition: in Mac OS X you can build commands to run automatically when you open a new shell. Using a netrc file you can do the same with opening an FTP session. The ~/.netrc file (or just netrc) is a text file that, like .bash_profile, lives in your home directory (aka – ~). The .netrc file allows FTP to perform automatic logins to FTP servers based on the name (not that there aren’t subcommands to do the same, but it allows you not to insert the password into your history or script).
Like .bash_profile, the .netrc will need to be created per user, which can be done with the following:
Because you’re going to put password information in there, let’s also restrict who can look at it:
chmod 700 .netrc
Now open your shiny new .netrc file and create a block of settings for each FTP
server you want to automate access for. This information will be the ftp server (machine), the username (login) and the password (yup, that’s the password). For example, the entire file could be:
You can also connect to a server using the following format with each command with each command:
Now that you can login without issue, let’s write a script to perform some routine tasks, such as keeping a web site up-to-date.
ftp -d krypted.com << ftpEnd
Or Downloading documents off of a website:
ftp -d krypted.com << ftpEnd
There are also some variables that you can use in the prompt:
- %/ – the current working directory of the FTP server
- %M – the hostname of the FTP server
- %m – the hostname only up to the .
- %n – the username used for the FTP server
Finally, you can also define macro’s in your .netrc file using macdef.
Forgot the admin password in Mac OS X? Well, Apple let’s you boot computers into what is known as Single User Mode. To boot a Mac into Single User Mode, boot the machine holding down Command-S. Once the system boots up, you should see a command prompt. Here, run fsck:
Then mount the file system:
mount -uw /
Then reset the password using the passed command
For example, if the user is root:
When prompted, provide the desired administrative password.
Best log analyzer ever. For centralizing and reviewing logs on lots of servers it’s a must have.
To list the files in a directory with the permissions for files, use the following command (assuming you’re in the working directory you would like to list files for:
The cron command has officially been deprecated in Mac OS X, but still functions if called upon. cron starts a process that executes commands at specified dates and times. Regularly scheduled commands can be specified according to instructions found in the crontab files in the directory /var/spool/cron/crontabs. Users can submit their own crontab files via the crontab command. Crontab copies the specified file or standard input if no file is specified, into a directory that holds all users’ crontabs. crontab options:
- The -e option edits a copy of the current users’ crontab file or creates an empty file to edit if crontab does not exist.
- The-r option removes a user’s crontab from the crontab directory.
- The -l options lists the crontab file for the invoking user.
A crontab file consists of lines of six fields each.The fields are separated by spaces or tabs. The first five are integers that specify the following (in order):
- minute (0-59)
- hour (0-23)
- day of the month (1-31)
- month of the year (1-12)
- day of the week (0-6 with 0=Sunday)
Each of these patterns may be either an asterisk (meaning all valid values) or a list of elements separated by commas. An element is either a number or two numbers separated by a minus sign ( meaning an inclusive range). Notice the time is in 24 hour format, 0 is midnight and 13 is one in the afternoon. The * wildcard can be used to run on every instance of a given object.
The sixth field of a line in a crontab file is a string to be executed by the shell at the specified times by the first fife fields. A percent character in this field (unless escaped by ) is translated to a newline character. Only the first line (up to a % or end of line) of the command field is executed by the shell. The other lines are made available to the command as standard input.
Any line beginning with a # is a comment and is ignored.
When I say working directory I am referring to the current directory you are in when using a command prompt. Just an FYI. If you are ever unsure as to what your working directory is then you can use the pwd command to print it out to the screen.
Someone asked me the other day how to setup OS X to log sudo events. Well, there’s nothing to do. Whether the password used with sudo is right or wrong, all attempts to authenticate through sudo are logged.
My Next Cube, which is about 15 years old has died I am sad to report. I will miss it and the witty banter we once shared. 🙁