Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

A shell script to get machine serial numbers UNIX
I needed a way to get machine serial numbers on my network and that feature is not in Remote Desktop, so I decided to do it my own way. See the shell script I created below. You must have FTP service turned on on a machine that the other Macs can hit. Edit the $FTP_ADDRESS, $FTP_LOGIN, $FTP_PASSWORD, and $FTP_PATH variables and you're done. You can run the script remotely via telnet/ssh, or you can make a package and have this script run as the pre /post install script. Then you can send and run it via Remote Desktop. This has been tested with OS X 10.2.x with the default C shell.

Read the rest of the hint for the script...

#! /bin/tcsh
set FTP_ADDRESS=<ftp address>
set FTP_LOGIN=<ftp server login>
set FTP_PASSWORD=<ftp server password>
set FTP_PATH=<path to the folder you want the text file stored>
echo "Changing to home directory..."
cd ~
echo "Setting hostname variable..."
set MAC_NAME=$HOST
echo "Creating text file..."
touch $MAC_NAME.txt
echo "Writing hostname to file..."
echo $MAC_NAME > ~/$MAC_NAME.txt
echo "Writing return carriage to file..."
echo "" >> ~/$MAC_NAME.txt
echo "Writing grepped serial number to file..."
system_profiler | grep -i "serial number" >> ~/$MAC_NAME.txt
echo "FTPing the file..."
curl -T ~/$MAC_NAME.txt -u $FTP_LOGIN:$FTP_PASSWORD ftp://$FTP_ADDRESS/$FTP_PATH
echo "Removing original text file..."
rm ~/$MAC_NAME.txt
echo "Complete!"
This can be prettied up, so any changes are welcome. It basically creates a text file on the machine it's running on, names it after the machine's hostname, puts the machine's hostname in the file, and greps the output of system_profiler for anything with "serial number" in the line and throws it in the text file as well. Then it FTPs that file to a server you specify and deletes the text file on the machine. Now your FTP server will have a directory full of text files with machine serial numbers in them. Quick and dirty, but it did what I needed it to do. I'd comment out the echo lines, as I was using them for debugging.
    •    
  • Currently 2.00 / 5
  You rated: 5 / 5 (4 votes cast)
 
[21,392 views]  

A shell script to get machine serial numbers | 16 comments | Create New Account
Click here to return to the 'A shell script to get machine serial numbers' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
A shell script to get machine serial numbers
Authored by: isd_glory on Apr 05, '04 11:14:10AM

system_profiler usually takes a while to run because it has to prepare a whole stack of information before it starts outputting things. If all you are looking for is the computer's serial number, this command would be a little more efficient:

system_profiler SPHardwareDataType | grep "Serial Number"

This way, the system_profiler only has to lookup basic hardware data instead of going through the whole system profiler process. Additionally, system_profiler can look up a bunch of other data types if you choose.



