为自动将“一键获取软硬件配置及管理员组”抓取的资料插入数据库进行管理,现在使用Shell进行处理并插入数据库。以下为扫描结果示例,需要将一下数据插入到MySQL数据库
System Information:
Computer Name = C0300022B068
OS Type = Windows 8.1 专业版 64-bit (6.3, Build 9600) (9600.winblue_gdr.131030-1505)
System Model = System Product Name
CPU Model = Pentium(R) Dual-Core CPU E5500 @ 2.80GHz (2 CPUs), ~2.8GHz
RAM Size = 4096MB RAM
DisplayCard :
Display Card = Microsoft 基本显示适配器
DisplayMemory = 256 MB
Mother Board:
Manufacturer =ASUSTeK Computer INC.
MotherBoard Model=P5KPL-AM
Hard Disk:
Model=ST3500418AS ATA Device
Size=500038694400
Network Card:
NetCard Model = TAP-Windows Adapter V9
NetCard Model = Realtek PCIe FE Family Controller
MAC Address = 00-FF-C6-35-FA-9F
MAC Address = 00-23-54-0A-31-A9
IP Address = 172.17.44.103(首选)
IP Address = 192.168.100.103(首选)
Software Information:
谷歌拼音输入法 2.7
Open××× 2.3.3-I002
TAP-Windows 9.9.2
7-Zip 9.30 (x64 edition)
Windows Live MIME IFilter
Java 8 Update 20 (64-bit)
Microsoft Visual C++ 2008 Redistributable - x64 9.0.30729.4148
Java SE Development Kit 8 Update 20 (64-bit)
Microsoft Application Error Reporting
PDF-Viewer
Microsoft Visual C++ 2005 Redistributable (x64)
MSVCRT110_amd64
Adobe Flash Player 15 Plugin
支付宝安全控件 4.3.0.3375
FileZilla Server
VIA 平台设备管理员
Mozilla Firefox 32.0.3 (x86 zh-CN)
Mozilla Maintenance Service
Notepad++
PSR Viewer
Tencent QQMail Plugin
迅雷7
VLC media player 2.1.3
Windows Live 软件包
WinSCP 5.5.5
Windows Live UX Platform
Windows Live Writer
Windows Live UX Platform Language Pack
Junk Mail filter update
Radmin Viewer 3.5
Windows Live Photo Common
Microsoft Visual C++ 2008 Redistributable - x86 9.0.30729.4148
Platform
Windows Live 软件包
Windows Live Writer
Windows Live Writer
微软设备健康助手
Windows Live Communications Platform
Java Auto Updater
Windows Live Mail
Windows Live Writer Resources
LibreOffice 4.3.0.4
Windows Live Installer
Windows Live Writer
Windows Live Writer Resources
Windows Live UX Platform Language Pack
Windows Live 程式集
Microsoft Visual C++ 2005 Redistributable
中国邮政储蓄银行网上银行安全套件 v2.3.3.40
Photo Common
MSVCRT
MSVCRT110
Microsoft Visual C++ 2008 Redistributable - x86 9.0.21022
Adobe Reader 8 - Chinese Traditional
Windows Live PIMT Platform
Windows Live Mail
Windows Live Mail
Windows Live SOXE
MSVCRT_amd64
Windows Live SOXE Definitions
Citrix XenCenter
Photo Common
Evernote v. 5.6.4
D3DX10
Microsoft WSE 3.0 Runtime
Microsoft Visual C++ 2008 Redistributable - x86 9.0.21022.218
Microsoft WSE 2.0 SP3 Runtime
百度云管家
迅雷看看播放器
迅雷看看高清播放组件
银联安全控件IE版 1.0.0.7
银联安全控件非IE版 3.0.0.2
360安全浏览器7
支付宝数字证书组件 2.0.0.1
USB Information:
USB is Open,Please Tag It.
Language Code:
Language:gbk
Admin Users:
Administrator
Luke
Power Users:
FileShare Information:
ADMIN$ C:\Windows 远程管理
C$ C:\ 默认共享
IPC$ 远程 IPC
D$ D:\ 默认共享
E$ E:\ 默认共享
F$ F:\ 默认共享
HardFile$ D:\HardFile
cd_rom D:\cd_rom
HardSoft Viewer
D:\HardSoft Viewer
public D:\public
1.Samba服务器的建立:
以下为samba服务器架设脚本
#!/bin/bash ##################################################### # # # This Script is Install&Setup Samba # # # ##################################################### yum -y install samba samba-common samba-client cp /etc/samba/smb.conf /etc/samba/smb.conf_default mkdir -p /data/hardfile groupadd hardgroup useradd -g hardgroup -s /sbin/nologin -d /dev/null harduser chmod 750 /data/hardfile chown harduser.hardgroup /data/hardfile (echo hardpasswd;echo hardpasswd) | smbpasswd -a -s harduser echo '* soft nofile 65000'>>/etc/security/limits.conf echo '* hard nofile 65000'>>/etc/security/limits.conf ulimit -SHn 65000 cat << EOF > /etc/samba/smb.conf [global] dos charset = cp950 unix charset = utf8 display charset = utf8 workgroup = Workgroup server string = HardSoft Viewer log file = /var/log/samba/log.%m max log size = 50 load printers = No idmap config * : backend = tdb [HardFile] comment = For HardSoft Viewer Result path = /data/hardfile write list = @hardgroup read only = No create mask = 0660 directory mask = 0770 EOF /etc/init.d/smb start;chkconfig smb on
2.编码问题的处理
因Windows的文件基于GBK和BIG5,(说明:公司为台资企业,大部分电脑为繁体操作系统,但是也存在一部分简体系统)批处理暂时未发现可以生成UTF-8字符的文本文件。怎么办呢,首先检查操作系统的区域设置信息即简繁体信息,然后shell获取到Language:gbk标签进行编码之别,再利用iconv软件将文本文件转成UTF-8编码,以下使用Yum安装iconv.
[root@Server ~]# yum -y install perl-Text-Iconv
3.MySQL的权限设置及建表
a.先建立一个HardSoft数据库,数据库下有Hardware(硬件资讯表)、Software(已装软件列表)、Access(权限表)、User(使用者表)
-- MySQL dump 10.15 Distrib 10.0.12-MariaDB, for Linux (x86_64) -- -- Host: localhost Database: HardSoft -- ------------------------------------------------------ -- Server version 10.0.12-MariaDB-log /*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */; /*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */; /*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */; /*!40101 SET NAMES utf8 */; /*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */; /*!40103 SET TIME_ZONE='+00:00' */; /*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */; /*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */; /*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */; /*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */; -- -- Current Database: `HardSoft` -- CREATE DATABASE /*!32312 IF NOT EXISTS*/ `HardSoft` /*!40100 DEFAULT CHARACTER SET utf8 */; USE `HardSoft`; -- -- Table structure for table `Access` -- DROP TABLE IF EXISTS `Access`; /*!40101 SET @saved_cs_client = @@character_set_client */; /*!40101 SET character_set_client = utf8 */; CREATE TABLE `Access` ( `aid` varchar(40) NOT NULL, `admins` varchar(200) DEFAULT NULL, `poweruser` varchar(200) DEFAULT NULL, `usb` tinyint(4) DEFAULT NULL, `atime` datetime DEFAULT NULL, PRIMARY KEY (`aid`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; /*!40101 SET character_set_client = @saved_cs_client */; -- -- Dumping data for table `Access` -- LOCK TABLES `Access` WRITE; /*!40000 ALTER TABLE `Access` DISABLE KEYS */; /*!40000 ALTER TABLE `Access` ENABLE KEYS */; UNLOCK TABLES; -- -- Table structure for table `Hardware` -- DROP TABLE IF EXISTS `Hardware`; /*!40101 SET @saved_cs_client = @@character_set_client */; /*!40101 SET character_set_client = utf8 */; CREATE TABLE `Hardware` ( `hid` varchar(50) NOT NULL, `htime` datetime DEFAULT NULL, `os` varchar(200) DEFAULT NULL, `cpu` varchar(200) DEFAULT NULL, `model` varchar(80) DEFAULT NULL, `ram` varchar(80) DEFAULT NULL, `display` varchar(100) DEFAULT NULL, `mf` varchar(80) DEFAULT NULL, `mb` varchar(200) DEFAULT NULL, `hd` varchar(200) DEFAULT NULL, `size` varchar(80) DEFAULT NULL, `net` varchar(100) DEFAULT NULL, `mac` varchar(200) DEFAULT NULL, `ip` varchar(200) DEFAULT NULL, PRIMARY KEY (`hid`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; /*!40101 SET character_set_client = @saved_cs_client */; -- -- Dumping data for table `Hardware` -- LOCK TABLES `Hardware` WRITE; /*!40000 ALTER TABLE `Hardware` DISABLE KEYS */; /*!40000 ALTER TABLE `Hardware` ENABLE KEYS */; UNLOCK TABLES; -- -- Table structure for table `Software` -- DROP TABLE IF EXISTS `Software`; /*!40101 SET @saved_cs_client = @@character_set_client */; /*!40101 SET character_set_client = utf8 */; CREATE TABLE `Software` ( `id` int(11) NOT NULL AUTO_INCREMENT, `sid` varchar(80) NOT NULL, `stime` datetime DEFAULT NULL, `soft` varchar(200) DEFAULT NULL, `note` varchar(50) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; /*!40101 SET character_set_client = @saved_cs_client */; -- -- Dumping data for table `Software` -- LOCK TABLES `Software` WRITE; /*!40000 ALTER TABLE `Software` DISABLE KEYS */; /*!40000 ALTER TABLE `Software` ENABLE KEYS */; UNLOCK TABLES; -- -- Table structure for table `User` -- DROP TABLE IF EXISTS `User`; /*!40101 SET @saved_cs_client = @@character_set_client */; /*!40101 SET character_set_client = utf8 */; CREATE TABLE `User` ( `id` varchar(20) NOT NULL, `factory` varchar(50) DEFAULT NULL, `user` varchar(50) DEFAULT NULL, `account` varchar(50) DEFAULT NULL, `locate` varchar(50) DEFAULT NULL, `note` varchar(50) DEFAULT NULL, PRIMARY KEY (`id`), UNIQUE KEY `id` (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; /*!40101 SET character_set_client = @saved_cs_client */; -- -- Dumping data for table `User` -- LOCK TABLES `User` WRITE; /*!40000 ALTER TABLE `User` DISABLE KEYS */; /*!40000 ALTER TABLE `User` ENABLE KEYS */; UNLOCK TABLES; /*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */; /*!40101 SET SQL_MODE=@OLD_SQL_MODE */; /*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */; /*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */; /*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */; /*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */; /*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */; /*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */; -- Dump completed on 2014-10-29 18:19:29
b.建立插入所使用的数据库账户及权限设置
grant select,insert,update,delete on HardSoft.* to 'HardSoft'@'localhost' identified by 'HardSoft-Viewer'
4.Shell脚本编码转换及字段处理,文件转移
[root@Server ~]# vim /usr/sbin/viewer.sh #!/bin/bash ########################################################## # This Script For Get HardSoft Information To MySQL # ########################################################## LC_ALL=C HSVDIR=/data/hardfile TMPDIR=/data/tmp BAKDIR=/data/hdbak [ ! -d $TMPDIR ] && mkdir -p $TMPDIR [ ! -d $BAKDIR ] && mkdir -p $BAKDIR MYSQLBIN='/data/mysql/bin/mysql' DBHOST='localhost' DBUSER='HardSoft' DBPWD='HardSoft-Viewer' mysql="$MYSQLBIN -h$DBHOST -u$DBUSER -p$DBPWD -e " db=HardSoft for file in `ls $HSVDIR` do cp -f $HSVDIR/$file $BAKDIR lang=`grep "Language:" $HSVDIR/$file|awk -F':' '{print $2}'` iconv -f $lang -t utf-8 -o $HSVDIR/$file $HSVDIR/$file #name=$file name=`grep 'Computer Name' $HSVDIR/$file|awk -F' = ' '{print $2}'|tr -d '\n\r'` os=`grep "OS Type" $HSVDIR/$file|grep "Type"|awk -F'=' '{print $2}'|sed 's#(.*)##g'|tr -d '\n\r'` cpu=`grep "CPU" $HSVDIR/$file|awk -F'=' '{print $2}'|tr -d '\n\r'` model=`grep "System Model" $HSVDIR/$file|awk -F'=' '{print $2}'|tr -d '\n\r'` #get RAM ram=`grep "RAM Size" $HSVDIR/$file|awk -F'=' '{print $2}'|sed 's# RAM##g'||tr -d '\n\r'` #get DisplayCard display=`grep "Display Card" $HSVDIR/$file|awk -F'=' '{print $2}'|tr -d '\n\r'` #get MotherBoard mf=`grep "Manufacturer" $HSVDIR/$file|awk -F'=' '{print $2}'|tr -d '\n\r'` mb=`grep "MotherBoard Model" $HSVDIR/$file|awk -F'=' '{print $2}'|tr -d '\n\r'` #get Hard Information starthd=`grep -n "Hard Disk:" $HSVDIR/$file|awk -F':' '{print $1}'` endhd=`grep -n "Network Card:" $HSVDIR/$file|awk -F':' '{print $1}'` #hdinfo=`awk "NR==$starthd+1,NR==$endhd-1 {print}" $HSVDIR/$file` #hd=`echo $hdinfo |awk -F'=' '{print $2}'|awk '{print $1}'` #echo $hdinfo #hd=`grep "Model" $hdinfo |awk -F'=' '{print $2}'|sed 's/Size//g'|tr -d '\n\r' ` hd=`awk "NR==$starthd+1,NR==$endhd-1 {print}" $HSVDIR/$file|grep "Model"|awk -F'=' '{print $2}'|sed 's#\r\r##g'` #hd=`echo $hdinfo |sed 's/Size//g' ` size=`awk "NR==$starthd+1,NR==$endhd-1 {print}" $HSVDIR/$file|grep "Size"|awk -F'=' '{print $2}'|sed 's#\r\r##g'` #size=`grep "Size" $hdinfo |awk -F'=' '{print $2}'|tr -d '\n\r'` #get Network net=`grep "NetCard Model" $HSVDIR/$file|awk -F'=' '{print $2}'|tr -d '\n\r'` mac=`grep "MAC Address" $HSVDIR/$file|awk -F'=' '{print $2}'|tr -d '\n\r'` ip=`grep "IP Address" $HSVDIR/$file|awk -F'=' '{print $2}'|sed 's/(.*)//g'|tr -d '\n\r'` #ip=`cat $HSVDIR/$file |grep "IP Address"|awk -F'=' '{print $2}'|sed 's/(.*)//g'|sed 's#\n\r##g'` #echo $ip #Check Hardware Table hunum=`$mysql "select count(*) from $db.Hardware where hid='$name'"` hnum=`echo $hunum|awk '{print $2}'` [ $hnum -ne 0 ] && $mysql "delete from $db.Hardware where hid='$name'" #Insert Hardware Data $mysql "insert into $db.Hardware(hid,htime,os,cpu,model,ram,display,mf,mb,hd,size,net,mac,ip) values ('$name',now(),'$os','$cpu','$model','$ram','$display','$mf','$mb','$hd','$size','$net','$mac','$ip')" ###################################################################### #get Software startsw=`grep -n "Software Information:" $HSVDIR/$file|awk -F':' '{print $1}'` endsw=`grep -n "USB Information:" $HSVDIR/$file|awk -F':' '{print $1}'` #Check Software Table sunum=`$mysql "select count(*) from $db.Software where sid='$name'"` snum=`echo $sunum|awk '{print $2}'` [ $snum -ne 0 ] && $mysql "delete from $db.Software where sid='$name'" #get Software & Insert Software Data awk "NR==$startsw+1,NR==$endsw-1 {print}" $HSVDIR/$file| sed -e '/^$/d' | sed "s/'//g" |sed 's#^\s##g'|while read line do $mysql "insert into $db.Software(sid,stime,soft) values('$name',now(),'$line')" done ####################################################################### #get USB usb=`grep 'USB is Open' $HSVDIR/$file|wc -l` #get Admins startad=`grep -n "Admin Users:" $HSVDIR/$file|awk -F':' '{print $1}'` endad=`grep -n "Power Users:" $HSVDIR/$file|awk -F':' '{print $1}'` ad=`awk "NR==$startad+1,NR==$endad-1 {print}" $HSVDIR/$file | sed -e '/^$/d'|sed '/Administrator/d'|sed '/Domain Admins/d'` #get PowerUser startpw=`grep -n "Power Users:" $HSVDIR/$file|awk -F':' '{print $1}'` endpw=`grep -n "FileShare Information:" $HSVDIR/$file|awk -F':' '{print $1}'` pw=`awk "NR==$startpw+1,NR==$endpw-1 {print}" $HSVDIR/$file | sed -e '/^$/d'` #Check Access Table aunum=`$mysql "select count(*) from $db.Access where aid='$name'"` anum=`echo $aunum|awk '{print $2}'` [ $anum -ne 0 ] && $mysql "delete from $db.Access where aid='$name'" #Insert Access Data $mysql "insert into $db.Access(aid,admins,poweruser,usb,atime) values('$name','$ad','$pw','$usb',now())" ###################################################################### #Check User Table & Insert ID unum=`$mysql "select count(*) from $db.User where User.id='$name'"` num=`echo $unum|awk '{print $2}'` [ $num -eq 0 ] && $mysql "insert into $db.User(id) values('$name')" ###################################################################### mv $HSVDIR/$file $TMPDIR done
5.建立定时任务,每天的8:00-20:00每分钟执行一次
echo "*/1 8-20 * * * root sh /usr/sbin/viewer.sh">>/etc/crontab /etc/init.d/crond restart