Bash脚本:
ACTIVE_MGMT_1=`ssh -n ${MGMT_IP_1} ". .bash_profile; xms sho proc TRAF.*" 2>/dev/null |egrep " A " |awk '/TRAF/{print $1}' |cut -d "." -f2`;
STANDBY_MGMT_1=`ssh -n ${MGMT_IP_2} ". .bash_profile; xms sho proc TRAF.*" 2>/dev/null |egrep " S " |awk '/TRAF/{print $1}' |cut -d "." -f2`;
MGMT_HOSTNAME_1=`ssh -n ${MGMT_IP_1} hostname 2>/dev/null`;
MGMT_HOSTNAME_2=`ssh -n ${MGMT_IP_2} hostname`2>/dev/null;
ssh -n ${MGMT_IP_1} ". .bash_profile; if [ ! -d "$MGMT_FOLDER" ]; then mkdir $MGMT_FOLDER; fi " 2>/dev/null 1>&2
ssh -n ${MGMT_IP_2} ". .bash_profile; if [ ! -d "$MGMT_FOLDER" ]; then mkdir $MGMT_FOLDER; fi " 2>/dev/null 1>&2
if [ -z "${ACTIVE_MGMT_1}" ] && [ -z "${STANDBY_MGMT_1}" ]; then
log "ERROR" "No active blade on Site: ${SITE_NAME}, first cluster. Skipping";
continue;
fi
log "INFO" "First Active blade: ${ACTIVE_MGMT_1} , standby: ${STANDBY_MGMT_1}";
log "INFO" "First Mng $MGMT_HOSTNAME_1 $MGMT_HOSTNAME_2";
我开始把它翻译成python:
#Check active MGMT on cluster 1
sp = subprocess.Popen(['ssh', '-n', '10.128.136.36', '. .bash_profile; xms sho proc TRAF.*', 'egrep', 'A'], stdout=subprocess.PIPE, stderr=None)
#ACTIVE_MGMT_1=`ssh -n ${MGMT_IP_1} ". .bash_profile; xms sho proc TRAF.*" 2>/dev/null |egrep " A " |awk '/TRAF/{print $1}' |cut -d "." -f2`;
sp = subprocess.Popen(['ssh', '-n', '10.128.136.36', '. .bash_profile; xms sho proc TRAF.*'], stdout=subprocess.PIPE, stderr=None)
#STANDBY_MGMT_1=`ssh -n ${MGMT_IP_2} ". .bash_profile; xms sho proc TRAF.*" 2>/dev/null |egrep " S " |awk '/TRAF/{print $1}' |cut -d "." -f2`;
任何建议如何做其他线路?
答案 0 :(得分:1)
您可以考虑使用像paramiko这样的python模块,并像他们的示例一样发出命令:
import paramiko, base64
key = paramiko.RSAKey(data=base64.decodestring('AAA...'))
client = paramiko.SSHClient()
client.get_host_keys().add('ssh.example.com', 'ssh-rsa', key)
client.connect('ssh.example.com', username='strongbad', password='thecheat')
stdin, stdout, stderr = client.exec_command('. .bash_profile; xms sho proc TRAF.*')
for line in stdout:
# do your grep/awk stuff here!
print '... ' + line.strip('\n')
client.close()
我从来没有在野外使用过那个模块,所以我不确定你能用多个命令做exec_command()
,但我想在远处的主机上编写一个小脚本是个好主意。设置环境以便一切都能正常运行,并调用该脚本。
这个好主意的另一个原因是您只打开一个到服务器的SSH连接,而不是打开/关闭多个会话。