我正在尝试使用paramiko嵌套SSH,在那里我将连接到系统A,然后从那里我将连接到系统B。在这里,要使用用户名和密码身份验证连接到系统AI,并使用用户名ssh_key连接到系统B 。与系统A的连接很好,但是在尝试与系统B通讯时出现错误
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(sys_A, 22, user, password)
print "Logged successfully to System A"
#
sshtransport = client.get_transport()
dest_addr = (sys_B, 22) # edited#
local_addr = (sys_A, 22) # edited#
sshchannel = sshtransport.open_channel("direct-tcpip", dest_addr, local_addr)
#
print "Logging to the node given"
node_client = paramiko.SSHClient()
node_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
k = paramiko.RSAKey.from_private_key_file('/home/test_user/test/ssh_key')
node_client.connect(sys_B, username='root', pkey=k, sock=sshchannel)
print "Login successful to the node"
#
stdin, stdout, stderr = node_client.exec_command(cmd) # edited#
stdoutstr = ''
stderrstr = ''
for line in stdout.readlines():
stdoutstr = stdoutstr + line + '\n'
for line in stderr.readlines():
stderrstr = stderrstr + line + '\n'
return (stdoutstr, stderrstr)
我收到以下错误消息:
----------------------------------------------------------------------
Traceback (most recent call last):
File "isolated_vm_test_base.py", line 173, in ssh_lpar_node_and_run_command
k = paramiko.RSAKey.from_private_key_file('/home/test_user/test/id_rsa')
File "/usr/lib/python2.7/site-packages/paramiko/pkey.py", line 196, in from_private_key_file
key = cls(filename=filename, password=password)
File "/usr/lib/python2.7/site-packages/paramiko/rsakey.py", line 45, in __init__
self._from_private_key_file(filename, password)
File "/usr/lib/python2.7/site-packages/paramiko/rsakey.py", line 163, in _from_private_key_file
data = self._read_private_key_file('RSA', filename, password)
File "/usr/lib/python2.7/site-packages/paramiko/pkey.py", line 268, in _read_private_key_file
data = self._read_private_key(tag, f, password)
File "/usr/lib/python2.7/site-packages/paramiko/pkey.py", line 277, in _read_private_key
raise SSHException('not a valid ' + tag + ' private key file')
SSHException: not a valid RSA private key file
这是我的私钥的样子
-----BEGIN OPENSSH PRIVATE K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