-
Notifications
You must be signed in to change notification settings - Fork 154
/
unix_users_to_sql.ksh
73 lines (62 loc) · 1.34 KB
/
unix_users_to_sql.ksh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#!/bin/ksh
# Script Name : unix_users_to_sql.ksh
# Author : Craig Richards
# Created : 27 August 2010
# Last Modified :
# Version : 1.0
# Modifications :
# Description : Export UNIX users to sql, you can change the last line if you want the output to go to a file, rather than be inserted straight into the database
#################################
# Start of procedures/functions #
#################################
typeset TABLE='PASSWD_FILE'
passwd_table()
{
print "WHENEVER SQLERROR EXIT"
print "WHENEVER OSERROR EXIT"
print "set autocommit off"
print "create table ${TABLE}
(
hostname varchar2(50),
user_name varchar2(50),
user_description varchar2(100),
shell varchar2(20)
);"
}
{
passwd_table
typeset -i COUNT=0
typeset FILE=/etc/passwd
typeset Item1
typeset Item5
typeset Item7
typeset host
cat ${FILE} | while read LINE
do
let COUNT=COUNT+1
Item1=$(print ${LINE} | cut -d':' -f1)
Item5=$(print ${LINE} | cut -d':' -f5)
Item7=$(print ${LINE} | cut -d':' -f7)
host=`hostname`
print "
insert into ${TABLE}
(
hostname,
user_name,
user_description,
shell
)
values
(
'${host}',
'${Item1}',
'${Item5}',
'${Item7}'
);"
done
print "commit;"
print "select * from ${TABLE};"
# Remove the hash from the line below, and has out the SQLPLUS line if you want ithe output to a file
#} > /tmp/insert_passwd.sql
} | sqlplus -s scott/tiger
## End of Program