Permission denIEd (publickey). The remote end hung up unexpectedly
从终端运行相同的命令工作正常,所以我认为这可能是NSTask没有设置将在访问ssh密钥的过程中使用的环境变量的问题.我尝试手动设置HOME和USER环境变量,如下所示:
[task setEnvironment:[NSDictionary dictionaryWithObjectsAndKeys:NSHomeDirectory(),@"HOME",NSUsername(),@"USER",nil]];
但这没有效果.是否有任何特定的环境变量我必须在NSTask中设置才能使其正常工作?
编辑:感谢达斯汀的提示,我进一步搞清楚了.我使用env命令列出当前会话的环境变量,我发现了这个:
SSH_AUTH_SOCK=/tmp/launch-DMQopt/Listeners
为了测试,我复制了该路径并将其设置为NSTask的环境变量并再次运行代码,这次它起作用了!也就是说,我确定每个会话的SSH_AUTH_SOCK都会发生变化,所以我不能只对它进行硬编码.如何动态生成/检索此变量?
解决方法 您可以尝试按照教程“ Wrapping rsync or SSH in an NSTask”(从 Ira开始),它提到了SSH_AUTH_SOCK变量:Since writing this post I’ve realised that I omitted an important additional step in setting up the environment variables for the NSTask.
In order to make passwordless key-based authentication work it’s necessary to grab theSSH_AUTH_SOCK
variable from the user’s environment and include this in the NSTask’s environment.
So,when setting environment variables for example;
NSTask *task;NSDictionary *environmentDict = [[nsprocessInfo processInfo] environment];// Environment variables needed for password based authentication NSMutableDictionary *env = [NSMutableDictionary dictionaryWithObjectsAndKeys: @"NONE",@"disPLAY",askPasspath,@"SSH_ASKPASS",username,@"AUTH_USERname",hostname,@"AUTH_HOSTname",nil];// Environment variable needed for key based authentication[env setobject:[environmentDict objectForKey:@"SSH_AUTH_SOCK"] forKey:@"SSH_AUTH_SOCK"];// Setting the task's environment[task setEnvironment:env];
但是,OP indragie评论:
I had trIEd this earlIEr but since it was being invoked with XCode,the
SSH_AUTH_SOCK
env var. wasn’t being passed to it.
opening the app from Finder corrects this issue.
With
askPasspath
being the path for the Askpass executable,which is included as part of the application’s main bundle. (In order to do this,find the executable under “Products” in xcode,and then drag it into “copy Bundle Resources” on the main application’s target.)
// Get the path of the Askpass program,which is// setup to be included as part of the main application bundleNsstring *askPasspath = [NSBundle pathForResource:@"Askpass" ofType:@"" inDirectory:[[NSBundle mainBundle] bundlePath]];总结
以上是内存溢出为你收集整理的objective-c – NSTask和Git – 权限问题全部内容,希望文章能够帮你解决objective-c – NSTask和Git – 权限问题所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)