• 一行代码修改Linux机器密码和Samba服务密码


    当我们需要用命令代替我们往Linux窗口输入东西时,可以考虑使用Linux文件描述符里的标准输入,而passwd、smbpasswd命令均有自己对应的标准输入参数,我们可以通过查看它们的帮助信息获取相应参数。

    passwd=**** && (echo $passwd;echo $passwd) | sudo -A smbpasswd username -s
    options:
      -L                   local mode (must be first option)
      -h                   print this usage message
      -s                   use stdin for password prompt #-s就是标准输出
      -c smb.conf file     Use the given path to the smb.conf file
      -D LEVEL             debug level
      -r MACHINE           remote machine
      -U USER              remote username (e.g. SAM/user)
    extra options when run by root or in local mode:
      -a                   add user
      -d                   disable user
      -e                   enable user
      -i                   interdomain trust account
      -m                   machine trust account
      -n                   set no password
      -W                   use stdin ldap admin password
      -w PASSWORD          ldap admin password
      -x                   delete user
      -R ORDER             name resolve order
    

    上面的脚本分解步骤是:

    1. 设置一个临时变量passwd,其意义是需要设置smb密码
    2. 通过echo或printf输出passwd的值两次并通过管道传递给smbpasswd命令。(此处输出两次,是因为在smbpasswd
    3. 时候需要输入两次密码来确认)
    4. 通过sudo -A参数来避免sudo时输入密码
    5. 通过smbpasswd nferzhuang -s参数来使用上面echo的输出作为smbpasswd的输入
    echo "password" |passwd --stdin user
    Usage: passwd [OPTION...] <accountName>
      -k, --keep-tokens       keep non-expired authentication tokens
      -d, --delete            delete the password for the named account (root only)
      -l, --lock              lock the password for the named account (root only)
      -u, --unlock            unlock the password for the named account (root only)
      -e, --expire            expire the password for the named account (root only)
      -f, --force             force operation
      -x, --maximum=DAYS      maximum password lifetime (root only)
      -n, --minimum=DAYS      minimum password lifetime (root only)
      -w, --warning=DAYS      number of days warning users receives before password expiration (root only)
      -i, --inactive=DAYS     number of days after password expiration when an account becomes disabled (root only)
      -S, --status            report password status on the named account (root only)
      --stdin                 read new tokens from stdin (root only)
    

    不过,当使用该命令进行批量修改机器密码时可以通过history查看到。

       70  echo "zgh234" |passwd --stdin root
       71  history
       72  passwd --help
       73  history
    

    参考链接:https://blog.csdn.net/nfer_zhuang/article/details/42454283

  • 相关阅读:
    C++小项目:directx11图形程序(七):modelclass
    C++小项目:directx11图形程序(六):cameraclass
    C++小项目:directx11图形程序(五):shadersclass
    C++小项目:directx11图形程序(四):d3dclass
    C++小项目:directx11图形程序(三):graphicsclass
    C++小项目:directx11图形程序(二):systemclass
    舞台上加载一外部SWF,舞台如何调用该swf内部方法呢?
    as3绘制抛物线
    Unity常用代码总结
    C# ref和out区别介绍
  • 原文地址:https://www.cnblogs.com/ghylpb/p/14545908.html
Copyright © 2020-2023  润新知