Not able to navigate to desired folder on remote Linux machine, how do i do that using paramiko?

十年热恋 提交于 2019-12-18 09:52:24

问题


I am Using Pyhton paramiko and My website server has folder structure like this-

1]dir1
      --dirP
      --dirQ
2]dir2
      --dirA
          --file.sh
      --dirB
3]dir3

where i want to access file.sh from dirA inside dir2 folder

I tried this-

import paramiko
client.connect('mysite.com', username='something', password='something')
stdin, stdout, stderr = client.exec_command('cd dir2')
stdin, stdout, stderr = client.exec_command('ls')
for line in stdout:
    print('... ' + line.strip('\n'))

but i get output-

  ...dir1
  ...dir2
  ...dir3

expected output is-

...dirA
...dirB

and also suggest how can i execute file.sh too?


回答1:


client.exec_command("cmd ...") is just like the command ssh user@host "cmd ..." so

client.exec_command('cd dir2')
client.exec_command('ls')

are just like

ssh user@host 'cd dir2' # this would not affect the following `ls'
ssh user@host 'ls'

. So you need to do like this:

client.exec_command('cd dir2; ls')

which is just like

ssh user@host 'cd dir2; ls'



回答2:


Or if you use variables need to add + for example

client.exec_command('str(var1)+str(var2))


来源:https://stackoverflow.com/questions/42897381/not-able-to-navigate-to-desired-folder-on-remote-linux-machine-how-do-i-do-that

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!