如何使用python 2.3 rsh ssh
我想编写一个脚本来执行以下Linux命令序列:如何使用python 2.3 rsh ssh,python,python-2.3,Python,Python 2.3,我想编写一个脚本来执行以下Linux命令序列: rsh rr01(此处系统将询问密码) ssh rr02(此处系统将再次请求密码) 在某个文件上激活我的应用程序 我天真地(愚蠢地)试图用一个简单的操作系统来实现这一点 请记住,我使用的是Python2.3,并且希望避免添加模块(如果可能的话) 提前谢谢 [EDIT]还可以查看另一个使用paramiko的示例 在上找到的脚本可能会对您有所帮助。它包括os模块(用于查找密钥)和用于日志记录的tempfile模块,这两个模块都可以在Python2.3全
提前谢谢 [EDIT]还可以查看另一个使用paramiko的示例 在上找到的脚本可能会对您有所帮助。它包括os模块(用于查找密钥)和用于日志记录的tempfile模块,这两个模块都可以在Python2.3全局模块索引()和用于SSH2协议实现的paramiko()中找到,这两个模块应该可以在Python2.3及更高版本上工作。这是一段简单的代码:
"""Friendly Python SSH2 interface."""
import os
import tempfile
import paramiko
class Connection(object):
"""Connects and logs into the specified hostname.
Arguments that are not given are guessed from the environment."""
def __init__(self,
host,
username = None,
private_key = None,
password = None,
port = 22,
):
self._sftp_live = False
self._sftp = None
if not username:
username = os.environ['LOGNAME']
# Log to a temporary file.
templog = tempfile.mkstemp('.txt', 'ssh-')[1]
paramiko.util.log_to_file(templog)
# Begin the SSH transport.
self._transport = paramiko.Transport((host, port))
self._tranport_live = True
# Authenticate the transport.
if password:
# Using Password.
self._transport.connect(username = username, password = password)
else:
# Use Private Key.
if not private_key:
# Try to use default key.
if os.path.exists(os.path.expanduser('~/.ssh/id_rsa')):
private_key = '~/.ssh/id_rsa'
elif os.path.exists(os.path.expanduser('~/.ssh/id_dsa')):
private_key = '~/.ssh/id_dsa'
else:
raise TypeError, "You have not specified a password or key."
private_key_file = os.path.expanduser(private_key)
rsa_key = paramiko.RSAKey.from_private_key_file(private_key_file)
self._transport.connect(username = username, pkey = rsa_key)
def _sftp_connect(self):
"""Establish the SFTP connection."""
if not self._sftp_live:
self._sftp = paramiko.SFTPClient.from_transport(self._transport)
self._sftp_live = True
def get(self, remotepath, localpath = None):
"""Copies a file between the remote host and the local host."""
if not localpath:
localpath = os.path.split(remotepath)[1]
self._sftp_connect()
self._sftp.get(remotepath, localpath)
def put(self, localpath, remotepath = None):
"""Copies a file between the local host and the remote host."""
if not remotepath:
remotepath = os.path.split(localpath)[1]
self._sftp_connect()
self._sftp.put(localpath, remotepath)
def execute(self, command):
"""Execute the given commands on a remote machine."""
channel = self._transport.open_session()
channel.exec_command(command)
output = channel.makefile('rb', -1).readlines()
if output:
return output
else:
return channel.makefile_stderr('rb', -1).readlines()
def close(self):
"""Closes the connection and cleans up."""
# Close SFTP Connection.
if self._sftp_live:
self._sftp.close()
self._sftp_live = False
# Close the SSH Transport.
if self._tranport_live:
self._transport.close()
self._tranport_live = False
def __del__(self):
"""Attempt to clean up if not explicitly closed."""
self.close()
def main():
"""Little test when called directly."""
# Set these to your own details.
myssh = Connection('example.com')
myssh.put('ssh.py')
myssh.close()
# start the ball rolling.
if __name__ == "__main__":
main()
Python 2.3?!那太老了!(2003年7月29日)我知道。。。但是我不得不使用这个版本。。。你是说在较新的版本中,我们有简单而优雅的解决方案,而在2.3版本中,没有解决方案或复杂的解决方案?没有。但我不会碰任何强迫我使用九年前的软件的系统。