【发布时间】: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