我需要在/ home / pi下的src文件夹中运行flask_functions.py应用,但无法在该特定文件夹中调用function。
from paramiko import SSHClient, AutoAddPolicy
def Connect(ip, username='pi', pw='password'):
'''ssh into the pi'''
print('connecting to {}@{}...'.format(username, ip))
ssh = SSHClient()
ssh.set_missing_host_key_policy(AutoAddPolicy())
ssh.connect(ip, username=username, password=pw)
print('connection status =', ssh.get_transport().is_active())
return ssh
def SendCommand(ssh, command, pw='password'):
'''send a terminal/bash command to the ssh'ed-into machine '''
print('sending a command... ', command)
stdin, stdout, stderr = ssh.exec_command( command )
if "sudo" in command:
stdin.write(pw+'\n')
stdin.flush()
print('\nstout:',stdout.read())
print('\nsterr:',stderr.read())
myssh = Connect(ip='10.224.172.138', username='pi', pw='raspberry')
SendCommand(myssh, command='pwd') (prints /home/pi)
SendCommand(myssh, command='cd ~/src & python3 flask_functions.py')
SendCommand(myssh, command='pwd') (prints /home/pi)
这是我的输出:
connecting to pi@10.224.172.138...
('connection status =', True)
('sending a command... ', 'pwd')
('\nstout:', '/home/pi\n')
('\nsterr:', '')
('sending a command... ', 'cd ~/src & python3 flask_functions.py')
('\nstout:', '')
('\nsterr:', "python3: can't open file 'flask_functions.py': [Errno 2] No such file or directory\n")
('sending a command... ', 'pwd')
('\nstout:', '/home/pi\n')
总是返回根目录,所以我不能正确运行我的flask应用程序。 如何在特定文件夹中运行脚本?预先感谢。