python远程执行linux命令256_Python多线程登录远端linux执行操作命令

什么是进程?

进程,就是程序的一个运行状态。

什么是线程?

为什么要使用线程?

线程,是进程内部的“执行单元”。

一个进程,可以包含多个线程,每个线程又可以执行不同的代码。

即,通过多个线程,可以使一个进程同时执行多个功能!

前提安装sshpass

解法1.#!/usr/bin/python

#-*- coding:utf-8

import threading

import os

def linux_ls1():

while True:

cmd="sshpass -p '00000000' ssh root@192.168.135.108 ls"

ret=os.popen(cmd)

print ret.read()

break

def linux_ls2():

while True:

cmd="sshpass -p '00000000' ssh root@192.168.135.105 ls"

ret=os.popen(cmd)

print ret.read()

break

t1=threading.Thread(target=linux_ls1)

t2=threading.Thread(target=linux_ls2)

t1.start()

t2.start()

解法2.#!/usr/bin/python

#-*- coding:utf-8

import threading

import os

def linux_ls1(pwd,user,ip):

while True:

cmd="sshpass -p '%s' ssh %s@%s ls" %(pwd,user,ip)

ret=os.popen(cmd)

print ret.read()

break

def linux_ls2(pwd,user,ip):

while True:

cmd="sshpass -p '%s' ssh %s@%s ls" %(pwd,user,ip)

ret=os.popen(cmd)

print ret.read()

break

t1=threading.Thread(target=linux_ls1,args=("00000000","root","192.168.135.105"))

t2=threading.Thread(target=linux_ls2,args=("00000000","root","192.168.135.108"))

t1.start()

t2.start()

解法3.#-*- coding:utf-8 -*-

import threading

import os

def my_work(pwd,user,ip):

while True:

cmd="sshpass -p '%s' ssh %s@%s ls" %(pwd,user,ip)

ret=os.popen(cmd)

print ret.read()

break

class MyThread(threading.Thread):

def __init__(self, pwd,user,ip):

threading.Thread.__init__(self)

self.pwd=pwd

self.user=user

self.ip=ip

#线程启动后,会执行self.run()方法

def run(self):

my_work(self.pwd, self.user, self.ip)

# 创建新线程t1

t1 = MyThread("00000000", "root","192.168.135.105")

t2 = MyThread("00000000", "root","192.168.135.108")

t1.start()    #启动线程

t2.start()

print "线程启动完毕"

解法4.#!/usr/bin/python

#-*- coding:UTF-8 -*-

import threading

import time

import os

class MyThread(threading.Thread):

def __init__(self,ip):

threading.Thread.__init__(self)

self.ip=ip

def run(self):

cmd="sshpass -p '00000000' ssh root@%s ls" %self.ip

ret=os.popen(cmd)

print ret.read()

time.sleep(3)

if __name__== '__main__':

t1=MyThread("192.168.135.105")

t2=MyThread("192.168.135.108")

t1.start()

t2.start()

解法5.#!/usr/bin/python

# encoding=utf-8

# Filename: put_files_hdfs.py

# 让多条命令并发执行,如让多条scp,ftp,hdfs上传命令并发执行,提高程序运行效率

import os

import threading

def execCmd(cmd):

try:

os.system(cmd)

except Exception, e:

print '%s\t 运行失败,失败原因\r\n%s' % (cmd,e)

if __name__ == '__main__':

# 需要执行的命令列表

cmds = ['sshpass -p "00000000" ssh root@192.168.135.108 ls','sshpass -p "00000000" ssh root@192.168.135.105 ls',]

#线程池

threads = []

for cmd in cmds:

th = threading.Thread(target=execCmd, args=(cmd,))

th.start()

threads.append(th)

# 等待线程运行完毕

for th in threads:

th.join()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值