LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-21-2014, 12:59 PM   #16
tomtweber
LQ Newbie
 
Registered: Feb 2014
Posts: 7

Original Poster
Rep: Reputation: Disabled

Quote:
Originally Posted by schneidz View Post
kind of a moving target... do you see how the string encap:Ethernet HWaddr is not the same as wlan0 Link encap:Ethernet HWaddr
and mlb_serial_number seems to be not what you are extracting (serial_number ?)

therefore dan martins awk command wont work on your 2nd post.

does chromebook have a way to ssh into them so that you wouldnt have to sneakernet to each one ?
No way to SSH. Only WIFI, no ethernet connection
 
Old 02-21-2014, 01:02 PM   #17
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,313

Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
Quote:
Originally Posted by tomtweber View Post
No way to SSH. Only WIFI, no ethernet connection
ssh works over wifi as demonstrated by my previous post about ssh'ing to my xbmc box... it seems like the sshd server daemon is disabled in chrome-os by default and it would take some hacking to re-enable (mite not be worth it).

so it seems like you have what you need to script up something that can strip out the wanted text and redirect it to a file ?

Last edited by schneidz; 02-21-2014 at 01:12 PM.
 
Old 02-21-2014, 02:29 PM   #18
tomtweber
LQ Newbie
 
Registered: Feb 2014
Posts: 7

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by danielbmartin View Post
Okay, this time only a hint.

Google is your friend. Do a Google search on awk substr and get lots of hits.
This one ... http://www.grymoire.com/Unix/Awk.html ... is excellent.

Daniel B. Martin
I found a site that explained it really well and helped me to fully understand what the numbers in () meant. Thanks for a starting point.

http://www.starlink.rl.ac.uk/docs/sc4.htx/node38.html
 
Old 02-21-2014, 03:06 PM   #19
tomtweber
LQ Newbie
 
Registered: Feb 2014
Posts: 7

Original Poster
Rep: Reputation: Disabled
Thanks to danielbmartin and schneidz I am very close to a working solution. Seeing as the data in the file could vary, searching by the number of characters from the beginning as offered by danielbmartin will not work. Searching just for the REGEX does not seem to be a valid solution either. But based on those two examples (and a lot of research on Google) I was able to come up with what I think will work. Please let me know if the following code is correctly formatted and whether it would work properly. I would place the script on a USB flashdrive. The tech would perform steps 1-12. At that point the script should run and output the service tag and WiFi MAC address to MAC.csv. Additionally as far as steps 8-12, can any of those steps be part of the script or is there a method of accomplishing the tasks is an easier way?

1. Plug in USB flashdrive
2. Power on ChromeBook
3. Press Esc-Refresh+Power
4. At the recovery screen, press Ctrl-D
5. Press enter to turn off OS verification
6. press Ctrl-D to enter developer mode
7. Press Ctrl-Alt-F2 to enter command prompt
8. Type chronos
9. Type sudo bash
10. Type mount -t auto /dev/sdb1 /media/removable
11. Type Chmod 755 /media/removable/MAC.sh
12. Type ./media/removable/MAC.sh

Code:
#!/bin/bash
#MAC.sh Capture the service tag and MAC address to a file
clear
infile=/media/removable/data.txt		#Sets data.txt as the source data file
outfile=/media/removable/MAC.csv		#Sets MAC.csv as the destination data file
dump_vpd_log --full --stdout > $infile 		#Writes system summary to data.txt
ifconfig wlan0 >> $infile 			#Appends WIFI info to data.txt
awk 'match($0, /service_tag/) {			#Finds the service tag and assigns it to variable serial
serial=substr($0,RSTART+RLENGTH+3,7)
print $serial","
}
;
match($0, /Ethernet  HWaddr/) {			#Finds the MAC address and assigns it to variable MAC
MAC=substr($0,RSTART+RLENGTH+1,17)
print $MAC\n
}
' $infile >> $outfile				#Uses data.txt as the AWK input and outputs the AWK results to MAC.CSV
umount /dev/sdb1				#Unmounts the USB flashdrive
eject /dev/sdb1					#Safely ejects the USB flashdrive
crossystem disable_dev_request=1; reboot	#Removes the ChromeBook from developer mode and reboots the system

Last edited by tomtweber; 02-21-2014 at 03:09 PM.
 
Old 02-21-2014, 03:46 PM   #20
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
Quote:
Originally Posted by tomtweber View Post
... searching by the number of characters from the beginning as offered by danielbmartin will not work.
The details of your overall task are way over my head. However, I suggest you become familiar with the Built-In Function index.
Code:
index(in, find)
    This searches the string in for the first occurrence of the string find,
    and returns the position in characters where that occurrence begins in the string in.
