python 使用ssh连接服务器进行远程命令行操作
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 功能
在 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
本文介绍如何使用Python的SSH库连接服务器,并通过SSH执行远程命令,包括登录、运行命令、输出处理及错误处理。
1924

被折叠的 条评论
为什么被折叠?



