2013-11-26 247 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 –