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


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: 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 | # ]