您的位置:首页 > 编程语言 > Python开发

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

2012-06-20 03:51 1181 查看
ssh $ip “nohup sh go.sh > log 2>&1 &”

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()


如果需要使用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 <cmd>

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

收藏到:Del.icio.us
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: