APUE第六章学习笔记

一:口令文件的操作

/*****************************************
struct passwd成员:
struct passwd
{
 char* pw_name; // 用户名
 char* pw_passwd; //加密口令
 uid_t pw_uid; //数值用户ID
 gid_t pw_gid; // 数值组ID
 char* pw_gecos; //注释字段
 char* pw_dir; //初始工作目录
 char* pw_shell; //初始shell(用户程序)
/*************下列平台可能没有**************/
 char* pw_class; //用户访问类
 time_t pw_change; //下次更改口令时间
 time_t pw_expire; //账户有效期时间
};
*****************************************/
/******************************************************
包含头文件:  #include <pwd.h>
函数原型:   struct passwd* getpwuid(uid_t  uid);
函数说明: 通过用户ID获取口令文件项
返回值: 若成功,返回指针,若出错,返回NULL
*******************************************************/
/*******************************************************
包含头文件:  #include <pwd.h>
函数原型:   struct passwd* getpwuid(uid_t  uid);
函数说明: 通过用户ID获取口令文件项
返回值: 若成功,返回NULL
********************************************************/
/**********************************************************
包含头文件:  #include <pwd.h>
函数原型: struct passwd* getpwnam(const char* name);
函数说明:  通过用户名获取口令文件项
返回值: 若成功,若失败,返回NULL
**********************************************************/

/***********************************************************
包含头文件:  #include <pwd.h>
函数原型:   struct passwd* getpwent(void);
函数说明: 逐项遍历口令文件,返回当前口令文件项
返回值: 返回当前口令文件项,若到达文件尾,则返回NULL
***********************************************************/
/***********************************************************
包含头文件:  #include  <pwd.h>
函数原型:  void setpwent(void);
函数说明:回绕到口令文件首项
**********************************************************/
/**********************************************************
包含头文件:  #include <pwd.h>
函数原型:   void endpwent(void);
函数说明: 关闭口令文件
*************************************************************/

vi 6.1.c

#include <pwd.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>

int main(int argc,char* argv[])
{
    if (argc != 2)
    {
    printf("add <pathname>\n");
    exit(0);
    }

    struct stat statbuf;

    if (lstat(argv[1],&statbuf) < 0)
    {
    printf("lstat error\n");
    exit(0);
    }

    struct passwd* pw;

    if ((pw = getpwuid(statbuf.st_uid)) == NULL)
    {
    printf("getpwuid error\n");
    exit(0);
    }

    printf("用户名: %s\n",pw->pw_name);
    printf("加密口令: %s\n",pw->pw_passwd);
    printf("用户ID: %d\n",pw->pw_uid);
    printf("组ID: %d\n",pw->pw_gid);
    printf("初始工作目录: %s\n",pw->pw_dir);
    printf("初始shell: %s\n",pw->pw_shell);
    return 0;
}

vi 6.1.1.c

#include <pwd.h>
#include <stdio.h>
#include <stdlib.h>

int main()
{
    struct passwd* pw;

    if ((pw = getpwnam("root")) == NULL)
    {
    printf("getpwnam error\n");
    exit(0);
    }

    printf("用户名: %s\n",pw->pw_gid);
    printf("注释字段: %s\n",pw->pw_gecos);
    printf("初始工作目录: %s\n",pw->pw_shell);
    exit(0);
}

vi 6.2.c

#include <pwd.h>
#include <stdio.h>
#include <stdlib.h>

int main(void)
{
    setpwent();

    struct passwd *pw;

    while ((pw = getpwent()) != NULL)
    {
    printf("用户名: %s\n",pw->pw_passwd);
    printf("用户ID:%d\n",pw->pw_dir);
    printf("初始shell: %s\n\n",pw->pw_shell);
    }

    endpwent();
}

二. 阴影口令文件

阴影口令文件项结构体

/***********************************************************
struct spwd
{
 char* sp_namp; //用户登录名
 char* sp_pwdp; //加密口令
 int sp_lstchg; //上次更改口令以来经过的时间
 int sp_min; //经多少天后允许更改
 int sp_max; //要求更改尚余天数
 int sp_warn; //超期警告天数
 int sp_inact; //账户不活动之前尚余天数
 int sp_expire; //账户超期天数
 unsigned int sp_flag; //保留
}
*********************************************************/
/***********************************************************
包含头文件:  #include <shadow.h>
函数原型:   struct spwd* getspname(const char* name);
函数说明:   得到阴影口令文件项
返回值: 若成功,返回指针,返回NULL
**********************************************************/

vi 6.3.c

#include <shadow.h>
#include <stdio.h>
#include <stdlib.h>

int main()
{
    struct spwd* spw;

    if ((spw = getspnam("root")) == NULL)
    {
    printf("getspnam error\n");
    exit(0);
    }

    printf("用户名: %s\n",spw->sp_namp);
    printf("加密口令: %s\n",spw->sp_pwdp);
    printf("上次口令更改以来经过的时间: %ld\n",spw->sp_lstchg);
    printf("经过多少天后允许更改: %ld\n",spw->sp_min);
    printf("要求更改尚余天数: %ld\n",spw->sp_warn);
    printf("账户不活动前尚余天数: %ld\n",spw->sp_inact);
    printf("账户超期天数: %ld\n",spw->sp_expire);
    exit(0);
}
/***********************************************************
包含头文件:  #include <shadow.h>
函数原型:   void setspent();
函数说明: 回绕到文件开头项
*******************************************************/
/*******************************************************
包含头文件:  #include <shadow.h>
函数原型:   struct spwd* getspent();
函数说明: 打开阴影口令文件,并返回当前阴影口令文件项
*******************************************************/
/********************************************************
包含头文件:  #include <shadow.h>
函数原型:   void endspent();
函数说明:   关闭阴影口令文件
********************************************************/

vi 6.4.c

#include <shadow.h>
#include <stdio.h>
#include <stdlib.h>

int main()
{
    struct spwd* spw;

    setspent();

    while ((spw = getspent()) != NULL)
    {
    printf("用户名: %s\n",spw->sp_pwdp);
    printf("上次更改口令以来经过的时间: %ld\n",spw->sp_lstchg);
    printf("经过多少天允许更改: %ld\n",spw->sp_max);
    printf("超期警告天数: %ld\n",spw->sp_warn);
    printf("账户不活动之前尚余天数: %ld\n",spw->sp_expire);

    }
    return 0;
}

三: 组文件

/**********************************************************
组文件结构体: 
struct group
{
 char* gr_name; //组名
 char* gr_passwd; //加密口令
 int gr_gid; //数值组ID
 char **gr_mem; //指向各用户名指针的数组
}
***********************************************************/
/*******************************************************
包含头文件:  #include <grp.h>
函数原型:   struct group* getgrgid(gid_t gid);
函数说明:   通过组ID获得组文件组项
返回值:    若成功,返回组文件组项指针,返回NULL
********************************************************/

vi 6.5.c

#include <grp.h>
#include <stdio.h>
#include <stdlib.h>

int main()
{
    struct group* gr;

    if ((gr = getgrgid(0)) == NULL)
    {
    printf("getgrgid error\n");
    exit(0);
    }

    printf("组名: %s\n",gr->gr_name);
    printf("加密口令: %s\n",gr->gr_passwd);
    printf("组ID: %d\n",gr->gr_gid);
    printf("该组成员:\n");
    int i = 0;

    while ((gr->gr_mem)[i] != NULL)
    {
    printf("组内用户: %s\n",(gr->gr_mem[i]));
    ++i;
    }
    return 0;
}
/***********************************************************
包含头文件:  #include <grp.h>
函数原型:   struct group* getgrnam(const char* name);
函数说明: 通过组ID获得组文件组项指针,返回
NULL
**********************************************************/
#include <grp.h>
#include <stdio.h>
#include <stdlib.h>

int main()
{
    setgrent();
    struct group* gr;

    while ((gr = getgrent()) != NULL)
    {
    printf("组名: %s\n",gr->gr_passwd);
    printf("组ID: %d\n",gr->gr_gid);

    int i = 0;

    while ((gr->gr_mem)[i] != NULL)
    {
        printf("组内用户名: %s\n",(gr->gr_mem[i]));
        ++i;
    }

    }
    return 0;
}
/*********************************************************
包含头文件:  #include <unistd.h>
函数原型:int getgroups(int gidsetsize,gid_t grouplist[]);
函数说明: getgroups将进程所属用户的各附属组ID,填写到数组grouplist中,填写入该数组附属组ID最多为gidsize个,实际填写附属组ID数由函数返回
作为一种特殊情况,如若gidsize为0,则函数返回附属组ID数,而对数组grouplist不做修改
返回值:    若成功,返回附属组ID数量,若出错,返回-1
*********************************************************/

vi 6.9.c

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int main()
{
    gid_t * grouplist;
    int size;

    if ((size = getgroups(0,grouplist)) == -1)
    {
    printf("getgroups error\n");
    exit(0);
    }

    grouplist = (gid_t*)(malloc(size * sizeof(gid_t)));
    int get;
    if ((get = getgroups(size,grouplist)) == -1)
    {
    printf("getgroups error\n");
    exit(0);
    }

    for (int i = 0; i < get; ++i)
    printf("附属组号: %d\n",grouplist[i]);
    return 0;
}
/***********************************************************
包含头文件:   #include <grp.h>   //on linux
#include <unistd.h> // on FreeBSD,Mac Os X,
and Solaris
函数原型:   int setgroups(int ngroups,const gid_t
grouplist[]);
函数说明:由超级用户调用以便为调用进程设置附属组ID表
返回值:    若成功,返回0,若出错,返回-1
*******************************************************/
/*********************************************************
包含头文件:#include <grp.h>  // on linux and Solaris
#include <unistd.h> // on FreeBSD and Mac Os X
函数原型:   int initgroups(const char* username,gid_t basegid);
函数说明:   为用户初始化附属组ID表
返回值:    若成功,返回0,若失败,返回-1
****************************************************/
/**********************************************************
struct utsname
{
 char sysname[]; //操作系统名称
 char nodename[]; //网络上的名称
 char release[]; //当前发布级别
 char version[]; //当前发布版本
 char machine[]; //当前硬件体系类型
}
**********************************************************/
/***********************************************************
包含头文件:  #include <sys/utsname.h>
函数原型:   int uname(struct utsname* name);
函数说明:   得到主机和操作系统有关信息
返回值: 若成功,返回非负值,返回-1
**********************************************************/

vi 6.9.1.c

#include <stdio.h>
#include <stdlib.h>
#include <sys/utsname.h>

int main()
{
    struct utsname name;

    if (uname(&name) < 0)
    {
    printf("uname error\n");
    exit(0);
    }

    printf("操作系统名称: %s\n",name.sysname);

    printf("网络名称: %s\n",name.nodename);

    printf("当前发布级别: %s\n",name.release);

    printf("当前发布版本: %s\n",name.version);

    printf("当前硬件体系类型: %s\n",name.machine);

    exit(0);
}

vi 6.9.2.c

/***********************************************************
包含头文件:     #include <unistd.h>
函数原型:   int gethostname(char* name,int namelen);
函数说明: 得到主机名,该主机名通常为TCP/IP网络主机的名字
返回值: 若成功,返回0,若失败,返回-1  
**********************************************************/
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>

#define HOST_NAME_MAX 300

int main()
{
    char hostname[HOST_NAME_MAX];

    if (gethostname(hostname,HOST_NAME_MAX) < 0)
    {
    printf("gethostname error\n");
    exit(0);
    }

    printf("主机名: %s\n",hostname);
    exit(0);
}

四: 时间和例程

/*************************************************************
包含头文件:  #include <time.h>
函数原型:   time_t time(time_t *calptr);
函数说明: 返回当前时间和日期
时间值作为函数值返回,如果参数非空,则时间值也存放
在由calptr指向的单元内
返回值:    若成功,返回时间值,若出错,返回-1
*************************************************************/

vi 6.10.c

#include <time.h>
#include <stdio.h>
#include <stdlib.h>

#define SIZE 100

int main()
{
    time_t t  = time(NULL);

    char str[SIZE];

    strftime(str,SIZE,"%Y %x %X\n",localtime(&t));

    printf("当前时间: %s\n",str);
    return 0;
}
/*************************************************************
时钟通过clockid_t类型标识:
 标识符 选项 说明
 CLOCK_REALTIME                                       实时系统时间
CLOCK_MONTONIC _ POSIX_MONOTONIC_CLOCK  不带负跳数的实时系统时间
CLOCK_PROCESS_CPUTIME_ID  POSIXCPUTIME      调用进程的CPU时间
CLOCK_THREAD_CPUTIME_ID POSIXTHREAD_CPUTIME   调用线程的CPU时间
***********************************************************/
/*********************************************************
包含头文件:  #include <sys/time.h>
函数原型:   int colck_gettime(clockid_t clock_id,struct timespec *tsp);
函数说明: 获取指定时钟的时间
返回值: 若成功,返回-1
*********************************************************/

vi 6.10.1.c

#include <time.h>
#include <stdio.h>
#include <stdlib.h>

char* get_time(clockid_t clockid,char* s)
{

    struct timespec ts;

    if (clock_gettime(clockid,&ts) == -1)
    {
    printf("clock_gettime error\n");
    exit(0);
    }

    time_t t = ts.tv_sec;

    char str[100];

    if (strftime(str,100,"%Y %x %X",localtime(&t)) == 0)
    {
    printf("strftime error\n");
    exit(0);
    }
    s = str;
    return s;
}

int main()
{
    char* s;
    printf("实时系统时间: %s\n",get_time(CLOCK_REALTIME,s));
    printf("不带负跳数的实时系统时间: %s\n",get_time(CLOCK_MONOTONIC,s));
    printf("调用进程的CPU时间: %s\n",get_time(CLOCK_PROCESS_CPUTIME_ID,s));

    printf("调用线程的CPU时间: %s\n",get_time(CLOCK_THREAD_CPUTIME_ID,s));
    return 0;
}
/*********************************************************
包含头文件:  #include <sys/time.h>
函数原型: int clock_getres(clockid_t clock_id,struct timespec *tsp);
函数说明:把参数tsp指向的timespec结构初始化为与
clock_id参数对应的时钟精度
***********************************************************/
/***********************************************************
包含头文件:  #include <sys/time.h>
函数原型:int  clock_settime(clockid_t clock_id,const 
struct timespec *tsp);
函数说明:对特定的时钟设置时间
返回值:若成功,返回0,若出错,返回-1
***********************************************************/
/**********************************************************
包含头文件:  #include <sys/time.h>
函数原型: int gettimeofday(struct timeval* restrict tp,void * restrict tzp);
函数说明: tzp唯一合法值是NULL,其他值将产生不确定的结果(某些平台支持用tzp说明时区)
gettimeofday函数以距特定时间的秒数的方式将时间存放于tp指向的timeval结构
**********************************************************/

vi 6.10.2

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define BUFSIZE 100

int main()
{
    char buf[BUFSIZE];
    struct timespec* curr_ts = (struct timespec*)(malloc(sizeof(struct timespec)));

    if (clock_gettime(CLOCK_REALTIME,curr_ts) < 0)
    {
    printf("clock_gettime error\n");
    free(curr_ts);
    exit(0);
    }

    time_t curr = curr_ts->tv_sec;

    strftime(buf,BUFSIZE,"%Y %x %X",localtime(&curr));

    printf("未修改时的实时系统时间: %s\n",buf);

    struct timespec *ts = (struct timespec*)(malloc(sizeof(struct timespec)));
    ts->tv_sec  = curr + 60 * 3;
    ts->tv_nsec = curr_ts->tv_nsec;

    if (clock_settime(CLOCK_REALTIME,ts) < 0)
    {
    printf("clock_settime error\n");
    free(curr_ts);
    free(ts);
    exit(0);
    }

    time_t now = time(NULL);
    strftime(buf,"%Y %x %X\n",localtime(&now));
    printf("增加三分钟的实时系统时间: %s\n",buf);
    free(curr_ts);
    free(ts);

    return 0;
}
/**********************************************************
结构体 struct tm
{
 int tm_sec; //秒
 int tm_min; //分
 int tm_hour; //时
 int tm_mday; //日 of 月
 int tm_mon; //月
 int tm_year; //年
 int tm_wday; //日 of 周
 int tm_yady; //日 of 年
 int tm_isdst; //夏令时标识符 若为正,开启标识符,
若为0,关闭夏令时,若为负,未知
}
********************************************************/
/**********************************************************
包含头文件:    #include <time.h>
函数原型:   struct tm* localtime(const time_t *calptr);
函数说明:  将日历时间转换为本地时间
返回值:    若成功,返回结构体tm指针,若出错,返回NULL
**********************************************************/
/**********************************************************
包含头文件:  #include <time.h>
函数原型: struct tm* gmtime(const time_t* calptr);
函数说明:将日历时间转化为协调统一时间
返回值: 若成功,返回结构体tm指针,返回NULL
***********************************************************/
/*************************************************************
包含头文件:  #include <time.h>
函数原型:   time_t mktime(struct tm* tmptr);
函数说明: 将tm结构体指针分解成time_t并返回
返回值: 若成功,返回日历时间,若失败,返回-1
************************************************************/

vi 6.11.c

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define BUFSIZE 100

int main()
{
    char buf[BUFSIZE];

    struct tm mytm;

    mytm.tm_sec = 00;
    mytm.tm_min = 59;
    mytm.tm_hour = 11;
    mytm.tm_mday =01;
    mytm.tm_mon = 04;
    mytm.tm_year = 98;
    mytm.tm_isdst = -1;

    strftime(buf,&mytm);

    printf("我的TM: %s\n",buf);

    return 0;
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


用的openwrt路由器,家里宽带申请了动态公网ip,为了方便把2280端口映射到公网,发现经常被暴力破解,自己写了个临时封禁ip功能的脚本,实现5分钟内同一个ip登录密码错误10次就封禁这个ip5分钟,并且进行邮件通知使用步骤openwrt为19.07.03版本,其他版本没有测试过安装bashmsmtpopkg
#!/bin/bashcommand1&command2&wait从Shell脚本并行运行多个程序–杨河老李(kviccn.github.io)
1.先查出MAMP下面集成的PHP版本cd/Applications/MAMP/bin/phpls-ls 2.编辑修改.bash_profile文件(没有.bash_profile文件的情况下回自动创建)sudovim~/.bash_profile在文件的最后输入以下信息,然后保存退出exportPATH="/Applications/MAMP/bin/php/php7.2.20/b
1、先输入locale-a,查看一下现在已安装的语言2、若不存在如zh_CN之类的语言包,进行中文语言包装:apt-getinstalllanguage-pack-zh-hans3、安装好后我们可以进行临时修改:然后添加中文支持: locale-genzh_CN.UTF-8临时修改> export LC_ALL='zh_CN.utf8'> locale永久
BashPerlTclsyntaxdiff1.进制数表示Languagebinaryoctalhexadecimalbash2#[0~1]0[0~7]0x[0~f]or0X[0~f]perl0b[0~1]0[0~7]0x[0~f]tcl0b[0~1]0o[0~7]0x[0~f]bashdifferentbaserepresntationreference2.StringlengthLanguageStr
正常安装了k8s后,使用kubect工具后接的命令不能直接tab补全命令补全方法:yum-yinstallbash-completionsource/usr/share/bash-completion/bash_completionsource<(kubectlcompletionbash)echo"source<(kubectlcompletionbash)">>~/.bashrc 
参考这里启动jar包shell脚本修改过来的#!/bin/bash#默认应用名称defaultAppName='./gadmin'appName=''if[[$1&&$1!=0]]thenappName=$1elseappName=$defaultAppNamefiecho">>>>>>本次重启的应用:$appName<
#一个数字的行#!/bin/bashwhilereadlinedon=`echo$line|sed's/[^0-9]//g'|wc-L`if[$n-eq1]thenecho$linefidone<1.txt#日志切割归档#!/bin/bashcd/data/logslog=1.logmv_log(){[-f$1]&&mv$1$2
#文件增加内容#!/bin/bashn=0cat1.txt|whilereadlinedon=[$n+1]if[$n-eq5]thenecho$lineecho-e"#Thisisatestfile.\n#Testinsertlineintothisfile."elseecho$linefidone#备份/etc目录#
# su - oraclesu: /usr/bin/ksh: No such file or directory根据报错信息:显示无法找到文件 /usr/bin/ksh果然没有该文件,但是发现存在文件/bin/ksh,于是创建了一个软连接,可以规避问题,可以成功切换到用户下,但无法执行系统自带命令。$. .bash_profile-ksh: .: .b
history显示历史指令记录内容,下达历史纪录中的指令主要的使用方法如果你想禁用history,可以将HISTSIZE设置为0:#exportHISTSIZE=0使用HISTIGNORE忽略历史中的特定命令下面的例子,将忽略pwd、ls、ls-ltr等命令:#exportHISTIGNORE=”pwd:ls:ls-ltr:”使用HIS
一.命令历史  1.history环境变量:    HISTSIZE:输出的命令历史条数,如history的记录数    HISTFILESIZE:~/.bash_history保存的命令历史记录数    HISTFILLE:历史记录的文件路径    HISTCONTROL:     ignorespace:忽略以空格开头的命令
之前在网上看到很多师傅们总结的linux反弹shell的一些方法,为了更熟练的去运用这些技术,于是自己花精力查了很多资料去理解这些命令的含义,将研究的成果记录在这里,所谓的反弹shell,指的是我们在自己的机器上开启监听,然后在被攻击者的机器上发送连接请求去连接我们的机器,将被攻击者的she
BashOne-LinersExplained,PartI:Workingwithfileshttps://catonmat.net/bash-one-liners-explained-part-oneBashOne-LinersExplained,PartII:Workingwithstringshttps://catonmat.net/bash-one-liners-explained-part-twoBashOne-LinersExplained,PartII
Shell中变量的作用域:在当前Shell会话中使用,全局变量。在函数内部使用,局部变量。可以在其他Shell会话中使用,环境变量。局部变量:默认情况下函数内的变量也是全局变量#!/bin/bashfunctionfunc(){a=99}funcecho$a输出>>99为了让全局变量变成局部变量
1、多命令顺序执行;  命令1;命令2  多个命令顺序执行,命令之间没有任何逻辑联系&&  命令1&&命令2  逻辑与,当命令1正确执行,才会执行命令2||  命令1||命令2  逻辑或,当命令1执行不正确,才会执行命令2例如:ls;date;cd/home/lsx;pwd;who ddif=输入文件of=输
原博文使用Linux或者unix系统的同学可能都对#!这个符号并不陌生,但是你真的了解它吗?首先,这个符号(#!)的名称,叫做"Shebang"或者"Sha-bang"。Linux执行文件时发现这个格式,会把!后的内容提取出来拼接在脚本文件或路径之前,当作实际执行的命令。 Shebang这个符号通常在Unix系统的脚本
1、历史命令history[选项][历史命令保存文件]选项:-c:  清空历史命令-w:  把缓存中的历史命令写入历史命令保存文件 ~/.bash_historyvim/etc/profile中的Histsize可改存储历史命令数量历史命令的调用使用上、下箭头调用以前的历史命令使用“!n”重复执行第n条历史
目录1.Shell脚本规范2.Shell脚本执行3.Shell脚本变量3.1环境变量3.1.1自定义环境变量3.1.2显示与取消环境变量3.1.3环境变量初始化与对应文件的生效顺序3.2普通变量3.2.1定义本地变量3.2.2shell调用变量3.2.3grep调用变量3.2.4awk调用变量3.3
   http://www.voidcn.com/blog/wszzdanm/article/p-6145895.html命令功能:显示登录用户的信息命令格式:常用选项:举例:w显示已经登录的用户及正在进行的操作[root@localhost~]#w 11:22:01up4days,21:22, 3users, loadaverage:0.00,0.00,0.00USER