python 连接到远程服务器并计算文件数量的脚本
python script to connect to remote server and count the number of files
我需要通过 Python.I 获取远程服务器中的文件数量,我使用了 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()
由于 paramiko 库已经为您建立了 ssh 连接,因此您无需在远程命令中执行 ssh。此外,您似乎正在将输出本地传输到 wc
.. 您不能在此处执行的操作,因为使用 paramiko 您没有使用本地 shell。所以:
如果您要在远程计算机上的 shell 中执行它,那么您的 grepCommand
应该是有效的。
例如:
find /etc -type f | wc -l
我需要通过 Python.I 获取远程服务器中的文件数量,我使用了 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()
由于 paramiko 库已经为您建立了 ssh 连接,因此您无需在远程命令中执行 ssh。此外,您似乎正在将输出本地传输到 wc
.. 您不能在此处执行的操作,因为使用 paramiko 您没有使用本地 shell。所以:
如果您要在远程计算机上的 shell 中执行它,那么您的 grepCommand
应该是有效的。
例如:
find /etc -type f | wc -l