【问题标题】:Executing passwd command in Python Paramiko to change a password on a Linux server在 Python Paramiko 中执行 passwd 命令以更改 Linux 服务器上的密码
【发布时间】:2021-08-27 17:43:33
【问题描述】:

我正在尝试编写一个 Python 3 脚本以实用 ssh 进入 Linux 服务器并更改密码。我已经使用 Paramiko 模块编写了一个脚本。

我在尝试运行多个 shell 命令时遇到了问题。我的脚本尝试执行命令,但 Paramiko 在一个 shell 命令后超时。

这是我目前正在编写的脚本。任何见解将不胜感激。

import paramiko

def change_pw():
    hostname = "IP" #IP Address of Linux Server
    username = "root" #username
    password = "oldpw!" #password for Linux Server

#NOTE - This variable is suppose to define 3 shell commands. I do not believe the script is sending these commands as listed because the password does not update.
    commands = [
        "passwd",
        "newpw!",
        "newpw!"
    ]

#NOTE - Attempted to utilize '\n' to execute multiple commands and failed
    # commands = [
    #     "passwd \n newpw! \n newpw!"
    # ]

    # initialize the SSH clientp0-
    client = paramiko.SSHClient()
    # add to known hosts
    client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    try:
        client.connect(hostname=hostname, username=username, password=password)
    except:
        print("[!] Cannot connect to the SSH Server")
        exit()

    # execute the commands
    for command in commands:
        print("="*50, command, "="*50)
        stdin, stdout, stderr = client.exec_command(command)
        print(stdout.read().decode())
        err = stderr.read().decode()
        if err:
            print(err)

change_pw()

【问题讨论】:

    标签: python python-3.x linux ssh paramiko


    【解决方案1】:

    您没有三个命令。你有一个命令,passwd,它需要两行输入。

    这两个问题展示了如何使用 Paramiko 为命令提供输入:

    所以专门针对passwd,需要使用:

    stdin, stdout, stderr = client.exec_command('passwd')
    # answer the new password prompts
    stdin.write('newpw\n')
    stdin.write('newpw\n')
    stdin.flush()
    # wait for the command to complete a print the output
    stdout.channel.set_combine_stderr(True)
    print(stdout.read().decode())
    

    关于Channel.set_combine_stderr,请参阅Paramiko ssh die/hang with big output


    强制警告:不要使用AutoAddPolicy - 这样做会失去对MITM attacks 的保护。如需正确解决方案,请参阅Paramiko "Unknown Server"

    【讨论】:

      【解决方案2】:

      问题是我试图使用 3 个输入命令来更改 root 的密码。我只需要调用 passwd 命令,然后为“输入新密码”和“确认新密码”传递两个输入变量

      import paramiko
      import time
      
      hostname = 'IP'
      username = 'root'
      password = 'oldpw'
      
      
      commands = ['passwd']
      
      
      
      # initialize the SSH clientp
      client = paramiko.SSHClient()
      # add to known hosts
      client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
      try:
          client.connect(hostname=hostname, username=username, password=password)
      except:
          print("[!] Cannot connect to the SSH Server")
          exit()
      
      # execute the commands
      for command in commands:
          print("="*50, 'PW change executed', "="*50)
          stdin, stdout, stderr = client.exec_command(command)
          stdin.write('newpw' '\n' 'newpw' '\n') #input varuables for "Enter new PW" and "re-enter new PW"
          stdin.flush()
          print(stdout.read().decode())
          err = stderr.read().decode()
          if err:
              print(err)
      

      【讨论】:

      • 如果只调用一个命令,for command in commands 的意义何在?该循环并非设计用于多个命令,因为代码仅针对passwd 定制。 + 不要这样使用AutoAddPolicy,这是一个安全漏洞。
      猜你喜欢
      • 2017-04-07
      • 2020-02-15
      • 1970-01-01
      • 2018-11-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多