You may find this helpful in writing string-handling code of this nature ...
Code:
# Contrived code, untested, may contain syntax errors
Pstart=index(haystack,needle1)
Pend=index(haystack,needle2)
InterestingString=substr(haystack,Pstart,Pend-Pstart+1)
It is possible to combine these three lines into one but that makes for less readable code.

Daniel B. Martin
 
Old 02-22-2014, 11:53 AM   #21
tomtweber
LQ Newbie
 
Registered: Feb 2014
Posts: 7

Original Poster
Rep: Reputation: Disabled
I finally have a fully functioning, tested and working, script. Thanks to everyone for their expertise and feedback.
Code:
#!/bin/bash
# MAC.sh Capture the service tag and MAC address to a CSV file named MAC.csv
#Clears the terminal screen
clear
#Erases all variables before starting
servicetag=''
mac=''
infile=''
outfile=''
size=''
#Sets MAC.txt as the source data file
infile=/media/ubuntu/Win7/Dell/MAC.txt
#Sets MAC.csv as the destination data file
outfile=/media/ubuntu/Win7/Dell/MAC.csv
#Writes system summary to MAC.txt
# dump_vpd_log --full --stdout > $infile
#Appends BIOS read serial number to MAC.txt
dmidecode -s system-serial-number >> $infile
#Finds the BIOS detected serial number and assigns it to variable servicetag3
servicetag3=$(dmidecode -s system-serial-number)
#Finds the Linux detected MAC address and assigns it to variable mac3
mac3=$(cat /sys/class/net/eth0/address)
mac3=$(echo $mac3 | tr '[:lower:]' '[:upper:]')
#Appends WIFI info to data.txt
# ifconfig wlan0 >> $infile
#Finds the service tag and assigns it to variable servicetag
servicetag=$(awk 'match($0,/service_tag/) {servicetag2=substr($0,RSTART+RLENGTH+3,7); print servicetag2}' $infile)
#Finds the MAC address and assigns it to variable mac
mac=$(awk 'match($0,/Ethernet  HWaddr/) {mac2=substr($0,RSTART+RLENGTH+1,17); print mac2}' $infile)
#Shows the service tag and WiFi MAC address			
echo "Service Tag 3 is" $servicetag3" and MAC 3 is" $mac3". The service tag is" $servicetag". The WiFi MAC is" $mac". The following has been written to MAC.csv: "$servicetag","$mac "\n The ChromeBook will exit developer mode and reboot in 10 seconds. Do not turn off the computer, please wait."
#Checks if MAC.csv exists, if not, creates file and writes spreadsheet headers
[ -f $outfile ] || echo "Service Tag,WiFi MAC Address" > $outfile
#Checks actual size of MAC.csv
size=$(du -b "$outfile" | cut -f 1)
#Checks if MAC.csv is greater than 5 bytes, if not, writes spreadsheet headers
[ $size -gt 5 ] || echo "Service Tag,WiFi MAC Address" >> $outfile
#Writes the service tag and WiFi MAC address to MAC.csv
echo $servicetag","$mac >> $outfile
#Unmounts the USB flashdrive
# umount /dev/sdb1
#Safely ejects the USB flashdrive
# eject /dev/sdb1
#Removes the ChromeBook from developer mode and reboots the system
#Pauses for 10 seconds before continuing
sleep 10
echo "Rebooting now!"
# crossystem disable_dev_request=1; reboot

Last edited by tomtweber; 02-22-2014 at 11:55 AM.
 
Old 02-22-2014, 12:30 PM   #22
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,313

Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
if chromebooks have a .profile or .bash_profile, you can consider putting that script in the last line of that file.
or maybe you would like to put it in /etc/rc.local or maybe you can crontab it and put it in a @reboot line.

not sure if chromos supports those since it seems to be a heavily reduced form of gnu/linux (like android) but its worth a shot.


edit: i just realized you only need to run this once per machine so automating it would be an unnecessary step (so my above advice wouldnt apply).

Last edited by schneidz; 02-22-2014 at 12:34 PM. Reason: retracted
 
Old 02-22-2014, 12:36 PM   #23
tomtweber
LQ Newbie
 
Registered: Feb 2014
Posts: 7

Original Poster
Rep: Reputation: Disabled
Thanks schneidz. I'll look into that!
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] How to write a .sh script to read configuration file and write into .txt file ISStaras Linux - Newbie 8 09-06-2012 06:03 PM
[SOLVED] how do u write a script that searches files by date? Chupakabra Linux - Newbie 7 07-26-2011 05:56 PM
[SOLVED] write file to a specific sector wiener Linux - General 8 06-28-2010 04:59 PM
how to get the specific text from a txt file in bash script deepakdeore2004 Programming 8 04-30-2010 06:35 AM
write text to specific location in file mcbenus Linux - Desktop 3 02-28-2008 06:40 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:54 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration