如何在python脚本中调用unixshell命令,将文件从源主机sftp到目标服务器操作系统……请帮忙
I have tried the following code
dstfilename="hi.txt"
host="abc.com"
user="sa"
os.system("echo cd /tmp >sample.txt)
os.system("echo put %(dstfilename)s" %locals()) // line 2
os.system("echo bye >>sample.txt")
os.system("sftp -B /var/tmp/sample.txt %(user)s@%(host)s)
How to append this result of line to sample.txt?
os.system("echo put %(dstfilename)s %locals()) >>sample.txt" // Seems this is syntatically not correct.
cat>sample.txt //should look like this
cd /tmp
put /var/tmp/hi.txt
bye
Any help?
Thanks you
您应该将您的命令导入
sftp
。试试这样的方法:但是,为了回答你的问题:
^{pr2}$当然不是。你想传递一个字符串吗操作系统. 所以它必须看起来像
结尾有一个
)
。在字符串表达式由应用了
%
格式的字符串文本组成:字符串文本包含shell的重定向:
一起:
一。但正如前面所说,这是我能想到的最糟糕的解决方案——最好直接写入临时文件,或者直接通过管道导入子进程。在
如果希望在任何sftp命令失败时返回非零代码,则应将这些命令写入文件,然后对其运行sftp批处理。以这种方式,您可以检索返回代码来检查sftp命令是否有任何故障。在
下面是一个简单的例子:
快速免责声明:我没有在shell中运行这个,所以可能会出现错误。如果是这样,请给我一个评论,我会改正的。在
好吧,我想你问题的字面意思应该是这样的:
正如@larsks所说,使用适当的filehandler为您生成tmp文件,我个人的偏好是不使用
locals()
进行字符串格式化。在然而,根据用例的不同,我不认为这是一个特别合适的方法-例如,如何输入sftp站点的密码?在
我想如果你看一下Paramiko中的SFTPClient,你会得到一个更健壮的解决方案,或者如果没有,你可能需要类似pexpect的东西来帮助进行自动化。在
相关问题 更多 >
编程相关推荐