2012-06-04 124 views
0

我很抱歉我的英文不好。 我搜索我的问题的解决方案有一天,但没有找到。使用python ssh运行本地shell脚本到远程

这是我的问题: 我有一些经理shell脚本在服务器-A。 我使用

ssh [email protected]_server_ip < shell_script.sh 

运行正常。

我想在python中做到这一点。 我是测试这一点:

1\paramiko, 'exec_command (str)' is only run ONE command.and i use stdin to invoke_shell,not ok 
2\pexect,sendline() is only ONE command. 

请帮帮我,谢谢!

(一些AIX不支持SFT,所以我不希望使用SFTP脚本到其他服务器。)

这样的shell脚本:

#!/bin/sh 
if [ $# -lt 1 ] 
os=`uname` 
if [ "$os" = "linux" ] || [ "$os" = "Linux" ] 
then 
    temp=`df -P $diskname| tail -1` 
    if [ "$temp" = "" ] 
    then 
    echo "error!t=$diskname not found" 
    exit 0 
    fi 
    # diskutil=`echo $temp|awk '{printf("%s",$5)}'|awk '{gsub("%",""); print $0}'` 
    disk_size=`echo $temp | awk '{print $2}'` 
    disk_size_mb=`expr $disk_size/1024` 
    disk_size=`echo | awk '{ printf("%.2f",(c1/1024.0)) }' c1=$disk_size_mb` 
    disk_size="${disk_size}" 

elif [ "$os" = "SunOS" ] 
then 
    temp=`df -k $diskname | tail -1` 
    .... 

elif [ "$os" = "AIX" ] || [ "$os" = "aix" ] 
then 
    temp=`df -k $diskname |tail -1|sed -e "s/%//g"` 
    .... 
else 
    echo "error!!=Unsupported platform: $os" 
    exit 
fi 

echo "Total Size=$disk_size_mb" 

回答

0

如果只是触发从Python的脚本是足够,请使用标准库中的subprocess.call。从看你的脚本,我猜它正在一些远程主机上运行,​​可能是并行的。您可能想看看优秀的fabric模块。这是一个围绕paramiko的包装,它极大地方便了本地和远程运行命令以及上下文件传输。

+0

谢谢simon.I会找到面料和测试。 –