python 使用ssh连接服务器进行远程命令行操作

python 使用ssh连接服务器进行远程命令行操作

分类: PYTHON 426人阅读 评论(0) 收藏 举报
ssh python 服务器 command output string

ssh $ip “nohup sh go.sh > log 2>&1 &”

[python] view plain copy
  1. import os  
  2. import re  
  3. import time  
  4. import sys  
  5. import pyssh  
  6. from threading import Thread   
  7.   
  8.   
  9. class SSHController(Thread):   
  10.   
  11.   
  12.     """Connect to remote host with SSH and issue commands. 
  13.     This is a facade/wrapper that uses PySSH to spawn and control an SSH client. 
  14.     You must have OpenSSH installed.  
  15.  
  16.  
  17.     @ivar host_name: Host name or IP address 
  18.     @ivar user_name: User name 
  19.     @ivar password: Password 
  20.     @ivar prompt: Command prompt (or partial string matching the end of the prompt) 
  21.     @ivar ssh: Instance of a pyssh.Ssh object 
  22.     """  
  23.     def __init__(self, host_name, user_name, password, cmd):  
  24.         """ 
  25.         @param host_name: Host name or IP address 
  26.         @param user_name: User name 
  27.         @param password: Password 
  28.         @param prompt: Command prompt (or partial string matching the end of the prompt) 
  29.         """  
  30.         Thread.__init__(self)  
  31.         self.host_name = host_name  
  32.         self.user_name = user_name  
  33.         self.password = password  
  34.         self.port = '22'  #default SSH port  
  35.         self.ssh = None  
  36.         self.cmd = cmd   
  37.   
  38.   
  39.     def login(self):  
  40.   
  41.         """Connect to a remote host and login. 
  42.         """  
  43.         self.ssh = pyssh.Ssh(self.user_name, self.host_name, self.port)  
  44.         self.ssh.login(self.password)  
  45.   
  46.   
  47.     def run_command(self, command):  
  48.         """Run a command on the remote host. 
  49.         @param command: Unix command 
  50.         @return: Command output 
  51.         @rtype: String 
  52.         """  
  53.         response = self.ssh.sendcmd(command)  
  54.         return self.__strip_output(command, response)   
  55.   
  56.   
  57.     def logout(self):  
  58.         """Close the connection to the remote host. 
  59.         """  
  60.         self.ssh.logout()  
  61.   
  62.   
  63.     def run_atomic_command(self, command):  
  64.         """Connect to a remote host, login, run a command, and close the connection. 
  65.         @param command: Unix command 
  66.         @return: Command output 
  67.         @rtype: String 
  68.         """  
  69.         self.login()  
  70.         command_output = self.run_command(command)  
  71.         self.logout()  
  72.         return command_output  
  73.   
  74.   
  75.     def __strip_output(self, command, response):  
  76.         """Strip everything from the response except the actual command output. 
  77.         @param command: Unix command 
  78.         @param response: Command output 
  79.         @return: Stripped output 
  80.         @rtype: String 
  81.         """  
  82.         lines = response.splitlines()  
  83.         # if our command was echoed back, remove it from the output  
  84.         if command in lines[0]:  
  85.             lines.pop(0)  
  86.         # remove the last element, which is the prompt being displayed again  
  87.         lines.pop()  
  88.         # append a newline to each line of output  
  89.         lines = [item + '\n' for item in lines]  
  90.         # join the list back into a string and return it  
  91.         return ''.join(lines)  
  92.   
  93.   
  94.     def run(self):          
  95.         self.run_atomic_command(self.cmd)  
  96.   
  97.   
  98. print time.ctime()  
  99. pinglist = []  
  100. for host in range(1,2):  
  101.    ip = "10.0.0."+str(host)  
  102.    print ip  
  103.    current = SSHController(ip,"tao","123456","ls")  
  104.    pinglist.append(current)  
  105.    current.start()  
  106.   
  107.   
  108. for pingle in pinglist:  
  109.    pingle.join()   
  110.   
  111.   
  112. print time.ctime()  


如果需要使用sudo命令需要到/etc/sudoers 中关闭终端连接选项 开启 tty 功能


通过 /etc/rc.local 或者 cfagent 执行 sudo 命令时,会得到这个错误,意思是执行sudo 的shell 需要一个控制终端。在 /etc/rc.local 或者 cfagent 中的命令,是没有控制终端的。


在 http://www.question-defense.com/2009/03/23/sudo-sorry-you-must-have-a-tty-to-run-sudo/ 上找到了答案,因为是英文的,总结其解决方案如下:


1. 编辑 /etc/sudoers


  1)Defaults    requiretty,修改为 #Defaults    requiretty,表示不需要控制终端。


  2)Defaults    requiretty,修改为 Defaults:nobody !requiretty,表示仅 nobody 用户不需要控制终端。


       如果修改为 Defaults:%nobody !requiretty,表示仅 nobody 组不需要控制终端。


2. 给 ssh 加上 -t 选项,表示不要控制终端。


    ssh -t hostname sudo


可以 man sudoers,获取更多相关信息。






收藏到:Del.icio.us

你可能感兴趣的:(python 使用ssh连接服务器进行远程命令行操作)