博客
关于我
c python shell获得主机名和ip
阅读量:769 次
发布时间:2019-03-23

本文共 850 字,大约阅读时间需要 2 分钟。

8964 Linux平台

#define BUF_LEN 1024void getIP(){    char hostname[BUF_LEN];    struct hostent *host;    int i = 0;    memset(hostname, 0, BUF_LEN);    gethostname(hostname, sizeof(hostname));    host = gethostbyname(hostname);    printf("\nhostname: %s\n", hostname);    for (; host->h_addr_list[i]; ++i){        printf("ip : %s\n", inet_ntoa(*(struct in_addr *)(host->h_addr_list[i])));    }}

Python平台

import socketdef showHostIP():    sk = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)    hostname = socket.gethostname()    print("hostname = %s" % hostname)    ip = socket.gethostbyname(hostname)    iplist = socket.gethostbyname_ex(hostname)    print(ip)    print(iplist)

Shell脚本

function showHost(){    hostname=$(uname -n)    ifconfig | grep "inet addr" | while read ipaddr;    do        ipaddr=${ipaddr#*:}        ipaddr=${ipaddr%% *}    done}

转载地址:http://kibzk.baihongyu.com/

你可能感兴趣的文章
Mysql--逻辑架构
查看>>
MySql-2019-4-21-复习
查看>>
mysql-5.7.18安装
查看>>
MySQL-Buffer的应用
查看>>
mysql-cluster 安装篇(1)---简介
查看>>
mysql-connector-java各种版本下载地址
查看>>
mysql-EXPLAIN
查看>>
mysql-group_concat
查看>>
MySQL-redo日志
查看>>
MySQL-【1】配置
查看>>
MySQL-【4】基本操作
查看>>
Mysql-丢失更新
查看>>
Mysql-事务阻塞
查看>>
Mysql-存储引擎
查看>>
mysql-开启慢查询&所有操作记录日志
查看>>
MySQL-数据目录
查看>>
MySQL-数据页的结构
查看>>
MySQL-架构篇
查看>>
MySQL-索引的分类(聚簇索引、二级索引、联合索引)
查看>>
Mysql-触发器及创建触发器失败原因
查看>>