jack-X
jack-X

Reputation: 287

python dealing with SSH [many clients ]

i mange 3 server [ Linux ] and i have to turn on scripts on these servers every 6 hours

so it's take a lite bit time to login in each one .. so i made this code

import paramiko
import os
ZI1={"ip":"192.168.1.2","pass":"server-1"}
ZI2={"ip":"192.168.1.3","pass":"Server-2"}
ZI3={"ip":"192.168.1.4","pass":"server-3"}
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
SPAM=1
while SPAM==3:
    ssh.connect(ZI1["ip"],username='root', password=ZI1["pass"])
    stdin, stdout, stderr = ssh.exec_command('perl Register.pl')
    print stdout.readlines()
    SPAM+=1
    ssh.connect(ZI2["ip"],username='root', password=ZI2["pass"])
    stdin, stdout, stderr = ssh.exec_command('perl Register.pl')
    print stdout.readlines()
    SPAM+=1
    ssh.connect(ZI2["ip"],username='root', password=ZI2["pass"])
    stdin, stdout, stderr = ssh.exec_command('perl Register.pl')
    print stdout.readlines()
    ssh.close()
    SPAM+=1

well it's not working as well ;( i wana to enter each one and run the script and go to another server without closing the connection or terminating the script so please help me.

after edit

now i edit it and i got new issues

i add this to my code :

import paramiko
import os
ZI1={"ip":"192.168.1.2","pass":"server-1"}
ZI2={"ip":"192.168.1.3","pass":"Server-2"}
ZI3={"ip":"192.168.1.4","pass":"server-3"}
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())

for F1 in ZI1:
    ssh.connect(ZI1["ip"],username='root', password=ZI1["pass"])
    stdin, stdout, stderr = ssh.exec_command('uname -a')
    print stdout.readlines()
    ssh.close()
for F2 in ZI2:
    ssh.connect(ZI2["ip"],username='root', password=ZI2["pass"])
    ssh.exec_command('ls -la')
    print stdout.readlines()
    ssh.close()
for F3 in ZI3:
    ssh.connect(ZI2["ip"],username='root', password=ZI2["pass"])
    ssh.exec_command('pwd')
    print stdout.readlines()
    ssh.close()

and i give each one different command to see does it work and i got very stranger output

['Linux xxxx 2.6.18-028stab070.14 #1 SMP Thu Nov 18 16:04:02 MSK 2010 x 86_64 x86_64 x86_64 GNU/Linux\n'] ['Linux xxxx 2.6.18-028stab070.14 #1 SMP Thu Nov 18 16:04:02 MSK 2010 x 86_64 x86_64 x86_64 GNU/Linux\n'] [] [] [] []

i said first server do the command uname -a and second one do the command ls -la but it's give them together the same command third server doesn't execute anything

Upvotes: 1

Views: 690

Answers (4)

plaes
plaes

Reputation: 32716

IMHO, you are approaching this problem from the wrong end...

Instead of making a connection from remote host, why don't you better set up cron scripts on the remote machines.

Upvotes: 0

Michael Kent
Michael Kent

Reputation: 1734

You probably should be using Fabric to do this. It would be a lot simpler for you.

Upvotes: 2

Winston Ewert
Winston Ewert

Reputation: 45039

for F1 in ZI1:

What the fried monkey are you trying to do here? For is repeating something, but you don't want to repeat anything.

Do you see the difference between these two lines of code:

stdin, stdout, stderr = ssh.exec_command('uname -a')

ssh.exec_command('ls -la')

Your second and third servers are both still using the stdout from the first server. You need to have the stdin, stdout, stderr again.

Upvotes: 3

tylerl
tylerl

Reputation: 30857

It appears that your problem is that you're calling stdout.readlines(), which is going to continue to read the output from the SSH command until there is no more input to read... so your program doesn't advance until then. As a simple solution, you can execute each command in its own thread, or you can fork and execute it in its own process space.

Upvotes: 0

Related Questions