0

Bash script:

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"; 

i start to translate it to 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`; 

any advise how to do the other lines?

Tim Pierce
  • 5,514
  • 1
  • 15
  • 31
  • 1
    Please format your code so that your question is readable. – Adam Matan Dec 08 '13 at 13:25
  • 1
    WHERE is the Question? Please avoid ambiguity or open-end questions. Your question should be at the very beginning of your post, be a concise and concrete short sentence ending with a "?"! ;-) So the recipients can quickly determine if they are fit to answer. Then add what your **specific** probleme is (e.g. error) add some code and error logs. – Don Question Dec 08 '13 at 14:02
  • 1
    You may find a more elegant solution here: http://stackoverflow.com/questions/3586106/perform-commands-over-ssh-with-python – Tim Pierce Dec 08 '13 at 15:01

1 Answers1

1

You could consider using a python module like paramiko, and issue your command like their example:

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()

I never used that module in the wild, so I'm not sure that you can do exec_command() with multiple commands, but I guess it could be a good idea to write a little script on the distant host that sets up the environment so that everything can work well, and just call that script.

Another reason why this is a good idea is that you open only one SSH connection to the server instead of opening/closing several sessions.

zmo
  • 24,463
  • 4
  • 54
  • 90