using subprocess.popen with ssh - python -
i'm trying run python script host clients via subprocess.popen. command sort of fire , forget , process in clients should run unlimited time untill kill it. problem - when run line in python process run on clients hour , stops after 1 hour , 2 minutes :
subprocess.popen(["rsh {} {} {}".format(ipclient,command,args)], shell=true)
where "command" path , command in clients. , when run rsh 'ip' 'command' 'args'
in shell works expected , not stop suddenly.
any idea?
while subprocess.popen
might work wrapping ssh
access, not preferred way so.
i recommend using paramiko.
import paramiko ssh_client = paramiko.sshclient() ssh_client.set_missing_host_key_policy(paramiko.autoaddpolicy()) ssh_client.connect(server, username=user,password=password) ... ssh_client.close()
and if want simulate terminal, if user typing:
chan=self.ssh_client.invoke_shell() def exec_cmd(cmd): """gets ssh command(s), execute them, , returns output""" prompt='bash $' # command line prompt in ssh terminal buff='' chan.send(str(cmd)+'\n') while not chan.recv_ready(): time.sleep(1) while not buff.endswith(prompt): buff+=self.chan.recv(1024) return buff[:len(prompt)]
example usage: exec_cmd('pwd')
if don't know prompt in advance, can set with:
chan.send('ps1="python-ssh:"\n')
Comments
Post a Comment