my code:
import osproc
import strutils
import strformat
import nre
import streams
let users = @["daemon","bin","sys","adm","uucp","nuucp","lpd","imnadm","ldap",
"lp","snapp","invscout","www","smbnull","iwww","owww","sshd",
"hpsmh","named","nobody","noaccess","hpdb","useradm"]
let services = @["iptables","ip6tables","Bluetooth","postfix","cups","cpuspeed",
"NetworkManager","vsftpd","dhcpd","nfs","nfslock","ypbind"]
proc intToBool(x: int): bool =
if x==0: result = true
else: result = false
let fh = open("os.csv", fmWrite)
fh.write("序号,参数,配置\n")
var output = execCmdEx("df -Pm /boot | sed -n '2p' | awk '{print $2}'")
fh.write("1,/boot文件系统," & strip(output[0]) & " MB\n")
output = execCmdEx("df -Pm /var | sed -n '2p' | awk '{print $2}'")
let vargb = fmt"{parseInt(strip(output[0]))/1024:.1f}"
fh.write("2,/var文件系统," & vargb & " GB\n")
output = execCmdEx("df -Pm /tmp | sed -n '2p' | awk '{print $2}'")
let tmpgb = fmt"{parseInt(strip(output[0]))/1024:.1f}"
fh.write("3,/tmp文件系统," & tmpgb & " GB\n")
output = execCmdEx("grep SwapTotal /proc/meminfo | awk '{print $2}'")
let swapgb = fmt"{parseInt(strip(output[0]))/1024/1024:.1f}"
fh.write("4,swap文件系统," & swapgb & " GB\n")
output = execCmdEx("df -Pm / | sed -n '2p' | awk '{print $4}'")
let rootgb = fmt"{parseInt(strip(output[0]))/1024:.1f}"
fh.write("5,/文件系统剩余可用空间," & rootgb & " GB\n")
output = execCmdEx("df -PH | tail -n +2 | awk '{print $6,$5}'")
var index: int = 6
let dfs = strip(output[0]).split("\n")
for df in dfs:
var fs_name = df.split()[0]
var fs_usagerate = df.split()[1]
fh.write(intToStr(index) & "," & fs_name & "文件系统使用率," & fs_usagerate & "\n")
index += 1
let fh1 = newFileStream("/etc/login.defs", fmRead)
var
fh1Content: string = ""
if not fh1.isNil:
fh1Content = fh1.readAll()
let PASS_MIN_LEN = fh1Content.find(re"(?im)^\s*PASS_MIN_LEN\s+([1-9]\d*)\s*$")
if not PASS_MIN_LEN.isNone:
fh.write(intToStr(index) & ",口令长度,最少" & PASS_MIN_LEN.get.captures[0] & "位\n")
index += 1
else:
fh.write(intToStr(index) & ",口令长度," & "NA\n")
index += 1
let fh2 = newFileStream("/etc/pam.d/system-auth", fmRead)
var
fh2Content: string = ""
if not fh2.isNil:
fh2Content = fh2.readAll()
let dcredit = fh2Content.find(re"(?im)^\s*password\s+requisite\s+pam_cracklib\.so(\s+\S+)*\s+dcredit=(-\d+|\d+).*$")
if not dcredit.isNone:
fh.write(intToStr(index) & ",口令中数字数目," & dcredit.get.captures[1] & "位\n")
index += 1
else:
fh.write(intToStr(index) & ",口令中数字数目," & "NA\n")
index += 1
else:
fh.write(intToStr(index) & ",口令中数字数目," & "NA\n")
index += 1
if not fh2.isNil:
let lcredit = fh2Content.find(re"(?im)^\s*password\s+requisite\s+pam_cracklib\.so(\s+\S+)*\s+lcredit=(-\d+|\d+).*$")
if not lcredit.isNone:
fh.write(intToStr(index) & ",口令中小写字母数目," & lcredit.get.captures[1] & "位\n")
index += 1
else:
fh.write(intToStr(index) & ",口令中小写字母数目," & "NA\n")
index += 1
else:
fh.write(intToStr(index) & ",口令中小写字母数目," & "NA\n")
index += 1
if not fh2.isNil:
let ucredit = fh2Content.find(re"(?im)^\s*password\s+requisite\s+pam_cracklib\.so(\s+\S+)*\s+ucredit=(-\d+|\d+).*$")
if not ucredit.isNone:
fh.write(intToStr(index) & ",口令中大写字母数目," & ucredit.get.captures[1] & "位\n")
index += 1
else:
fh.write(intToStr(index) & ",口令中大写字母数目," & "NA\n")
index += 1
else:
fh.write(intToStr(index) & ",口令中大写字母数目," & "NA\n")
index += 1
if not fh2.isNil:
let ocredit = fh2Content.find(re"(?im)^\s*password\s+requisite\s+pam_cracklib\.so(\s+\S+)*\s+ocredit=(-\d+|\d+).*$")
if not ocredit.isNone:
fh.write(intToStr(index) & ",口令中特殊字符数目," & ocredit.get.captures[1] & "位\n")
index += 1
else:
fh.write(intToStr(index) & ",口令中特殊字符数目," & "NA\n")
index += 1
else:
fh.write(intToStr(index) & ",口令中特殊字符数目," & "NA\n")
index += 1
if not fh1.isNil:
let PASS_MAX_DAYS = fh1Content.find(re"(?im)^\s*PASS_MAX_DAYS\s+([1-9]\d*)\s*$")
if not PASS_MAX_DAYS.isNone:
fh.write(intToStr(index) & ",口令最长过期时间," & PASS_MAX_DAYS.get.captures[0] & "天\n")
index += 1
else:
fh.write(intToStr(index) & ",口令最长过期时间," & "NA\n")
index += 1
if not fh1.isNil:
let PASS_WARN_AGE = fh1Content.find(re"(?im)^\s*PASS_WARN_AGE\s+([1-9]\d*)\s*$")
if not PASS_WARN_AGE.isNone:
fh.write(intToStr(index) & ",口令过期提前提醒时间," & PASS_WARN_AGE.get.captures[0] & "天\n")
index += 1
else:
fh.write(intToStr(index) & ",口令过期提前提醒时间," & "NA\n")
index += 1
#let fh3 = newFileStream("/etc/ssh/sshd_config", fmRead)
#var
# fh3Content: string = ""
#if not fh3.isNil:
# fh3Content = fh3.readAll()
# let r = re"(?im)^[ \t]*Port[ \t]+(\d+)$"
#
# var port: string = ""
# for res in findAll(fh3Content, r):
# port = port & res.split()[1] & " "
#
# if port != "":
# fh.write(intToStr(index) & ",ssh端口号," & port.strip() & "\n")
# index += 1
# else:
# fh.write(intToStr(index) & ",ssh端口号," & "NA\n")
# index += 1
#else:
# fh.write(intToStr(index) & ",ssh端口号," & "NA\n")
# index += 1
let fh3 = newFileStream("/etc/ssh/sshd_config", fmRead)
var
fh3Content: string = ""
if not fh3.isNil:
fh3Content = fh3.readAll()
let r = re"(?im)^[ \t]*Port[[:blank:]]+(\d+)$" # [ \t]与[[:blank:]]是等价的
var start = 0
var port: string = ""
var m = find(fh3Content, r, start)
while m.isSome:
port = port & m.get().captures[0] & " "
start = m.get().captureBounds[0].b + 1
m = find(fh3Content, r, start)
if port != "":
fh.write(intToStr(index) & ",ssh端口号," & port.strip() & "\n")
index += 1
else:
fh.write(intToStr(index) & ",ssh端口号," & "NA\n")
index += 1
else:
fh.write(intToStr(index) & ",ssh端口号," & "NA\n")
index += 1
if not fh2.isNil:
let unlock_time = fh2Content.find(re"(?im)^\s*auth\s+required\s+pam_tally2\.so(\s+\S+)*\s+unlock_time=(\d+).*$")
if unlock_time.isSome:
fh.write(intToStr(index) & ",连续登陆失败锁定时长," & unlock_time.get.captures[1] & "秒\n")
index += 1
else:
fh.write(intToStr(index) & ",连续登陆失败锁定时长," & "NA\n")
index += 1
else:
fh.write(intToStr(index) & ",连续登陆失败锁定时长," & "NA\n")
index += 1
if not fh2.isNil:
let deny = fh2Content.find(re"(?im)^\s*auth\s+required\s+pam_tally2\.so(\s+\S+)*\s+deny=(\d+).*$")
if deny.isSome:
fh.write(intToStr(index) & ",连续登陆失败尝试次数," & deny.get.captures[1] & "次\n")
index += 1
else:
fh.write(intToStr(index) & ",连续登陆失败尝试次数," & "NA\n")
index += 1
else:
fh.write(intToStr(index) & ",连续登陆失败尝试次数," & "NA\n")
index += 1
output = execCmdEx("grep -iln 'BONDING_OPTS[[:blank:]]*=' /etc/sysconfig/network-scripts/*")
if intToBool(output[1]):
let bonds = strip(output[0]).split("\n")
for bond in bonds:
var fh4 = newFileStream(bond, fmRead)
var fh4Content: string = ""
if not fh4.isNil:
fh4Content = fh4.readAll()
var bondr = re"""(?ims)^[ \t]*DEVICE[ \t]*=[ \t]*(\S+)[ \t]*$.*^[ \t]*BONDING_OPTS[ \t]*=[ \t]*"[ \t]*mode[ \t]*=[ \t]*(\d+)[ \t]+miimon[ \t]*=[ \t]*(\d+)[ \t]*"[ \t]*$"""
var bondm = fh4Content.find(bondr)
if bondm.isSome:
fh.write(intToStr(index) & "," & bondm.get.captures[0] & "双网卡绑定模式," & bondm.get.captures[1] & "\n")
index += 1
fh.write(intToStr(index) & "," & bondm.get.captures[0] & "双网卡绑定检测时长," & bondm.get.captures[2] & "\n")
index += 1
else:
fh.write(intToStr(index) & "," & bond.split("-")[2] & "双网卡绑定模式,NA\n")
index += 1
fh.write(intToStr(index) & "," & bond.split("-")[2] & "双网卡绑定检测时长,NA\n")
index += 1
fh4.close()
else:
fh.write(intToStr(index) & ",双网卡绑定模式,NA\n")
index += 1
fh.write(intToStr(index) & ",双网卡绑定检测时长,NA\n")
index += 1
var fh5 = newFileStream("/etc/resolv.conf", fmRead)
var fh5Content: string = ""
if not fh5.isNil:
fh5Content = fh5.readAll()
let ns = re"(?im)^[ \t]*nameserver[[:blank:]]+(\S+)[ \t]*$"
var start: int = 0
var nameserver: string = ""
var nsm = find(fh5Content, ns, start)
while nsm.isSome:
nameserver = nameserver & nsm.get().captures[0] & " "
start = nsm.get().captureBounds[0].b + 1
nsm = find(fh3Content, ns, start)
if nameserver != "":
fh.write(intToStr(index) & ",DNS服务器," & nameserver.strip() & "\n")
index += 1
else:
fh.write(intToStr(index) & ",DNS服务器," & "NA\n")
index += 1
else:
fh.write(intToStr(index) & ",DNS服务器," & "NA\n")
index += 1
for user in users:
output = execCmdEx("passwd -S " & user)
if intToBool(output[1]):
fh.write(intToStr(index) & "," & user & "用户,不存在\n")
index += 1
else:
let matchObject = find(output[0], re"^\S+[ \t]+(\S+)[ \t]+")
if matchObject.get.captures[0] == "LK":
fh.write(intToStr(index) & "," & user & "用户,已锁定\n")
index += 1
else:
fh.write(intToStr(index) & "," & user & "用户,未锁定\n")
index += 1
for service in services:
output = execCmdEx("service " & service & " status")
if intToBool(output[1]):
fh.write(intToStr(index) & "," & service & "服务,已启动\n")
index += 1
else:
fh.write(intToStr(index) & "," & service & "服务,不存在或已关闭\n")
index += 1
output = execCmdEx("selinuxenabled")
if intToBool(output[1]):
fh.write(intToStr(index) & ",SELINUX,已开启\n")
else:
fh.write(intToStr(index) & ",SELINUX,已关闭\n")
if not fh1.isNil:
fh1.close()
if not fh2.isNil:
fh2.close()
if not fh3.isNil:
fh3.close()
if not fh5.isNil:
fh5.close()
fh.close()
I compile it whit the following:
nim --gcc.exe:musl-gcc --gcc.linkerexe:musl-gcc --passL:-static -d:release --opt:size `-l:"-lpcre"` c hello.nim
or
nim -d:release --opt:size `-l:"-lpcre"` c osc.nim
When I execute the code on other machine:
# ./osc
could not load: libpcre.so(.3|.1|)
compile with -d:nimDebugDlOpen for more information
I can execute the code on my local machine.
zhouhm@mybuster:~$ nim --gcc.exe:musl-gcc --gcc.linkerexe:musl-gcc --passL:-lpcre -d:release --opt:size c osc.nim
Hint: used config file '/home/zhouhm/nim-0.20.2/config/nim.cfg' [Conf]
Hint: system [Processing]
Hint: widestrs [Processing]
Hint: io [Processing]
Hint: osc [Processing]
Hint: osproc [Processing]
Hint: strutils [Processing]
Hint: parseutils [Processing]
Hint: math [Processing]
Hint: bitops [Processing]
Hint: macros [Processing]
Hint: algorithm [Processing]
Hint: unicode [Processing]
Hint: os [Processing]
Hint: pathnorm [Processing]
Hint: osseps [Processing]
Hint: posix [Processing]
Hint: times [Processing]
Hint: options [Processing]
Hint: typetraits [Processing]
Hint: strtabs [Processing]
Hint: hashes [Processing]
Hint: streams [Processing]
Hint: cpuinfo [Processing]
Hint: linux [Processing]
Hint: strformat [Processing]
Hint: nre [Processing]
Hint: pcre [Processing]
Hint: util [Processing]
Hint: tables [Processing]
Hint: [Link]
/usr/bin/ld: cannot find -lpcre
collect2: error: ld returned 1 exit status
Error: execution of an external program failed: 'musl-gcc -o /home/zhouhm/osc /home/zhouhm/.cache/nim/osc_r/stdlib_assertions.nim.c.o /home/zhouhm/.cache/nim/osc_r/stdlib_io.nim.c.o /home/zhouhm/.cache/nim/osc_r/stdlib_system.nim.c.o /home/zhouhm/.cache/nim/osc_r/stdlib_parseutils.nim.c.o /home/zhouhm/.cache/nim/osc_r/stdlib_math.nim.c.o /home/zhouhm/.cache/nim/osc_r/stdlib_unicode.nim.c.o /home/zhouhm/.cache/nim/osc_r/stdlib_strutils.nim.c.o /home/zhouhm/.cache/nim/osc_r/stdlib_pathnorm.nim.c.o /home/zhouhm/.cache/nim/osc_r/stdlib_posix.nim.c.o /home/zhouhm/.cache/nim/osc_r/stdlib_options.nim.c.o /home/zhouhm/.cache/nim/osc_r/stdlib_times.nim.c.o /home/zhouhm/.cache/nim/osc_r/stdlib_os.nim.c.o /home/zhouhm/.cache/nim/osc_r/stdlib_hashes.nim.c.o /home/zhouhm/.cache/nim/osc_r/stdlib_strtabs.nim.c.o /home/zhouhm/.cache/nim/osc_r/stdlib_streams.nim.c.o /home/zhouhm/.cache/nim/osc_r/stdlib_osproc.nim.c.o /home/zhouhm/.cache/nim/osc_r/stdlib_strformat.nim.c.o /home/zhouhm/.cache/nim/osc_r/stdlib_pcre.nim.c.o /home/zhouhm/.cache/nim/osc_r/stdlib_tables.nim.c.o /home/zhouhm/.cache/nim/osc_r/stdlib_nre.nim.c.o /home/zhouhm/.cache/nim/osc_r/osc.nim.c.o -lm -lrt -lpcre -ldl'
/usr/bin/ld: cannot find -lpcre
collect2: error: ld returned 1 exit status
Error: execution of an external program failed:
$ nim --gcc.exe:musl-gcc --gcc.linkerexe:musl-gcc --passL:-lpcre -d:release --opt:size c osc.nim
zhouhm@mybuster:~$ nim --gcc.exe:musl-gcc --gcc.linkerexe:musl-gcc --passL:-static -d:release --opt:size -d:usePcreHeader c osc.nim
Hint: used config file '/home/zhouhm/nim-0.20.2/config/nim.cfg' [Conf]
Hint: system [Processing]
Hint: widestrs [Processing]
Hint: io [Processing]
Hint: osc [Processing]
Hint: osproc [Processing]
Hint: strutils [Processing]
Hint: parseutils [Processing]
Hint: math [Processing]
Hint: bitops [Processing]
Hint: macros [Processing]
Hint: algorithm [Processing]
Hint: unicode [Processing]
Hint: os [Processing]
Hint: pathnorm [Processing]
Hint: osseps [Processing]
Hint: posix [Processing]
Hint: times [Processing]
Hint: options [Processing]
Hint: typetraits [Processing]
Hint: strtabs [Processing]
Hint: hashes [Processing]
Hint: streams [Processing]
Hint: cpuinfo [Processing]
Hint: linux [Processing]
Hint: strformat [Processing]
Hint: nre [Processing]
Hint: pcre [Processing]
Hint: util [Processing]
Hint: tables [Processing]
CC: stdlib_nre.nim
Error: execution of an external compiler program 'musl-gcc -c -w -Os -I/home/zhouhm/nim-0.20.2/lib -I/home/zhouhm -o /home/zhouhm/.cache/nim/osc_r/stdlib_nre.nim.c.o /home/zhouhm/.cache/nim/osc_r/stdlib_nre.nim.c' failed with exit code: 1
/home/zhouhm/.cache/nim/osc_r/stdlib_nre.nim.c:11:10: fatal error: pcre.h: No such file or directory
#include <pcre.h>
^~~~~~~~
compilation terminated.
root@mybuster:~# find / -name 'pcre.h'
/home/zhouhm/hello_musl/pcre8.42/include/pcre.h
/home/zhouhm/hello_musl/pcre-8.42/pcre.h
/usr/include/pcre.h
zhouhm@mybuster:~$ nim --gcc.exe:musl-gcc --gcc.linkerexe:musl-gcc --passL:-static -d:release --opt:size `-l:"-lpcre"` -d:usePcreHeader c osc.nim
bash: -l:-lpcre: command not found
Hint: used config file '/home/zhouhm/nim-0.20.2/config/nim.cfg' [Conf]
Hint: system [Processing]
Hint: widestrs [Processing]
Hint: io [Processing]
Hint: osc [Processing]
Hint: osproc [Processing]
Hint: strutils [Processing]
Hint: parseutils [Processing]
Hint: math [Processing]
Hint: bitops [Processing]
Hint: macros [Processing]
Hint: algorithm [Processing]
Hint: unicode [Processing]
Hint: os [Processing]
Hint: pathnorm [Processing]
Hint: osseps [Processing]
Hint: posix [Processing]
Hint: times [Processing]
Hint: options [Processing]
Hint: typetraits [Processing]
Hint: strtabs [Processing]
Hint: hashes [Processing]
Hint: streams [Processing]
Hint: cpuinfo [Processing]
Hint: linux [Processing]
Hint: strformat [Processing]
Hint: nre [Processing]
Hint: pcre [Processing]
Hint: util [Processing]
Hint: tables [Processing]
CC: stdlib_nre.nim
Error: execution of an external compiler program 'musl-gcc -c -w -Os -I/home/zhouhm/nim-0.20.2/lib -I/home/zhouhm -o /home/zhouhm/.cache/nim/osc_r/stdlib_nre.nim.c.o /home/zhouhm/.cache/nim/osc_r/stdlib_nre.nim.c' failed with exit code: 1
/home/zhouhm/.cache/nim/osc_r/stdlib_nre.nim.c:11:10: fatal error: pcre.h: No such file or directory
#include <pcre.h>
^~~~~~~~
compilation terminated.
I success The whole process is:
# aptitude install musl
# aptitude install musl-dev
# aptitude install musl-tools
$ git clone https://github.com/kaushalmodi/hello_musl
$ cp /home/zhouhm/hello_musl/pcre8.42/include/pcre.h /home/zhouhm
$nim --gcc.exe:musl-gcc --gcc.linkerexe:musl-gcc --passL:-static --passL:"/home/zhouhm/hello_musl/pcre-8.42/.libs/libpcre.a" -d:release --opt:size -d:usePcreHeader c osc.nim
perfect!!!
or
download from https://ftp.pcre.org/pub/pcre/pcre-8.43.tar.bz2
./configure --prefix=/usr \
--docdir=/usr/share/doc/pcre-8.43 \
--enable-unicode-properties \
--enable-pcre16 \
--enable-pcre32 \
--enable-pcregrep-libz \
--enable-pcregrep-libbz2 \
--enable-pcretest-libreadline
make
make install
nim --gcc.exe:musl-gcc --gcc.linkerexe:musl-gcc --passL:-static --passL:"/usr/lib/libpcre.a" -d:release --opt:size -d:usePcreHeader c osc.nim
strip osc
You don't need copy pcre.h to the path that osc.nim reside