- 浏览: 610450 次
- 性别:
- 来自: 上海
文章分类
最新评论
-
月光杯:
问题解决了吗?
Exceptions in HDFS -
iostreamin:
神,好厉害,这是我找到的唯一可以ac的Java代码,厉害。
[leetcode] word ladder II -
standalone:
One answer I agree with:引用Whene ...
How many string objects are created? -
DiaoCow:
不错!,一开始对这些确实容易犯迷糊
erlang中的冒号 分号 和 句号 -
standalone:
Exception in thread "main& ...
one java interview question
I need to execute commands on several remote servers at the same time via ssh. Writing shell script to send commands to servers with code below is quite simple but has trouble to know when all the tasks are finished on all servers. ssh $ip “nohup sh go.sh > log 2>&1 &” Then I turn to perl, write following perl code:
#!/usr/bin/perl use threads; use Net::SSH::Perl; use strict; my @thread_array; system( "touch $result_file" ); open( ResultHandle, "> $result_file" ) || die "Open result file error...\n"; sub Test { my $host = shift; chomp( $host ); $current_host = $host; print "\nDebug:Try to connect $host for sequence write test...\n"; eval { alarm $time_out; my $ssh = Net::SSH::Perl->new( $host ); $ssh->login($user, $pass); my($out,$err,$exit ) = $ssh->cmd($myCmd); alarm 0; print ResultHandle " Test\n $host \n $out\n \n $err"; print "$host\n$out\n$err\n$exit\n"; }; } my @clients = ("10.0.0.4","10.0.0.6"); for(my $i = 0; $i <= $#clients; $i++){ print "thread $i\n"; $thread_array[$i] = threads->new(\&Test, $clients[$i]); } foreach my $thread( @thread_array ) { $thread -> join( ); }
However, when I run this script, it says “Segment fault” when running more than threads, I do not know why and it stops me moving ahead. Quite disappointed!!!
Then I turn to python…first try the library pyssh:
With following code:
import os import re import time import sys import pyssh from threading import Thread class SSHController(Thread): """Connect to remote host with SSH and issue commands. This is a facade/wrapper that uses PySSH to spawn and control an SSH client. You must have OpenSSH installed. @ivar host_name: Host name or IP address @ivar user_name: User name @ivar password: Password @ivar prompt: Command prompt (or partial string matching the end of the prompt) @ivar ssh: Instance of a pyssh.Ssh object """ def __init__(self, host_name, user_name, password, cmd): """ @param host_name: Host name or IP address @param user_name: User name @param password: Password @param prompt: Command prompt (or partial string matching the end of the prompt) """ Thread.__init__(self) self.host_name = host_name self.user_name = user_name self.password = password self.port = '22' #default SSH port self.ssh = None self.cmd = cmd def login(self): """Connect to a remote host and login. """ self.ssh = pyssh.Ssh(self.user_name, self.host_name, self.port) self.ssh.login(self.password) def run_command(self, command): """Run a command on the remote host. @param command: Unix command @return: Command output @rtype: String """ response = self.ssh.sendcmd(command) return self.__strip_output(command, response) def logout(self): """Close the connection to the remote host. """ self.ssh.logout() def run_atomic_command(self, command): """Connect to a remote host, login, run a command, and close the connection. @param command: Unix command @return: Command output @rtype: String """ self.login() command_output = self.run_command(command) self.logout() return command_output def __strip_output(self, command, response): """Strip everything from the response except the actual command output. @param command: Unix command @param response: Command output @return: Stripped output @rtype: String """ lines = response.splitlines() # if our command was echoed back, remove it from the output if command in lines[0]: lines.pop(0) # remove the last element, which is the prompt being displayed again lines.pop() # append a newline to each line of output lines = [item + '\n' for item in lines] # join the list back into a string and return it return ''.join(lines) def run(self): self.run_atomic_command(self.cmd) print time.ctime() pinglist = [] for host in range(1,2): ip = "10.0.0."+str(host) print ip current = SSHController(ip,"tao","123456","ls") pinglist.append(current) current.start() for pingle in pinglist: pingle.join() print time.ctime()
But again, this script failed. It says: ValueError: signal only works in main thread.I do not know if it’s pyssh library’s problem.
Then I turn to a library named paramiko, and finally I succeed!
First you should install this library; possibly you may need another library pycrypto-2.0.1.
With all things ready, and the following script:
#!/usr/bin/env python import os, sys, socket import paramiko from threading import Thread class myThread(Thread): def __init__ (self, ip, usr, pwd, command): Thread.__init__(self) self.ip = ip self.usr= usr self.pwd= pwd self.command = command def run (self): client = paramiko.SSHClient() client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) client.connect(self.ip, username=self.usr, password=self.pwd) stdin, stdout, stderr = client.exec_command(self.command) if self.command.startswith("sudo") stdin.write(“123456\n”) stdin.flush() for line in stdout.read().splitlines(): print 'host: %s: %s' % (self.ip, line) for line in stderr.read().splitlines(): print 'host: %s: %s' % (self.ip, line) def test (self): pass # some code to test this class mythreads = [] hosts = [] cmd = "" if __name__ == "__main__": num = int(sys.argv[1]) print "total %d nodes\n" % num for i in range(2,2+num): hosts.append("10.0.0." + str(sys.argv[i])) for i in range(2+num,len(sys.argv)): cmd += " " + sys.argv[i] print "cmd to execute: %s\n" % cmd for ip in hosts: t = myThread(ip, "tao","123456",cmd) mythreads.append(t); t.start() for thread in mythreads: thread.join() print "execution on %s completes!\n" % thread.ip print "test done!"
This script accepts arguments like this: 2 1 4 du –h
First argument 2 means I want to execute this command on two hosts, and followed with the last part of ip. ( ip of my machines all have the pattern of 10.0.0.*).
Then the command follows the hosts. If you need to execute a sudo command, remember to modify /etc/sudoers to allow you send ssh commands without a real tty.
You can add a line like this to walk around this problem.
Defaults:tao !requiretty
发表评论
-
lost on Py_DECREF/INCREF when handling PyList_Append in Python C extension
2015-11-03 21:41 1056The main hint is in the docs, i ... -
How can I convert a character to a integer in Python, and viceversa?
2015-10-27 14:38 738Use chr() and ord(): >>& ... -
Date/Time处理函数总结 [To Do]
2013-04-12 10:46 697几种我所用到的用来处理日期,时间的函数总结。 Perl 1 ... -
Twisted中的DeferredList
2013-02-21 17:08 1238在这个blog上学习twisted。 http://blog ... -
python中list类型相减
2009-10-16 09:00 8217两个list ()类型相减 就是比如 a=[1,2,3, ... -
python中的import
2009-10-12 17:00 1098什么时候你应该使用 from module import? ... -
python中的and-or技巧
2009-09-29 09:27 1598>>> a = "first&qu ... -
python中使用可选参数和命名参数
2009-09-28 11:35 3124Python 允许函数参数有缺省值;如果调用函数时不使用参 ... -
python中的连续赋值
2009-09-28 10:10 1900连续值赋值 >>> range(7) ... -
python中的boolean
2009-09-28 09:55 4423在 2.2.1 版本之前,Python 没有单独 ... -
Python 和其他编程语言数据类型的比较
2009-09-27 14:43 1292摘自《Dive into Python》。 静态类型语言 ... -
python中删除非空目录
2009-08-25 21:08 5863import shutil shutil.rmtree ... -
python subprocess
2009-08-13 11:30 3171Module: subprocessPurpose: Spaw ... -
python中使用随机数
2009-08-05 09:35 1544随机整数: >>> import rand ... -
Example of Using getopt in Python
2009-07-24 12:31 1325import getopt, sys def main( ... -
python脚本参数处理的一个技巧
2009-07-23 23:20 1778import sys wid ...
相关推荐
perl-threads-shared-1.43-6.el7.x86_64 perl-threads-1.87-4.el7.x86_64 perl-Filter-1.49-3.el7.x86_64 1:perl-Pod-Simple-3.28-4.el7.noarch perl-Getopt-Long-2.40-2.el7.noarch 4:perl-5.16.3-286.el7.x86...
离线安装包,亲测可用
离线安装包,亲测可用
赠送jar包:jboss-threads-3.1.0.Final.jar; 赠送原API文档:jboss-threads-3.1.0.Final-javadoc.jar; 赠送源代码:jboss-threads-3.1.0.Final-sources.jar; 赠送Maven依赖信息文件:jboss-threads-3.1.0.Final....
Python库`signalr-client-threads-0.0.12.tar.gz`是一个专门为Python开发者设计的 SignalR 客户端库,它允许你在Python应用程序中与使用SignalR服务的Web应用进行实时通信。SignalR是ASP.NET框架下的一项技术,用于...
本文将深入探讨标题所提及的三个实用脚本:“show-busy-java-threads”、“show-duplicate-java-classes”以及“find-in-jars”。这些脚本都是针对Java开发者和系统管理员的利器,旨在提高效率和解决问题。 1. **...
`show-busy-java-threads.sh`脚本就是为了帮助开发者快速定位和排查这类性能问题而设计的。这个脚本主要用于监控并展示Java应用程序中的繁忙线程,从而帮助我们理解程序的执行状态,找出可能导致高CPU负载的原因。 ...
PATH : /usr/local/bin:/usr/bin:/bin:/opt/bin:/c/Windows/System32:/c/Windows:/c/Windows/System32/Wbem:/c/Windows/System32/WindowsPowerShell/v1.0/:/usr/bin/site_perl:/usr/bin/vendor_perl:/usr/bin/core_...
赠送jar包:jboss-threads-3.1.0.Final.jar; 赠送原API文档:jboss-threads-3.1.0.Final-javadoc.jar; 赠送源代码:jboss-threads-3.1.0.Final-sources.jar; 赠送Maven依赖信息文件:jboss-threads-3.1.0.Final....
Using built-in specs. COLLECT_GCC=gcc COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-redhat-linux/4.8.5/lto-wrapper Target: x86_64-redhat-linux Configured with: ../configure --prefix=/usr --mandir=/usr/...
mingw-std-threads-master介绍 mingw-std-threads-master是一个为MinGW(Minimalist GNU for Windows)环境提供的标准线程库实现。MinGW是一个开源项目,它提供了在Windows平台上使用GNU工具集(如GCC)进行软件...
"worker-threads-pool" 是一个专门为Node.js设计的库,它的目的是简化工作线程池的管理和操作。工作线程池可以有效地将CPU密集型任务分配到不同的工作线程中,从而提高程序的执行效率。这个库的主要功能包括创建...
21)..Added: Major improvements in removal of recursive areas from call stack 22)..Added: Statistics collection 23)..Added: Support for uploading multiple files in JIRA 24)..Added: EResLeaks ...
PATH : /usr/local/bin:/usr/bin:/bin:/opt/bin:/c/Windows/system32:/c/Windows:/c/Windows/System32/Wbem:/c/Windows/System32/WindowsPowerShell/v1.0:/usr/bin/site_perl:/usr/bin/vendor_perl:/usr/bin/core_...
Develop a strong set of programming skills with Pyhton that you will be able to express in any situation, on every platform, thanks to Python's portability Stop writing scripts and start architecting ...
适用于x86架构linux的mysql-community-server-5.7.28-x86,文件列表 ...perl-threads-shared-1.43-6.el7.x86_64.rpm perl-Time-HiRes-1.9725-3.el7.x86_64.rpm perl-Time-Local-1.2300-2.el7.noarch.rpm
* Multiple injections from URL, with automatic payloading, using tor proxy, injecting on payloads character encoding in "Hexadecimal", with verbose output and saving results to file (XSSlist.dat&#...
资源分类:Python库 所属语言:Python 资源全名:hanging_threads-1.1.0-py2-none-any.whl 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059
官方离线安装包,测试可用。使用rpm -ivh [rpm完整包名] 进行安装
官方离线安装包,测试可用。使用rpm -ivh [rpm完整包名] 进行安装