python 使用ssh连接服务器进行远程命令行操作
2012-06-20 03:51
1181 查看
ssh $ip “nohup sh go.sh > log 2>&1 &”
如果需要使用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
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
相关文章推荐
- python 使用ssh连接服务器进行远程命令行操作
- Python使用Paramiko模块编写脚本进行远程服务器操作
- 使用Xshell或PuTTY对阿里云服务器进行远程连接操作
- Python使用Paramiko模块编写脚本进行远程服务器操作
- 通过python代码远程连接服务器进行操作之paramiko模块
- 利用python 更新ssh 远程代码 操作远程服务器
- 利用Python实现对linux服务器SSH远程连接和系统操作的几种方式
- python使用socket连接远程服务器的方法
- python使用socket连接远程服务器的方法
- windows使用ssh连接远程服务器
- 使用ssh进行远程连接以及远程跳板以及scp远程上传下载文件说明
- Python使用SocksiPy使paramiko库通过Socks代理SSH连接服务器
- [Python Fabric] [SSH] Mac OS X 10.9 + Vagrant虚拟环境使用Python Fabric进行SSH远程登录的简单实验
- 利用python 更新ssh 远程代码 操作远程服务器的实现代码
- Mac下使用终端连接远程使用ssh协议的git服务器
- [置顶] mac使用Shell(终端)SSH连接远程服务器
- Python 使用 Thrift 连接 HBASE 进行操作
- 使用mac自带ssh连接远程服务器
- win10的bash使用ssh连接远程服务器