本文整理汇总了Python中paramiko.Transport方法的典型用法代码示例。如果您正苦于以下问题:Python paramiko.Transport方法的具体用法?Python paramiko.Transport怎么用?Python paramiko.Transport使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类paramiko
的用法示例。
在下文中一共展示了paramiko.Transport方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: ssh_edit_file
# 需要导入模块: import paramiko [as 别名]
# 或者: from paramiko import Transport [as 别名]
def ssh_edit_file(adress, user, passw, remotefile, regex, replace):
client = paramiko.SSHClient()
client.load_system_host_keys()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
trans = paramiko.Transport((adress, 22))
trans.connect(username=user, password=passw)
sftp = paramiko.SFTPClient.from_transport(trans)
f_in = sftp.file(remotefile, "r")
c_in = f_in.read()
pattern = re.compile(regex, re.MULTILINE | re.DOTALL)
c_out = pattern.sub(replace, c_in)
f_out = sftp.file(remotefile, "w")
f_out.write(c_out)
f_in.close()
f_out.close()
sftp.close()
trans.close()
示例2: main
# 需要导入模块: import paramiko [as 别名]
# 或者: from paramiko import Transport [as 别名]
def main():
target_ip = etss_range(ip)
for i in target_ip:
try:
t = paramiko.Transport((i, port))
t.connect(username=user, password=passwd)
sftp = paramiko.SFTPClient.from_transport(t)
#Download files
filepath = '/scripts/test.txt'
localpath = 'test.txt'
#dirlist = scp.listdir(filepath)
#print dirlist
#sftp.get(filepath, localpath)
#Upload files
sftp.put(localpath, filepath)
time.sleep(15)
print "SFTP connection established to %s" % (i)
print "File(s) being uploaded ..."
except:
sftp.close()
t.close()
print "Problem connecting with ip: %s" % (i)
continue
示例3: get_remote_server_key
# 需要导入模块: import paramiko [as 别名]
# 或者: from paramiko import Transport [as 别名]
def get_remote_server_key(ip: str, port: int=22) -> paramiko.pkey.PKey:
"""
Returns PKey for given server
:param ip: IP or Hostname
:param port: Port
:return: Returns PKey
"""
my_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
my_socket.settimeout(10)
my_socket.connect((ip, port))
my_transport = paramiko.Transport(my_socket)
my_transport.start_client()
ssh_key = my_transport.get_remote_server_key()
my_transport.close()
my_socket.close()
return ssh_key
示例4: __init__
# 需要导入模块: import paramiko [as 别名]
# 或者: from paramiko import Transport [as 别名]
def __init__(self, local_port, remote_address, remote_port, transport):
"""
type: local_port: int
param: local_port: local tunnel endpoint ip binding
type: remote_address: str
param: remote_address: Remote tunnel endpoing ip binding
type: remote_port: int
param: remote_port: Remote tunnel endpoint port binding
type: transport: :py:class:`paramiko.Transport`
param: transport: Paramiko ssh transport
"""
super(Forward, self).__init__()
self.local_port = local_port
self.remote_address = remote_address
self.remote_port = remote_port
self.transport = transport
示例5: handle_tcp_ssh
# 需要导入模块: import paramiko [as 别名]
# 或者: from paramiko import Transport [as 别名]
def handle_tcp_ssh(socket, dstport):
try:
t = paramiko.Transport(socket)
t.local_version = 'SSH-2.0-OpenSSH_6.6.1p1 Ubuntu-2ubuntu2'
t.load_server_moduli() # It can be safely commented out if it does not work on your system
t.add_server_key(host_key_rsa)
t.add_server_key(host_key_dss)
server = Server(socket.getpeername())
t.start_server(server=server)
t.join()
except Exception as err:
#print(traceback.format_exc())
pass
try:
print("-- SSH TRANSPORT CLOSED --")
t.close()
except:
pass
socket.close()
示例6: __init__
# 需要导入模块: import paramiko [as 别名]
# 或者: from paramiko import Transport [as 别名]
def __init__(self, sock, addr):
super(SshServer, self).__init__()
# tweak InternalApi and RootCmd for non-bgp related commands
self.api = InternalApi(log_handler=logging.StreamHandler(sys.stderr))
setattr(self.api, 'sshserver', self)
self.root = RootCmd(self.api)
self.root.subcommands['help'] = self.HelpCmd
self.root.subcommands['quit'] = self.QuitCmd
transport = paramiko.Transport(sock)
transport.load_server_moduli()
host_key = self._find_ssh_server_key()
transport.add_server_key(host_key)
self.transport = transport
transport.start_server(server=self)
示例7: get_key_file
# 需要导入模块: import paramiko [as 别名]
# 或者: from paramiko import Transport [as 别名]
def get_key_file(ssh_key):
'''根据key内容临时生成一个key文件'''
#file_path = '/tmp/%s'%shortuuid.uuid()
file_path = '/tmp/codo_cmdb_id_rsa'
cmd = 'echo "{}" > {} && chmod 600 {}'.format(ssh_key,file_path,file_path)
ret,stdout = exec_shell(cmd)
if ret == 0:
return file_path
else:
return None
# def remote_upload_file(ip,user,pwd,cmd,local_file,remote_file,port=22):
# '''ssh上传并执行bash for pwd'''
# t = paramiko.Transport((ip,port))
# t.connect(username=user, password=pwd)
# sftp = paramiko.SFTPClient.from_transport(t)
# sftp.put(local_file,remote_file)
# ssh = paramiko.SSHClient()
# ssh._transport = t
# stdin, stdout, stderr = ssh.exec_command(cmd)
# show_log = stdout.read().decode('utf-8').strip()
# t.close()
# return show_log
示例8: __init__
# 需要导入模块: import paramiko [as 别名]
# 或者: from paramiko import Transport [as 别名]
def __init__(self, host, username, password, remote_dir, port=None, keeps=None):
super(SFTPBackend, self).__init__(keeps)
self.host = host
self.port = port or 22
self.username = username
self.password = password
self.remote_dir = remote_dir
# 这一步会建立连接
try:
self.transport = paramiko.Transport((self.host, self.port))
# 登录验证
self.transport.connect(username=self.username, password=self.password)
self.sftp = paramiko.SFTPClient.from_transport(self.transport)
except:
# 这个的关闭,并不会关闭transport的连接
if self.sftp:
self.sftp.close()
# 所以如果登录失败,要记得关闭连接
if self.transport:
self.transport.close()
t, v, tb = sys.exc_info()
reraise(t, v, tb)
示例9: start_server
# 需要导入模块: import paramiko [as 别名]
# 或者: from paramiko import Transport [as 别名]
def start_server(host, port, keyfile, level):
paramiko_level = getattr(paramiko.common, level)
paramiko.common.logging.basicConfig(level=paramiko_level)
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, True)
server_socket.bind((host, port))
server_socket.listen(BACKLOG)
print host,port
while True:
conn, addr = server_socket.accept()
host_key = paramiko.RSAKey.from_private_key_file(keyfile)
transport = paramiko.Transport(conn)
transport.add_server_key(host_key)
transport.set_subsystem_handler(
'sftp', paramiko.SFTPServer, StubSFTPServer)
server = StubServer()
transport.start_server(server=server)
channel = transport.accept()
while transport.is_active():
time.sleep(1)
示例10: _start_sftp_server
# 需要导入模块: import paramiko [as 别名]
# 或者: from paramiko import Transport [as 别名]
def _start_sftp_server():
"""Start the SFTP local server."""
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.setblocking(False)
sock.bind(('localhost', 2222))
sock.listen(10)
reads = {sock}
others = set()
while not event.is_set():
ready_to_read, _, _ = select.select(reads, others, others, 1)
if sock in ready_to_read:
client_socket, address = sock.accept()
ts = paramiko.Transport(client_socket)
host_key = paramiko.RSAKey.from_private_key_file(t_path('server_id_rsa'))
ts.add_server_key(host_key)
server = StubServer()
ts.set_subsystem_handler('sftp', paramiko.SFTPServer, StubSFTPServer)
ts.start_server(server=server)
sock.close()
示例11: __repr__
# 需要导入模块: import paramiko [as 别名]
# 或者: from paramiko import Transport [as 别名]
def __repr__(self):
"""
Returns a string representation of this object, for debugging.
@rtype: str
"""
out = '<paramiko.Transport at %s' % hex(long(id(self)) & 0xffffffffL)
if not self.active:
out += ' (unconnected)'
else:
if self.local_cipher != '':
out += ' (cipher %s, %d bits)' % (self.local_cipher,
self._cipher_info[self.local_cipher]['key-size'] * 8)
if self.is_authenticated():
out += ' (active; %d open channel(s))' % len(self._channels)
elif self.initial_kex_done:
out += ' (connected; awaiting auth)'
else:
out += ' (connecting)'
out += '>'
return out
示例12: __repr__
# 需要导入模块: import paramiko [as 别名]
# 或者: from paramiko import Transport [as 别名]
def __repr__(self):
"""
Returns a string representation of this object, for debugging.
"""
out = '<paramiko.Transport at %s' % hex(long(id(self)) & xffffffff)
if not self.active:
out += ' (unconnected)'
else:
if self.local_cipher != '':
out += ' (cipher %s, %d bits)' % (self.local_cipher,
self._cipher_info[self.local_cipher]['key-size'] * 8)
if self.is_authenticated():
out += ' (active; %d open channel(s))' % len(self._channels)
elif self.initial_kex_done:
out += ' (connected; awaiting auth)'
else:
out += ' (connecting)'
out += '>'
return out
示例13: print_remote_cpu_info
# 需要导入模块: import paramiko [as 别名]
# 或者: from paramiko import Transport [as 别名]
def print_remote_cpu_info(hostname, port, username, password):
client = paramiko.Transport((hostname, port))
client.connect(username=username, password=password)
stdout_data = []
stderr_data = []
session = client.open_channel(kind='session')
session.exec_command(COMMAND)
while True:
if session.recv_ready():
stdout_data.append(session.recv(RECV_BYTES))
if session.recv_stderr_ready():
stderr_data.append(session.recv_stderr(RECV_BYTES))
if session.exit_status_ready():
break
print ('exit status: ', session.recv_exit_status())
print (b''.join(stdout_data))
print (b''.join(stderr_data))
session.close()
client.close()
开发者ID:PacktPublishing,项目名称:Python-Network-Programming-Cookbook-Second-Edition,代码行数:24,代码来源:6_3_print_remote_cpu_info.py
示例14: sftp_conn
# 需要导入模块: import paramiko [as 别名]
# 或者: from paramiko import Transport [as 别名]
def sftp_conn(self):
""""""
try:
self.transport = paramiko.Transport((self.host, self.port))
self.transport.connect(username = self.username, password = self.password)
self.sftp = paramiko.SFTPClient.from_transport(self.transport)
return True
except paramiko.AuthenticationException:
G.log(G.ERROR, "SFTP Authentication failed when connecting to %s" % self.host)
return False
except:
G.log(G.ERROR, "SFTP Could not SSH to %s, waiting for it to start" % self.host)
return False
#----------------------------------------------------------------------
示例15: ssh_command
# 需要导入模块: import paramiko [as 别名]
# 或者: from paramiko import Transport [as 别名]
def ssh_command(ip, user, passwd, command):
transport = paramiko.Transport(ip)
try:
transport.start_client()
except Exception as e:
print(e)
try:
transport.auth_password(username=user,password=passwd)
except Exception as e:
print(e)
if transport.is_authenticated():
print(transport.getpeername())
channel = transport.opem_session()
channel.exec_command(command)
response = channel.recv(1024)
print('Command %r(%r)-->%s' % (command,user,response))
开发者ID:PacktPublishing,项目名称:Mastering-Python-for-Networking-and-Security,代码行数:20,代码来源:SSH_transport.py