0

hello guys i have three server and i mange it from SSH so i made this script to run my Register script "Register.py" so everyday i turn on Register mode so the problem how i can login to multiple SSH connection without close the other

import paramiko
import os
ZI1={"ip":"192.168.1.2","pass":"Administrator"}
ZI2={"ip":"192.168.1.3","pass":"AdminTeachers"}
ZI3={"ip":"192.168.1.4","pass":"AdminStudents"}
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
for F1 in ZI1:
    ssh.connect(ZI1["ip"],username='root', password=ZI1["pass"])
    ssh.exec_command('./register.py -time 6') #6 hour so the script still working for 6 hours
    ssh.close()
for F2 in ZI2:
    ssh.connect(ZI2["ip"],username='root', password=ZI2["pass"])
    ssh.exec_command('./register.py -time 6')
    ssh.close()
for F3 in ZI3:
    ssh.connect(ZI2["ip"],username='root', password=ZI2["pass"])
    ssh.exec_command('./register.py -time 6')
    ssh.close()

so what i have to do to open 3 sessions without stopping script !!

3
  • Have you tried creating 3 instances of paramiko.SSHClient() and placing them in the separator threads? Commented Jun 7, 2011 at 12:44
  • @Johnsyweb, he is asking a different question about the same script. It doesn't look like a dup to me Commented Jun 7, 2011 at 12:50
  • @Mike Pennington, @jack-X: I sit corrected, with apologies. Commented Jun 7, 2011 at 21:19

3 Answers 3

1

I'd suggest looking at Fabric. It may help you with working with SSH connections.

Sign up to request clarification or add additional context in comments.

Comments

1

The way you are currently doing it blocks beacuse you are not logging out of the hosts for six hours.

Multiprocessing:

If you need to see return codes from the script, you should open your connections to each host using python's multiprocessing module.

nohup:

Another method (that will not allow you to see the script's return value via paramiko) is to use nohup to disassociate the script from the shell. That will put it in the background and allow you to logout. To do this use...

    ssh.exec_command('nohup ./register.py -time 6 &') 

Typos:

BTW, you had typos in the last loop... ZI2 should be ZI3 in the last loop... furthermore, the for-loops are unnecessary... I fixed your very last iteration... Acks to @johnsyweb for spotting more of the OP's typos than I did...

ssh.connect(ZI3["ip"],username='root', password=ZI3["pass"])
ssh.exec_command('./register.py -time 6')   # <------------- missing s in ssh
ssh.close()

4 Comments

Up !!! please help me i got error (<class 'socket.error'>, error(10061, 'No connection could be made because the t arget machine actively refused it'), <traceback object at 0x0290BDC8>)
That's not the only error in that last for-loop, it is also referencing ZI2, presumably a copy & paste error from the previous block. More pertinently: why are you using a for-loop here? There's no iteration other than doing everything twice (because there are two elements in that dict). I maintain that cron is the best approach here.
@Johnsyweb, I agree with what you said... I wrote this as I was preparing to walk out the door for work this morning; I should have spotted the issues with the for loop. As for cron, perhaps that is true, but if this is a homework assignment, he may be handcuffed to paramiko... I will leave that for the OP to comment on.
He he. I know what it's like. I made a similar mistake by marking this as a duplicate when I was rushing off to do something else.
0

Another way is using Thread if you need do some action based on return of Register.py

See example:

import paramiko
import os
import sys
from threading import Thread

SERVER_LIST = [{"ip":"192.168.1.2","pass":"Administrator"},{"ip":"192.168.1.4","pass":"AdminStudents"},{"ip":"192.168.1.3","pass":"AdminTeachers"}]



class ExecuteRegister(Thread):
    def __init__ (self,options):
        Thread.__init__(self)
        self.options = options       
        self.ssh = paramiko.SSHClient()
        self.ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())



    def run(self):
        try:
           self.ssh.connect(self.options['ip'],username='root', password=self.options["pass"])
           self.ssh.exec_command('./register.py -time 6') #6 hour so the script still working for 6 hours
           self.ssh.close()
        except:
           print sys.exc_info()



for server in SERVER_LIST:
    Register = ExecuteRegister(server)
    Register.start()

1 Comment

mr.olarva does your code make the script still working without close it? (session still open?) also i got that error ! (<class 'socket.er onnected party did onnection failed b ct at 0x02888D78>) thanks a lot !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.