我需要通过Python获取远程服务器中的文件数。我使用了paramiko模块并编写了下面的脚本。在终端中运行时的linux命令给了我想要的输出,而当从Python执行时,输出为[u' 0 \ n']。非常感谢任何帮助。
#!/usr/local/bin/python
import paramiko
client=paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect('hostname', username='username')
grepCommand="ssh username@hostname 'find /usr/local/somefolder1/somefolder2 -type f -exec ls {} \;'|wc -l"
stdin,stdout,stderr = client.exec_command(grepCommand)
data=stdout.readlines()
print data
client.close()
答案 0 :(得分:0)
由于paramiko库已经为您建立了ssh连接,因此您不需要在远程命令中执行ssh。此外,您似乎将输出本地传输到wc
..这是您无法做到的事情,因为paramiko您没有使用本地shell。所以:
如果要在远程计算机上的shell中执行它,那么grepCommand
应该是正常的。
例如:
find /etc -type f | wc -l