使用Parami自动执行sshkeygen

2024-04-27 02:48:14 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个lxd容器master,它有一个IP 10.154.151.8。我想通过我的机器上的ssh对它执行ssh-keygen。也就是说

import paramiko
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect('10.154.151.8',username='root')
stdin,stdout,stderr = ssh.exec_command('ssh-keygen')

我在这里面临的问题是,在给出命令ssh-keygen之后,询问位置密码短语(两次)。在交互式shell中,我通常只需按三次回车键。但是在这里,因为我通过ssh连接到master,所以我不能这样做。输出为:

Generating public/private rsa key pair.
Enter file in which to save the key (/home/rohit/.ssh/id_rsa): 
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /home/rohit/.ssh/id_rsa.
Your public key has been saved in /home/rohit/.ssh/id_rsa.pub.
The key fingerprint is:

所以我试着做:

stdin.write("\n")
stdin.write("\n")
stdin.write("\n")

但这并没有创建id_rsa.pub文件。你知道吗

我也试过:

stdin.write("") 
stdin.write("") 
stdin.write("") 

但这也不管用。你知道吗

那么,我做错什么了?你知道吗

此外,有没有其他方法(最好使用paramiko)来生成id_rsa.pub文件?你知道吗

感谢您的帮助!你知道吗


Tags: keyinmasteridparamikohomestdinrsa