[英]python paramiko ssh
我是 python 新手。 我寫了一個腳本來連接主機並執行一個命令
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(host, username=user, password=pw)
print 'running remote command'
stdin, stdout, stderr = ssh.exec_command(command)
stdin.close()
for line in stdout.read().splitlines():
print '%s$: %s' % (host, line)
if outfile != None:
f_outfile.write("%s\n" %line)
for line in stderr.read().splitlines():
print '%s$: %s' % (host, line + "\n")
if outfile != None:
f_outfile.write("%s\n" %line)
ssh.close()
if outfile != None:
f_outfile.close()
print 'connection to %s closed' %host
except:
e = sys.exc_info()[1]
print '%s' %e
當遠程命令不需要 tty 時工作正常。 我找到了一個帶有 Paramiko 的 invoke_shell 示例嵌套 SSH 會話。 我對這個解決方案不滿意,因為如果服務器的提示未在我的腳本中指定 -> 無限循環或腳本中指定的提示是返回文本中的字符串 -> 並非所有數據都將被接收. 有沒有更好的解決方案,比如在我的腳本中將 stdout 和 stderr 發回?
接受的答案有問題,有時(隨機)會從服務器帶來剪輯響應。 我不知道為什么,我沒有調查接受答案的錯誤原因,因為這段代碼非常適合我:
import paramiko
ip='server ip'
port=22
username='username'
password='password'
cmd='some useful command'
ssh=paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(ip,port,username,password)
stdin,stdout,stderr=ssh.exec_command(cmd)
outlines=stdout.readlines()
resp=''.join(outlines)
print(resp)
stdin,stdout,stderr=ssh.exec_command('some really useful command')
outlines=stdout.readlines()
resp=''.join(outlines)
print(resp)
您可以在以下位置找到大量 paramiko API 文檔: http ://docs.paramiko.org/en/stable/index.html
我使用以下方法在受密碼保護的客戶端上執行命令:
import paramiko
nbytes = 4096
hostname = 'hostname'
port = 22
username = 'username'
password = 'password'
command = 'ls'
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(nbytes))
if session.recv_stderr_ready():
stderr_data.append(session.recv_stderr(nbytes))
if session.exit_status_ready():
break
print 'exit status: ', session.recv_exit_status()
print ''.join(stdout_data)
print ''.join(stderr_data)
session.close()
client.close()
@ThePracticalOne的代碼非常適合顯示用法,除了一件事:有時輸出會不完整。( session.recv_ready()
在if session.recv_ready():
而session.recv_stderr_ready()
和session.exit_status_ready()
在進入下一個循環之前變為真)
所以我的想法是在准備退出會話時檢索數據。
while True:
if session.exit_status_ready():
while True:
while True:
print "try to recv stdout..."
ret = session.recv(nbytes)
if len(ret) == 0:
break
stdout_data.append(ret)
while True:
print "try to recv stderr..."
ret = session.recv_stderr(nbytes)
if len(ret) == 0:
break
stderr_data.append(ret)
break
無密碼 SSH 為我工作
import paramiko
def connect_SSH():
ssh = paramiko.SSHClient()
username = '<uname>'
port = <port-no>
ip = '<ip-address>'
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(ip,port,username)
stdin, stdout, stderr = ssh.exec_command('<cmd>')
outlines = stdout.readlines()
resp=''.join(outlines)
print(resp)
connect_SSH()
ThePracticalOne - 你是英雄! 我遇到了 exec_command(它是 Client 的成員)的問題 我嘗試在 Windows 服務器上通過 ssh 運行 powershell 命令,並且只有您的示例
client = paramiko.Transport((hostname, port))
client.connect(username=username, password=password)
和
while True:
if session.recv_ready():
stdout_data.append(session.recv(nbytes))
if session.recv_stderr_ready():
stderr_data.append(session.recv_stderr(nbytes))
if session.exit_status_ready():
break
對我有幫助!
###### Use paramiko to connect to LINUX platform############
import paramiko
ip='server ip'
port=22
username='username'
password='password'
ssh=paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(ip,port,username,password)
--------Connection Established-----------------------------
######To run shell commands on remote connection###########
import paramiko
ip='server ip'
port=22
username='username'
password='password'
ssh=paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(ip,port,username,password)
stdin,stdout,stderr=ssh.exec_command(cmd)
outlines=stdout.readlines()
resp=''.join(outlines)
print(resp) # Output
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.