[英]ssh using python(without paramiko and fabric)
I have a requirement to write a python script to do a ssh login from one server to the other. 我需要编写python脚本来从一个服务器到另一个服务器进行ssh登录。 Unfortunately these servers do not have any internet connection and i am not able to download packages.
不幸的是,这些服务器没有任何互联网连接,我无法下载软件包。 I have written the below code using python sub process but once the ssh command is executed stdin.write dose not input the password.
我已经使用python子进程编写了以下代码,但是一旦执行ssh命令,stdin.write将不会输入密码。 I have tried p.communicate also but to no avail.
我也尝试了p.communicate,但无济于事。 Please help me on this script to ssh without using paramiko or fabric.
请在不使用paramiko或fabric的情况下帮助我将此脚本转换为ssh。
cmd = "ssh root@17.10.14.243"
p = subprocess.Popen(cmd, stdin = subprocess.PIPE, stdout = subprocess.PIPE, stderr = subprocess.PIPE, bufsize = 1,shell = True)
p.stdin.write("Pass")
I hope that's not your real password in the question! 我希望这不是您的真实密码!
ssh
won't read passwords from stdin
, so using p.communicate
or various writes sadly won't work. ssh
不会从stdin
读取密码,因此使用p.communicate
或其他各种写操作将无法正常工作。
If it's always these two machines, you could ensure that they both have ssh identities and then make an authorized_keys
file. 如果总是这两台机器,则可以确保它们都具有ssh身份,然后制作一个
authorized_keys
文件。 If you don't set a password when creating an ssh identity with ssh-keygen
, and that key is in another user@host's authorized_keys
file, then you'll be let in without a password: 如果在使用
ssh-keygen
创建ssh身份时未设置密码,并且该密钥在另一个user @ host的authorized_keys
文件中,那么您将无需密码即可进入:
ssh-keygen -t rsa
ssh-copy-id user@other-host
If ssh-copy-id
doesn't exist on your system, then you want to put the contents of $HOME/.ssh/id_rsa.pub
into the other host's $HOME/.ssh/authorized_keys
file (which can have multiple entries) 如果系统上不存在
ssh-copy-id
, $HOME/.ssh/id_rsa.pub
将$HOME/.ssh/id_rsa.pub
放入另一台主机的$HOME/.ssh/authorized_keys
文件中(该文件可以具有多个条目)
There is also ssh-askpass
, if that's installed on your system. 如果您的系统上已经安装了
ssh-askpass
,也可以使用。 Setting the SSH_ASKPASS
environment variable to the name of a script will run that script to get the password to use, if ssh is run without an attached terminal. 如果ssh在没有连接终端的情况下运行,则将
SSH_ASKPASS
环境变量设置为脚本名称将运行该脚本以获取要使用的密码。 So if /path/to/myscript
prints out mypassword
, then export SSH_ASKPASS=/path/to/myscript
will use mypassword
when there's no terminal available. 因此,如果
/path/to/myscript
打印出mypassword
,则在没有可用终端时, export SSH_ASKPASS=/path/to/myscript
将使用mypassword
。
Both options have their security concerns, so it depends on the environment you have and what you're most worried about. 这两个选项都有其安全性问题,因此取决于您所拥有的环境以及您最担心的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.