嗨我正在使用python并尝试连接到sftp并想从那里检索xml文件并需要放在我的本地系统中,下面是代码
import paramiko
sftpURL = 'sftp.somewebsite.com'
sftpUser = 'user_name'
sftpPass = 'password'
ssh = paramiko.SSHClient()
# automatically add keys without requiring human intervention
ssh.set_missing_host_key_policy( paramiko.AutoAddPolicy() )
ssh.connect(sftpURL, username=sftpUser, password=sftpPass)
ftp = ssh.open_sftp()
files = ftp.listdir()
print files
Run Code Online (Sandbox Code Playgroud)
这里连接已成功完成,现在我想查看所有文件夹和所有文件,并需要输入所需的文件夹以从那里检索xml文件.
最后我的目的是在连接到sftp服务器后查看所有文件夹和文件.在上面的代码中我使用了ftp.listdir()通过它获得输出,如下所示
['.bash_logout', '.bash_profile', '.bashrc', '.mozilla', 'testfile_248.xml']
Run Code Online (Sandbox Code Playgroud)
我想知道这些是否是唯一存在的文件?
我上面使用的命令也是查看文件夹的权利吗?
查看所有文件夹和文件的命令是什么
我正在使用SSH编写用于静态路由管理的Java GUI程序.我的代码如下:
import com.jcraft.jsch.*;
import java.io.*;
public class Konsep {
String status;
static String username;
static String hostname;
String inputcommand;
String output;
static Session session;
JSch jsch = new JSch();
public String status(String stringstatus) {
stringstatus = status;
return stringstatus;
}
public String InputCommand(String inputcommandstatus) {
inputcommandstatus = inputcommand;
return inputcommandstatus;
}
public void connect(String usernamelokal, String hostnamelokal,
String password, int port) {
// JSch jsch=new JSch();
try {
Session sessionlokal = jsch.getSession(usernamelokal,
hostnamelokal, port);
sessionlokal.setPassword(password);
UserInfo ui = new …Run Code Online (Sandbox Code Playgroud) 我正在使用下面Connect()的SshJava类中的方法,以便使用SSH(JSch)连接到服务器并在服务器中运行命令.
问题是运行Connect()服务器时会提示下一条消息:
Kerberos username [********]:
Kerberos password for ********:
Run Code Online (Sandbox Code Playgroud)
并且为了继续运行,我需要手动按Enter两次键,一个用于用户名,一个用于密码.我试图添加下一个代码:
// Press ENTER
Robot r = new Robot();
r.keyPress(KeyEvent.VK_ENTER);
r.keyRelease(KeyEvent.VK_ENTER);
Run Code Online (Sandbox Code Playgroud)
但是这段代码只适用于用户名,我无法弄清楚当服务器要求输入密码时如何自动按ENTER键.到目前为止,我已经尝试将另一个代码片段作为上面显示的代码片段
session.connect();
Run Code Online (Sandbox Code Playgroud)
线.
package ConnectSSH;
import java.awt.Robot;
import java.awt.event.KeyEvent;
import java.io.*;
import com.jcraft.jsch.ChannelExec;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.UserInfo;
public class Ssh{
private static final String user = "********";
private static final String host = "********";
private static final Integer port = 22;
private static final String pass = "********";
public void …Run Code Online (Sandbox Code Playgroud) 我无法连接到AWS Transfer for SFTP.我成功设置了服务器并尝试使用WinSCP进行连接.
我设置了一个具有信任关系的IAM角色,如下所示:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Service": "transfer.amazonaws.com"
},
"Action": "sts:AssumeRole"
}
]
}
Run Code Online (Sandbox Code Playgroud)
我使用主目录和主目录将文档中描述的范围向下策略与此配对homebuckethomedir
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "ListHomeDir",
"Effect": "Allow",
"Action": [
"s3:ListBucket",
"s3:GetBucketAcl"
],
"Resource": "arn:aws:s3:::${transfer:HomeBucket}"
},
{
"Sid": "AWSTransferRequirements",
"Effect": "Allow",
"Action": [
"s3:ListAllMyBuckets",
"s3:GetBucketLocation"
],
"Resource": "*"
},
{
"Sid": "HomeDirObjectAccess",
"Effect": "Allow",
"Action": [
"s3:DeleteObjectVersion",
"s3:DeleteObject",
"s3:PutObject",
"s3:GetObjectAcl",
"s3:GetObject",
"s3:GetObjectVersionAcl",
"s3:GetObjectTagging",
"s3:PutObjectTagging",
"s3:PutObjectAcl",
"s3:GetObjectVersion"
],
"Resource": …Run Code Online (Sandbox Code Playgroud) 我尝试将文件上传到FTP服务器上的目录.我用这个方法FtpWebRequest.我想将一个文件上传到该用户的主目录,但我总是收到以下错误消息:
请求的URI对此FTP命令无效.
有什么问题?我试过关闭被动模式,但它仍然是一样的.
static void FtpUpload()
{
// Get the object used to communicate with the server.
FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://12.22.44.45");
request.Method = WebRequestMethods.Ftp.UploadFile;
request.UsePassive = false;
// This example assumes the FTP site uses anonymous logon.
request.Credentials = new NetworkCredential("pokus", "password");
// Copy the contents of the file to the request stream.
StreamReader sourceStream = new StreamReader(path);
byte[] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());
sourceStream.Close();
request.ContentLength = fileContents.Length;
Stream requestStream = request.GetRequestStream();
requestStream.Write(fileContents, 0, fileContents.Length);
requestStream.Close();
FtpWebResponse response = (FtpWebResponse)request.GetResponse(); …Run Code Online (Sandbox Code Playgroud) 我正在使用Apache Commons FTP上传文件.在上传之前,我想检查服务器上是否已存在该文件,并从该服务器备份到同一服务器上的备份目录.
有谁知道如何将文件从FTP服务器复制到同一台服务器上的备份目录?
public static void uploadWithCommonsFTP(File fileToBeUpload){
FTPClient f = new FTPClient();
FTPFile backupDirectory;
try {
f.connect(server.getServer());
f.login(server.getUsername(), server.getPassword());
FTPFile[] directories = f.listDirectories();
FTPFile[] files = f.listFiles();
for(FTPFile file:directories){
if (!file.getName().equalsIgnoreCase("backup")) {
backupDirectory=file;
} else {
f.makeDirectory("backup");
}
}
for(FTPFile file: files){
if(file.getName().equals(fileToBeUpload.getName())){
//copy file to backupDirectory
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
Run Code Online (Sandbox Code Playgroud)
编辑过的代码:仍有问题,当我备份zip文件时,备份文件已损坏.
有没有人知道它的原因?
public static void backupUploadWithCommonsFTP(File fileToBeUpload) {
FTPClient f = new FTPClient();
boolean backupDirectoryExist = false;
boolean fileToBeUploadExist = …Run Code Online (Sandbox Code Playgroud) 我需要连接到能够使用lftp成功连接的FTPS服务器.但是,当我尝试使用Python ftplib.FTP_TLS时,它会超时,堆栈跟踪显示它正在等待服务器发送欢迎消息等.有谁知道问题是什么以及如何克服?我想知道是否需要在服务器端进行某些操作,但是lftp客户端如何正常工作.任何帮助是极大的赞赏.
这是堆栈跟踪:
ftp = ftplib.FTP_TLS()
ftp.connect(cfg.HOST, cfg.PORT, timeout=60)
File "C:\Users\username\Softwares\Python27\lib\ftplib.py", line 135, in connect
self.welcome = self.getresp()
File "C:\Users\username\Softwares\Python27\lib\ftplib.py", line 210, in getresp
resp = self.getmultiline()
File "C:\Users\username\Softwares\Python27\lib\ftplib.py", line 196, in getmultiline
line = self.getline()
File "C:\Users\username\Softwares\Python27\lib\ftplib.py", line 183, in getline
line = self.file.readline()
File "C:\Users\username\Softwares\Python27\lib\socket.py", line 447, in readline
data = self._sock.recv(self._rbufsize)
socket.timeout: timed out
Run Code Online (Sandbox Code Playgroud)
使用lftp成功登录到同一个ftps服务器:
$ lftp
lftp :~> open ftps://ip_address:990
lftp ip_address:~> set ftps:initial-prot P
lftp ip_address:~> login ftps_user_id ftps_user_passwd
lftp sftp_user_id@ip_address:~> ls
ls: Fatal error: …Run Code Online (Sandbox Code Playgroud) 我的程序可以使用以下代码将文件上传到FTP服务器:
WebClient client = new WebClient();
client.Credentials = new System.Net.NetworkCredential(ftpUsername, ftpPassword);
client.BaseAddress = ftpServer;
client.UploadFile(fileToUpload, WebRequestMethods.Ftp.UploadFile, fileName);
Run Code Online (Sandbox Code Playgroud)
现在我需要删除一些文件,我不能这样做.我该怎么用而不是
client.UploadFile(fileToUpload, WebRequestMethods.Ftp.UploadFile, fileName);
Run Code Online (Sandbox Code Playgroud) 我知道有一些简单的命令登录到远程计算机通过VBA通过指定即User@hostname与-pw选项中提供的密码Wshell.Run函数,其中Wshell为CreateObject("WScript.Shell").
但是,我需要使用私钥文件(在PuTTY中的Connection/SSH/Auth选项下指定),同时通过Excel-VBA宏登录.有办法吗?
我需要它,因为我在我的VBA代码的第二种形式上使用管理员帐户(第一种形式是使用用户自己的帐户),因此无法在第二种形式上提供密码文本框.因此,唯一安全的解决方案是通过公钥和私钥使用SSH登录.
提前致谢!
ftp ×4
ssh ×4
.net ×3
java ×3
c# ×2
jsch ×2
python ×2
sftp ×2
amazon-iam ×1
amazon-s3 ×1
command-line ×1
delete-file ×1
ftp-client ×1
ftplib ×1
ftps ×1
gssapi ×1
keep-alive ×1
kerberos ×1
paramiko ×1
private-key ×1
putty ×1
session ×1
ssl ×1
vb.net ×1
webclient ×1