引言

由于在某些场景需要使用静态链接的snmpwalk或者其他程序,为了方便执行文件的分发,适配多版本系统,所以需要自己编译net-snmp

实验操作系统Ubuntu22 amd64

获取程序

git clone方式

$ git clone https://github.com/net-snmp/net-snmp.git

或者也可以依据tag下载源码包

生成Makefile

net-snmp项目根路径下面有一个configure文件,可以用于生成Makefile,命令执行过程中会弹出一些输入确认,直接回车确认即可

$ ./configure --prefix=/tmp/snmp --disable-embedded-perl --without-perl-modules --disable-ucd-snmp-compatibility --disable-scripts

--prefix参数指定文件安装目录到/tmp/snmp

需要禁用perl相关模块以及一些脚本,否则可能会遇到错误如下

relocation R_X86_64_PC32 against symbol `netsnmp_ds_handle_config' can not be used when making a shared object; recompile with -fPIC

生成执行程序

如果此时项目根目录下面生成一个Makefile,则表示configure命令成功

执行程序的源码和生成的二进制都在apps/目录下

$ make LDFLAGS="-static"

校验生成的二进制执行文件,发现不是静态链接的

$ ldd apps/snmpwalk
        linux-vdso.so.1 (0x00007ffed71c1000)
        libcrypto.so.3 => /lib/x86_64-linux-gnu/libcrypto.so.3 (0x00007ff639800000)
        libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007ff639400000)
        /lib64/ld-linux-x86-64.so.2 (0x00007ff639d7e000)

静态编译snmpwalk

发现在make snmpwalk执行的时候会产生两条执行命令,第一条是 gcc包含-static参数,但是第二条没包含

$ cd apps
$ rm snmpwalk
$ make snmpwalk
/bin/bash ../libtool  --mode=link gcc  -static -g -O2 -DNETSNMP_ENABLE_IPV6 -fno-strict-aliasing -DNETSNMP_REMOVE_U64 -g -O2 -Ulinux -Dlinux=linux  -Wall -Wextra -Wstrict-prototypes -Wwrite-strings -Wcast-qual -Wimplicit-fallthrough -Wlogical-op -Wundef -Wno-format-truncation -Wno-missing-field-initializers -Wno-sign-compare -Wno-unused-parameter  -o snmpwalk snmpwalk.lo ../snmplib/libnetsnmp.la   
libtool: link: gcc -g -O2 -DNETSNMP_ENABLE_IPV6 -fno-strict-aliasing -DNETSNMP_REMOVE_U64 -g -O2 -Ulinux -Dlinux=linux -Wall -Wextra -Wstrict-prototypes -Wwrite-strings -Wcast-qual -Wimplicit-fallthrough -Wlogical-op -Wundef -Wno-format-truncation -Wno-missing-field-initializers -Wno-sign-compare -Wno-unused-parameter -o snmpwalk .libs/snmpwalk.o  ../snmplib/.libs/libnetsnmp.a -lm -lssl -lcrypto

所以需要执行命令如下,把make snmpwalk的输出的第二条命令粘贴下来,手动在gcc后面加上-static参数

$ rm snmpwalk
$ gcc -static -g -O2 -DNETSNMP_ENABLE_IPV6 -fno-strict-aliasing -DNETSNMP_REMOVE_U64 -g -O2 -Ulinux -Dlinux=linux -Wall -Wextra -Wstrict-prototypes -Wwrite-strings -Wcast-qual -Wimplicit-fallthrough -Wlogical-op -Wundef -Wno-format-truncation -Wno-missing-field-initializers -Wno-sign-compare -Wno-unused-parameter -o snmpwalk .libs/snmpwalk.o  ../snmplib/.libs/libnetsnmp.a -lm -lssl -lcrypto 
# 执行的时候会产生告警信息如下,这是正常的
/usr/bin/ld: /usr/lib/gcc/x86_64-linux-gnu/11/../../../x86_64-linux-gnu/libcrypto.a(libcrypto-lib-dso_dlfcn.o): in function `dlfcn_globallookup':
(.text+0x17): 警告: Using 'dlopen' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
/usr/bin/ld: ../snmplib/.libs/libnetsnmp.a(system.o): in function `netsnmp_str_to_gid':
/home/gong/rust-work/github/net-snmp/snmplib/system.c:1492: 警告: Using 'getgrnam' in statically linked applications requires at runtime the shared libraries from the bc version used for linking
/usr/bin/ld: /home/gong/rust-work/github/net-snmp/snmplib/system.c:1494: 警告: Using 'endgrent' in statically linked applications requires at runtime the shared librar from the glibc version used for linking
/usr/bin/ld: ../snmplib/.libs/libnetsnmp.a(system.o): in function `netsnmp_str_to_uid':
/home/gong/rust-work/github/net-snmp/snmplib/system.c:1459: 警告: Using 'getpwnam' in statically linked applications requires at runtime the shared libraries from the bc version used for linking
/usr/bin/ld: /home/gong/rust-work/github/net-snmp/snmplib/system.c:1461: 警告: Using 'endpwent' in statically linked applications requires at runtime the shared librar from the glibc version used for linking
/usr/bin/ld: ../snmplib/.libs/libnetsnmp.a(system.o): in function `netsnmp_getaddrinfo':
/home/gong/rust-work/github/net-snmp/snmplib/system.c:884: 警告: Using 'getaddrinfo' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
/usr/bin/ld: ../snmplib/.libs/libnetsnmp.a(system.o): in function `netsnmp_gethostbyaddr':
/home/gong/rust-work/github/net-snmp/snmplib/system.c:1050: 警告: Using 'gethostbyaddr' in statically linked applications requires at runtime the shared libraries frome glibc version used for linking
/usr/bin/ld: ../snmplib/.libs/libnetsnmp.a(system.o): in function `netsnmp_gethostbyname':
/home/gong/rust-work/github/net-snmp/snmplib/system.c:980: 警告: Using 'gethostbyname' in statically linked applications requires at runtime the shared libraries from  glibc version used for linking

检查snmpwalk链接

$ ldd snmpwalk

 不是动态可执行文件

之后也可以把生成的snmpwalk复制到另外一台古老的linux上运行校验,如果机器上有docker, 可以执行命令docker run -it alpine sh,把snmpwalk复制到容器内部执行

以上就是net-snmp静态编译链接的获取程序及生成执行程序详解的详细内容,更多关于net-snmp静态编译链接的资料请关注本站其它相关文章!

点赞(0) 打赏

评论列表 共有 0 条评论

暂无评论

微信公众账号

微信扫一扫加关注

发表
评论
返回
顶部