使用 NSTask 和 rsync 捕获所需的凭据
法国开发商的拳头帖子! 我正在尝试使用 rsync 和 Objective-C 创建一个简单的同步。 所以我像这样使用 NSTask :
NSTask *task = [[NSTask alloc] init];
[task setLaunchPath:@"/usr/bin/rsync"];
NSArray* args = [NSArray arrayWithObjects:@"-av", @"/Users/BiB1/Documents/test/", @"[email protected]:~/test/", nil];
NSDictionary* env = [NSDictionary dictionaryWithObject:<#(id)object#> forKey:<#(id)key#>
[task setArguments:args];
NSPipe *outPipe = [[NSPipe alloc] init];
[task setStandardOutput:outPipe];
[outPipe release];
[task launch];
NSData *data = [[outPipe fileHandleForReading] readDataToEndOfFile];
[task waitUntilExit];
int status = [task terminationStatus];
[task release];
if(status != 0)
{
NSDictionary *eDict = [NSDictionary dictionaryWithObject:@"Sync impossible" forKey:NSOSStatusErrorDomain];
NSError *outError = [NSError errorWithDomain:NSOSStatusErrorDomain code:0 userInfo:eDict];
NSLog(@"EDICT : %@",eDict);
NSLog(@"ERROR : %@",outError);
}
NSString *aString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
[self.textField setStringValue:aString];
[aString release];
在终端中,命令工作正常,但我需要密码。而在 NSTask 中我没有这个请求。
所以我的问题是,有一种方法可以捕获所需的凭据,或者是否可以将密码设置为参数或其他内容。
提前致谢。
比B1
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
来自 rsync man 页面:
from the rsync man page:
据我所知,执行此操作的唯一方法是创建一个单独的程序来提供密码(即“Askpass”程序)并设置环境变量 SSH_ASKPASS。我在这里编写了一组有关如何执行此操作的说明
http://www.mudflatsoftware.com/blog/2010/01/wrapping-rsync-or-ssh-in-an-nstask/
以及此处的源代码示例。
http://bitbucket.org/iracooke/ssh-nstask
虽然这些示例是针对 ssh 的,但它们也适用于rsync。我自己使用这个,虽然设置起来有些复杂,但效果很好。
As far as I know the only way to do this is to create a separate program to supply the password (ie an "Askpass" program) and to set the environment variable SSH_ASKPASS. I've written up a set of instructions on how to do this here
http://www.mudflatsoftware.com/blog/2010/01/wrapping-rsync-or-ssh-in-an-nstask/
and a source code example here.
http://bitbucket.org/iracooke/ssh-nstask
Although the examples are for ssh, they also apply to rsync. I use this myself and it works pretty well although it's somewhat complicated to setup.
我发现避免密码的最佳方法是将证书与 SSH/rsync 一起使用。这是我对 rsync 的调用:
重要的部分是:
-i 指定一个证书作为身份来证明您是谁并自动“登录”。
该证书是通过调用以下命令生成的:
您的备份来源,然后将密钥放在目标上的特定位置
运行此脚本(需要下面的脚本),您将在其中存储带有服务器参数 ip 和端口号的证书对于 ssh
(例如 ./setup.sh //192.168.1.22 23024)
您将需要将此脚本存储在同一位置,名为“remote.sh”,
该脚本将需要用户名和密码一次,但从那时起 rsync 将使用证书作为您的凭证。
key 是私有证书,保存在您的计算机上,用于对传出数据进行签名。
key.pub 是公共证书,保存在用于检查签名数据并确认其来自您的任何计算机上。
.ssh 是您的主目录中的一个特殊文件夹,用于存储与 ssh 一起使用的证书。
我希望这会有所帮助。
I find the best way to avoid passwords is to use certificates with SSH/rsync. This is my call to rsync:
the important part is:
-i specifies a certificate as an identity to prove who you are and 'log in' automatically.
this certificate is generated by calling:
where your backing up from and then putting the key is a specific place on the destination
run this script (which requires the script below) from where you will store your certificate with parameter ip of the server and port number for ssh
(eg ./setup.sh //192.168.1.22 23024)
you will need this script stored in the same location, named 'remote.sh'
the script will require the username and password once, but from then on rsync will use the certificate as your credentials.
key is the private certificate and kept on your computer used to sign outgoing data
key.pub is the public certificate and is kept on any computer used to check the signed data and confirm it is from you.
.ssh is a special folder in your home directory to store certificates for use with ssh..
I hope this helps.