2013-11-26 159 views
-2

我發現這個代碼連接到遠程sftp服務器的用戶名,密碼和主機的幫助下,但我也需要包括端口號,任何一個可以讓他們知道如何包括端口在這段代碼中的數字,也是這段代碼 'parmiko.util.log_to_file(log_filename)'我應該怎樣爲log_filename進行硬編碼? Iam在unix環境中運行此代碼。python代碼連接到sftp服務器

import os 
import paramiko 
server, username, password = ('host', 'username', 'password') 
ssh = paramiko.SSHClient() 
parmiko.util.log_to_file(log_filename)  
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) 

' #In case the server's key is unknown,' 
#we will be adding it automatically to the list of known hosts 
ssh.load_host_keys(os.path.expanduser(os.path.join("~", ".ssh", "known_hosts"))) 

#Loads the user's local known host file 
ssh.connect(server, username=username, password=password) 
ssh_stdin, ssh_stdout, ssh_stderr = ssh.exec_command('ls /tmp') 

print "output", ssh_stdout.read() #Reading output of the executed co'mmand 
error = ssh_stderr.read() 

#Reading the error stream of the executed command 
print "err", error, len(error) 

#Transfering files to and from the remote machine' 
sftp = ssh.open_sftp() 
'sftp.get(remote_path, local_path)' 
sftp.put(local_path, remote_path) 
sftp.close() 
ssh.close() 

回答

1

有一個port=命名參數ssh.connect()方法。見manual

例子:

ssh.connect(server, port=portnumber, username=username, password=password) 
+0

了那裏,第二個我以前那樣:P +1 –