[ Reply to This | # ]
AWESOME
Authored by: sapridyne on Apr 05, '04 12:11:50PM

You know, it's stuff like this hint that makes me love OS X even more... what a kick-ass operating system. I have been using OS X since DP 3 and I just got my ACSA -- and I keep on learning cool new stuff like this. Thanks, dude.



[ Reply to This | # ]
A shell script to get machine serial numbers
Authored by: raveldcp on Apr 05, '04 12:46:15PM

Two comments:

1. Most of the data can be retrieved from ioreg, and ioreg is much faster.
2. FTP is a known security hole, use mail -s "file" < serial.txt and you can have the file mailed to you without turning on ftp.



[ Reply to This | # ]
ioreg - SSH
Authored by: ssevenup on Apr 05, '04 03:22:30PM
OK, so something like this...

ioreg -c "IOPlatformExpertDevice" | awk '/IOPlatformSerialNumber/ {print $4}'

And yup, SSH or anything pretty much would be preferred over ftp :-(

---
Mark Moorcroft
ELORET Corp. - NASA/Ames RC
Sys. Admin.

[ Reply to This | # ]

ioreg - SSH
Authored by: mtalbot on Jan 19, '07 11:26:17PM
To return the serial number without the quote marks, set the field separator (-F '"') in the last awk command...
ioreg -c "IOPlatformExpertDevice" | awk -F '"' '/IOPlatformSerialNumber/ {print $4}'


[ Reply to This | # ]
A shell script to get machine serial numbers
Authored by: adamjacobmuller on Apr 15, '04 10:37:00PM

ftp is considered insecure because it transmits all information in cleartext, but so does mail, so if your concerned at all about the privacy of such data don't use ftp OR mail
mail does work around the fact that your not sending a system username and password to connect to an ftp server but it still transmits the data in cleartext
use ssh or scp



[ Reply to This | # ]
A shell script to get machine serial numbers
Authored by: afranz on Apr 05, '04 01:13:33PM
one can use 'ssh' directly to get to the information:
 ssh user@remotemachine system_profiler SPHardwareDataType | grep "Serial Number"
but i would require to enter a password. I never used RemoteDesktop but if it authenticates you automatically, you could have a series of SSH commands in a shell script on your machine.

[ Reply to This | # ]
A shell script to get machine serial numbers
Authored by: LouieNet on Apr 12, '04 03:38:31AM

If the procedure needs to be performed repeatedly, can a new dedicated user be set up with the whole command set as the shell? The reason to do this would be to set up ssh to allow a no-password login if you're coming in from only specific machines, but the account will have very limited access.

---
G4 cube, 1152M RAM, OS X Server 10.2.8
17" Powerbook G4, 1G RAM, OS X 10.2.8



[ Reply to This | # ]
A shell script to get machine serial numbers
Authored by: sauron on Apr 05, '04 07:18:07PM
I ran into this problem myself when I had to collect system information on a decent amount of hosts so I wasted an evening coming up with something that'd do the job for me

This TCL/expect script ssh-s into each host listed in $hostfile and executes all the commands in $scriptfile

The script assumes you have thesame $user on each $host; it will prompt you once for the password which you can enter without it being displayed and it will try to log in into each host and execute the commands, logging _everything_ in $host.output in your current working directory

You still have to set the variable user in the script to your login; in the script file you specify each command line with full pathname

You invoke the script with

>expect sweeper.exp todo.txt hosts.txt

todo.txt containing the commands (1 command/line)
hosts.txt containg the host names (1 host/line)

I made this by trial & error - TCL gurus, please do not smite me

---


#!/usr/bin/expect

log_user 0

set user "YOURUSERNAMEHERE"

foreach { scriptfile hostfile } $argv {
}

send_user "Enter password: "
stty -echo
expect_user -re "(.*)\n"
stty echo
puts ""
set password $expect_out(1,string)

proc runcommands {} {
	set prompt "(\\%|\\#|\\>|\\$) *$"
	global host scriptfile

	expect {
		-re $prompt {
			log_file $host.output
			set commandlist [ open $scriptfile r ]
			while { [gets $commandlist command ] > -1 } {
				send "$command\r"
				expect {
					-re $prompt { 
					}
				}
			}
			close $commandlist
			log_file
		}
	}
}

set hostlist [ open $hostfile r ]
while { [gets $hostlist host] >-1 } {

	send_user "$host\t"
        spawn /usr/bin/ssh $user@$host
        expect {
                "password:" { 
                        send "$password\r"
                        runcommands
                        puts "OK"
                }
                "(yes/no)?" { 
                        send "yes\r"
                        exp_continue
                }
                "denied" { puts "ACCESS DENIED" }
                "Bad host name" { puts "BAD HOST NAME" }
                timeout { puts "TIMEOUT" }
        }
        close
}
close $hostlist


[ Reply to This | # ]
A shell script to get machine serial numbers
Authored by: bhayes on May 07, '04 01:51:45PM

I tried this script using "system_profiler SPMemoryDataType" in my todo.txt, but the output files are zero kb. I tried using "ls" as the command, just in case it's a problem with system_profiler, but I got the same result. The script logs in to the remote machines correctly, and I get an "OK" for each machine, but something seems to be breaking on the output. Any suggestions? All machines are running 10.3.3.

---
Bob Hayes
http://www.artbeats.com



[ Reply to This | # ]
shell programming tip
Authored by: sjk on Apr 06, '04 12:06:56AM
For reasons discussed in previous hints and elsewhere on the net, sh (or bash and zsh, on OS X) is a preferable language choice to csh (or tcsh, on OS X) for shell script programming. It's common sage advice for the benefit of anyone who's more seriously interested in writing shell scripts. At times I think it deserves a hint of its own. :-)

[ Reply to This | # ]
shell programming tip
Authored by: LouieNet on Apr 12, '04 03:34:04AM
In addition, be wary of putting sensitive information, such as a password, into an environment variable. While the script is running, the variable can be read by another user if the correct ps option is used.

And if you have to put the password into the script, please make sure the appropriate read permissions are set so it cannot be read by unintended eyes.

---
G4 cube, 1152M RAM, OS X Server 10.2.8
17" Powerbook G4, 1G RAM, OS X 10.2.8

[ Reply to This | # ]

shell programming tip
Authored by: savowood on May 18, '04 10:26:38AM

The adage of not using (t)csh for scripting is based on a personal choice. The author of the csh_whynot.html page makes his assertions on personal preference and uses a few obscure references where csh takes longer than bash or sh. Typically, csh and tcsh are easier since they work more like C for those of you who program.

I write shell scripts using sh, bash, csh, and tcsh. I use the shell which gets the job done best. There are cases where I'd rather use sh over tcsh. There are other cases where tcsh works better and should be used.

Calling one shell better than the other like this author does pigeonholes us. Would you rather have just one OS in the world? Do you think it would be a better place? Since you're here on the OSX hints forum, I'd guess you wouldn't.



[ Reply to This | # ]
A shell script to get machine serial numbers
Authored by: kjcase on Sep 07, '04 02:41:44PM

Being new to UNIX, what would the command for getting more info extracted out of system profiler look like? I tried the following and nothing happened.

system_profiler SPHardwareDataType | grep "Serial Number" | grep "Machine Model" | grep "Memory" | grep "CPU Type"

How should I have written the command correctly?

thanks

Jerry



[ Reply to This | # ]
A shell script to get machine serial numbers
Authored by: j e v v on Sep 07, '04 03:20:11PM

Jerry,

"grep" is a filter program. By piping the output of system_profiler into it you tell the system to "display only the lines containing....". After the first grep there are no more lines left.

What you want is probably this:

system_profiler SPHardwareDataType | egrep "Serial Number|Machine Model|Memory|CPU" | sort

egrep is a version of grep that understands regular expressions. In this case you ask the system to "display all lines containing Serial Number OR Machine Model OR Memory OR CPU"

You can also sort the output like this:

system_profiler SPHardwareDataType | egrep "Serial Number|Machine Model|Memory|CPU"



[ Reply to This | # ]
A shell script to get machine serial numbers
Authored by: kjcase on Sep 08, '04 04:02:36PM

Fantastic! Works beautifully! This will save a lot of time creating an inventory, once I get the script put together.

Thanks again.

Jerry



[ Reply to This | # ]