All pastes #850597 Raw Edit

hdparm serial extractor

public cpp v1 · immutable
#850597 ·published 2008-01-11 09:05 UTC
rendered paste body
#include <iostream>#include <string>#include <fstream>#include <stdexcept>std::string getstrline( std::ifstream& file ){	char buffer[512];	file.getline( buffer, 512 );	return std::string( buffer );}std::string getSerialNumber( std::string hdline ){	size_t start, end;	start = hdline.find("SerialNo=");	if( start == std::string::npos )		throw std::runtime_error("string does not contain serial information");	start += strlen("SerialNo=");	end = hdline.find_last_not_of("\n\r", start);	if( end == std::string::npos )		end = hdline.length();		return hdline.substr(start, end);}int main(){	std::string outputfile = "hdinfo";	std::string cmd = "sudo hdparm -i /dev/[sh]da > ";	cmd += outputfile;	system( cmd.c_str() );		std::ifstream tmpfile( outputfile.c_str() );	while( tmpfile.tellg() >= 0 )	{		try		{			std::cout << getSerialNumber( getstrline( tmpfile ) ) << '\n';		}		catch(...)		{			continue;		}	}	tmpfile.close();	cmd = "rm -f ";	cmd += outputfile;	system( cmd.c_str() );		return 0;}