使用 NSTask 和 rsync 捕获所需的凭据

发布于 2024-09-29 03:23:18 字数 1382 浏览 7 评论 0 原文

法国开发商的拳头帖子! 我正在尝试使用 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

Fist post for a french developer!
I'm trying to create a simple synchronization using rsync and objective-c.
So I used NSTask like that :

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];

In the terminal the command work fine but I have a request for the password. And in NSTask I don't have this request.

So my question is, there is a method for catching crendential needed, or is it possible to set password as parameter or something else.

Thanks by advance.

BiB1

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

橘味果▽酱 2024-10-06 03:23:18

来自 rsync man 页面:

远程守护程序上的某些模块可能需要身份验证。
如果是这样,您将收到一个密码
连接时提示。您可以通过设置来避免密码提示
环境变量
RSYNC_PASSWORD 为您要使用的密码或使用 --password-file 选项。这可能会用到——
编写 rsync 脚本时 full。

警告:在某些系统上,所有用户都可以看到环境变量。在那些使用的系统上
推荐使用 --password-file。

from the rsync man page:

Some modules on the remote daemon may require authentication.
If so, you will receive a password
prompt when you connect. You can avoid the password prompt by setting the
environment variable
RSYNC_PASSWORD to the password you want to use or using the --password-file option. This may be use-
ful when scripting rsync.

WARNING: On some systems environment variables are visible to all users. On those systems using
--password-file is recommended.

新一帅帅 2024-10-06 03:23:18

据我所知,执行此操作的唯一方法是创建一个单独的程序来提供密码(即“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.

梦明 2024-10-06 03:23:18

我发现避免密码的最佳方法是将证书与 SSH/rsync 一起使用。这是我对 rsync 的调用:

rsync -azvr --stats --rsh="ssh -p${ssh port} -i /path/to/certificate/key" ${source} ${destination}

重要的部分是:

--rsh="ssh -p${ssh port} -i /path/to/certificate/key"

-i 指定一个证书作为身份来证明您是谁并自动“登录”。

该证书是通过调用以下命令生成的:

ssh-keygen -t rsa -b 2048 -f key

您的备份来源,然后将密钥放在目标上的特定位置

运行此脚本(需要下面的脚本),您将在其中存储带有服务器参数 ip 和端口号的证书对于 ssh
(例如 ./setup.sh //192.168.1.22 23024)

echo "generate key"
ssh-keygen -t rsa -b 2048 -f key
echo "push key to server"
rsync -avz --rsh="ssh -p$2" key.pub $1:~/.ssh/key.pub
echo "put key in authorized_keys on server"
ssh -p$2 $1 'bash -s' < ../remote.sh

您将需要将此脚本存储在同一位置,名为“remote.sh”,

#!/bin/bash

mkdir .ssh
cd .ssh
cat key.pub > authorized_keys

exit

该脚本将需要用户名和密码一次,但从那时起 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:

rsync -azvr --stats --rsh="ssh -p${ssh port} -i /path/to/certificate/key" ${source} ${destination}

the important part is:

--rsh="ssh -p${ssh port} -i /path/to/certificate/key"

-i specifies a certificate as an identity to prove who you are and 'log in' automatically.

this certificate is generated by calling:

ssh-keygen -t rsa -b 2048 -f key

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)

echo "generate key"
ssh-keygen -t rsa -b 2048 -f key
echo "push key to server"
rsync -avz --rsh="ssh -p$2" key.pub $1:~/.ssh/key.pub
echo "put key in authorized_keys on server"
ssh -p$2 $1 'bash -s' < ../remote.sh

you will need this script stored in the same location, named 'remote.sh'

#!/bin/bash

mkdir .ssh
cd .ssh
cat key.pub > authorized_keys

exit

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文