我尝试编写一个非常小的脚本,以便连接到我的raspberrypi,如下所示:
#!/usr/bin/python
#connect to raspberry pi -- ethernet
import os
os.popen('ssh -l pi 169.254.249.166')
但它返回只是立即打印我的密码提示三次 - 我不能写任何东西,我不知道为什么。
在请求之后我可以推送Intro并返回给我提示。
脚本的输出如下:
xxx@xxx:~$ python connect_raspberry.py
xxx@xxx:~$ pi@169.254.249.166's password:
xx@xx:~$ pi@169.254.249.166's password:
Permission denied, please try again.
pi@169.254.249.166's password:
Permission denied, please try again.
pi@169.254.249.166's password:
Permission denied (publickey,password).`
为什么脚本,或者我从提示中猜测的Raspberry,让我输入密码?它只是回答“许可被拒绝”,但我没有输入任何内容。
我真正尝试编写的是在konsole中运行脚本:python code.py
并且它返回给我到Rasp提示符(我想我应该将密码写入变量但是首先,我尝试记录自动并写密码):
xx@xx:~$ python code.py
pi@169.254.249.166's password:
The programs included with the Debian GNU/Linux system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.
Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by applicable law.
Last login: Sun Sep 17 23:16:57 2017 from peces.local
pi@raspberrypi:~ $
另一方面,有关如何将密码写入Python脚本中的变量以自动登录的任何帮助,我的意思是:
xxx@xxx:~$ python connect_raspberry.py
The programs included with the Debian GNU/Linux system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.
Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by applicable law.
Last login: Sun Sep 17 23:16:57 2017 from peces.local
pi@raspberrypi:~ $
对于后者,我不太了解ssh
命令的-f
选项
(此标志表示-n
标志)。
如果它是我自动登录所需要的,我有点困惑。
答案 0 :(得分:0)
os.popen('command')
没有将command
的标准输入连接到您的控制台(您的键盘和显示器),它只是在Python程序中启动一个进程并等待Python与它通信。< / p>
os.popen()
是very old and long since obsolete;你想避免这种情况,并使用更现代的东西。 Python 3.6文档建议使用subprocess.run()
或类似的东西,但是使用裸subprocess
与SSH交互仍然是棘手和繁琐的 - 你真的想要使用知道如何做到这一点的专用库。有人在评论中提出paramiko
,我同意,但也可以探索pexpect
。
import paramiko
ssh = paramiko.SSHClient()
ssh.connect('169.254.249.166', 22, 'pi', 'xyzzy')
stdin, stdout, stderr = ssh.exec_command('python code.py')
# ... interact with your script
ssh.close()
基本上从here复制/粘贴。
此处没有“konsole”,唯一的接口是连接到网络而不是终端窗口的远程ssh
会话。 (如果你的脚本不是交互式的,那么很多都是不必要的,真的。那么来自shell的ssh pi python code.py >output
!)
答案 1 :(得分:0)
感谢有用的答案。
我一直在编写不同的代码,我有这个:
#!/usr/bin/python
#connect to raspberry pi by ethernet
import paramiko
try:
ssh=paramiko.SSHClient()
ssh.load_system_host_keys()
ssh.connect('169.254.249.166', 22, 'pi','raspberry')
except SSHException:
print('error')
stdin, stdout, stderr= ssh.exec_command('cat /etc/motd')
print(stdout.read().splitlines())
var=''
stdin, stdout, stderr= ssh.exec_command('who')
data=stdout.read()
print(data)
while (var!='close'): # loop until close passphrase
print('type a command.')
var=input('>> ')
stdin, stdout, stderr= ssh.exec_command(var)
print('read --> ', stdout.read().splitlines())
print('error --> ', stderr.read())
ssh.close()
运行此代码我可以登录raspberrypi并执行不同类型的命令(但不是所有可用的命令)。
例如,我不能在文件夹之间移动,我的意思是,如果我输入:cd视频到&#39; var&#39;。 stdout是[] 甚至pwd命令总是返回相同的路径:/ home / pi
是输出:
xx@xx:~$ python connect_raspberry.py
[b'', b'The programs included with the Debian GNU/Linux system are free software;', b'the exact distribution terms for each program are described in the', b'individual files in /usr/share/doc/*/copyright.', b'', b'Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent', b'permitted by applicable law.']
b'pi tty1 2017-09-20 10:17\npi :0 2017-09-20 10:17 (:0)\n'
type a command.
>> cd Videos
read --> []
error --> b''
type a command.
>> pwd
read --> [b'/home/pi']
error --> b''
type a command.
>>
连接到树莓是我正在寻找,但是 为什么它不是完美的?我的意思是,为什么有些命令不起作用,比如cd ..或cd / home甚至sudo su
此外,我还尝试将覆盆子中的文件复制到我的电脑上,但它没有运行:
>> sudo cp salida.txt /home/my_user
read --> []
error --> b''
type a command.
>>
在我的电脑里不是salida.txt
再次,感谢您的回答,我得到了我需要但现在出现了新问题