使用libgit2sharp-SSH使用SshUserKeyCredentials进行身份验证时遇到一些问题:
var co = new CloneOptions();
co.CredentialsProvider = (_url, _user, _cred) => new SshUserKeyCredentials { PrivateKey="C:\\path\\to\\private_key" };
Repository.Clone("git@... .repository.git", path, co);
Run Code Online (Sandbox Code Playgroud)
我发现SshUserKeyCredentials对象浏览源代码,所以我的第一个问题是,是否可以使用此对象从gitlab部署基于密钥的签出?
该对象似乎需要PrivateKey,Username,PublicKey和Passphrase的任意组合.我目前正在使用PrivateKey.
我最终得到的错误:
{"Failed to start SSH session: Unable to exchange encryption keys"}
Run Code Online (Sandbox Code Playgroud)
如果这种方式不起作用,是否有另一种方法可以使用部署密钥以编程方式管理来自C#环境的git?
使用的操作系统
Windows 7 - V 6.1
Run Code Online (Sandbox Code Playgroud)
安装了Apache
httpd-2.2.22-win32-x86-no_ssl.msi
Run Code Online (Sandbox Code Playgroud)
我的httpd.conf
AddType application/x-httpd-php .php
LoadModule php5_module "X:/Program Files/PHP/php5apache2_2.dll"
Run Code Online (Sandbox Code Playgroud)
已安装php版本
php-5.3.16-Win32-VC9-x86.msi
Run Code Online (Sandbox Code Playgroud)
我复制了php_ssh2-0.11.2-5.3-nts-vc9-x86.zip中未压缩的php_ssh2.dll
php_ssh2-0.11.2-5.3-nts-vc9-x86.zip
Run Code Online (Sandbox Code Playgroud)
至
X:\Program Files\PHP\ext\php_ssh2.dll
Run Code Online (Sandbox Code Playgroud)
我的php.ini
extension_dir="X:\Program Files\PHP\ext"
PHPIniDir "X:/Program Files/PHP"
[PHP_SSH2]
extension=php_ssh2.dll
Run Code Online (Sandbox Code Playgroud)
在使用php_ssh2.dll配置php.ini并将文件php_ssh2.dll复制到extension_dir之后,Apache已重新启动 - 错误仍然存在
Fatal error: Call to undefined function ssh2_connect() in X:\Program Files\Apache Software Foundation\Apache2.2\htdocs\index.php on line 4
Run Code Online (Sandbox Code Playgroud)
用于测试的代码
<?php
$connection = ssh2_connect("XXX.XXX.XXX.XXX", 22);
if(ssh2_auth_password($connection, "XXXXXX", "XXXXXX"))
printf("CONNECTED");
else
printf("ERROR");
?>
Run Code Online (Sandbox Code Playgroud)
使用phpinfo()的测试在浏览器中完美地显示页面,因为php配置了httpd.conf,但参数extension_dir显示为
extension_dir C:\php C:\php
Run Code Online (Sandbox Code Playgroud)
但此参数设置为"X:\ Program Files\PHP\ext"
我正在使用SSH Ganymed库别名Trilead别名Orion.
我试图理解会话的确切行为,因为我需要保持ssh连接打开很长时间(也许永远),并在我的jvm关闭或类似的事情时关闭它.
所以,我的问题是这个.假设我做了这样的事情:
Connection conn = new Connection(this.hostName, this.port);
conn.addConnectionMonitor(new ConnectionMonitor()
{
@Override
public void connectionLost(Throwable reason)
{
System.out.println("Connection Lost " reason.getMessage());
}
});
conn.connect(null, 1000, 20000);
conn.authenticateWithPublicKey(this.user, keyfile, this.password);
Thread.sleep(30000); //sleep the Thread for 30 seconds
Session sess = conn.openSession();
sess.execCommand("ls");
conn.close();
Run Code Online (Sandbox Code Playgroud)
并且,在线程休眠的30秒内,我断开网络接口以模拟网络问题.
1)connectionMonitor不拦截disconnect事件,并且不打印Connection Lost消息2)When
Session sess = conn.openSession();
Run Code Online (Sandbox Code Playgroud)
执行后,进程阻塞,没有任何反应,直到我再也没有连接网络接口.这是因为,查看Ganymed代码,似乎由于未检测到断开事件,会话被打开,并且会打开会话直到成功.
所以我的问题是:1)这种行为是否需要或者这是一个错误?2)有没有办法在Connection.openSession()方法和Connection.connect()方法中设置超时?
提前致谢.
ssh_exec()拒绝在Windows中执行命令,我遇到了一个噩梦.
这是我的代码:
<?php
$connection = ssh2_connect('localhost', 22);
ssh2_auth_none($connection, 'root');
$stream = ssh2_exec($connection, 'C:\Program Files\CCleaner\CCleaner.exe',FALSE);
?>
Run Code Online (Sandbox Code Playgroud)
它向我显示以下警告:Unable to request a channel from remote host in.如果您有任何提示,请告诉我.谢谢.
我正在尝试安装ssh2.所以我把libssh2.dll文件windows\system32文件夹比使用cmd我试图加载这个文件作为手段PHP在Windows机器上安装SSH2
我写
regsvr32 libssh2.dll
Run Code Online (Sandbox Code Playgroud)
在此之后,我得到以下错误.
The module "libssh2.dll" was loaded but the entry-pointDllRegisterServer was not found.
Make sure that "libssh2.dll" is a valid DLL or OCX file and then try it again
Run Code Online (Sandbox Code Playgroud) 我正在尝试libgit2使用 SSH 密钥对 git 服务器进行身份验证。到目前为止,这适用于像 的 URL ssh://myuser@host.domain:1234/dirs/repo.git,其中我的应用程序接受 URL 作为参数。
但是,如果我从 URL 中删除用户名(即ssh://host.domain:1234/dirs/repo.git)连接失败,即使我以编程方式设置了用户名(见下文)。
考虑以下用于检查某个存储库是否可访问的程序的 MCVE(除必要外,没有错误检查):
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include <pwd.h>
#include <git2.h>
#include <git2/sys/repository.h>
int my_cred_cb(git_cred **out, const char *url,
const char *username_from_url, unsigned int allowed_types, void *payload)
{
uid_t uid = geteuid(); // Get effective user ID
struct passwd* pw = getpwuid(uid); // Get password file entry
char privkeypath[strlen(pw->pw_dir) + 13];
strcpy(privkeypath, pw->pw_dir);
strcat(privkeypath, "/.ssh/id_rsa"); …Run Code Online (Sandbox Code Playgroud) 我正在寻找一种顺序执行多个命令的方法。我现在所做的是为每个命令创建一个新频道并关闭它。如果我只使用一个频道,我会收到一个错误,提示一个频道不能重复使用。但我不确定这是否是正确的方法,因为为每个命令打开一个通道听起来很昂贵。
我想要做的是创建一个到 OpenWrt 设备的 ssh 连接,该设备包含一个名为 uci 的可执行文件,它可以修改设备上的配置文件并像这样使用它:
uci set network.lan.ipaddr='192.168.1.2'
uci set network.lan.dns='192.168.1.1'
Run Code Online (Sandbox Code Playgroud)
我的代码类似于:
let tcp = TcpStream::connect("127.0.0.1:22").unwrap();
let mut sess = Session::new().unwrap();
sess.set_tcp_stream(tcp);
sess.handshake().unwrap();
sess.userauth_agent("username").unwrap();
let mut channel = sess.channel_session().unwrap();
channel.exec("ls").unwrap();
channel.wait_close();
println!("#1 exit: {}", channel.exit_status().unwrap());
let mut channel = sess.channel_session().unwrap();
channel.exec("ls").unwrap();
channel.wait_close();
println!("#2 exit: {}", channel.exit_status().unwrap());
Run Code Online (Sandbox Code Playgroud)
如果我不关闭通道并按顺序执行 2 个命令,我会收到错误代码 -39(错误使用)。
尝试 ./configure libcurl 7.22.0 时出现以下错误
链接时可用的一个或多个库在运行时不可用。链接时使用的库:-lssh2 -lssl -lcrypto -lrt -lz
当我使用--without-libssh2 ./configure 时,它工作得很好。
我已采取的步骤:
apt-get install libssl-dev
apt-get install libssh-dev
cd /var
wget http://www.libssh2.org/download/libssh2-1.3.0.tar.gz
tar -zxvf libssh2-1.3.0.tar.gz
cd libssh2-1.3.0
./configure
make
make install
Run Code Online (Sandbox Code Playgroud)
顺便说一句,SSL 支持运行良好。我一定是 libssh 做错了什么
我也尝试过:
./configure --with-libssh2
./configure --with-libssh2-path=/usr/local/lib
./configure --with-libssh2=/usr
./configure --with-libssh2=/usr/local/lib
Run Code Online (Sandbox Code Playgroud)
但这并没有什么区别。我不知道还能尝试什么。
<?php
$ssh = ssh2_connect('domain.tld');
ssh2_auth_password($ssh, 'username', 'password');
$shell = ssh2_shell($ssh);
echo fread($shell, 1024*1024);
fwrite($shell, "sudo ls -la\n");
$output = fread($shell, 1024*1024);
echo $output;
if (preg_match('#[pP]assword[^:]*:#', $output)) {
fwrite($shell, "password\n");
echo fread($shell, 1024*1024);
}
Run Code Online (Sandbox Code Playgroud)
所做的只是显示横幅和提示。它实际上并没有给我 ls -la 命令的输出。在 phpseclib 上它工作得很好。
有任何想法吗?
我正在尝试通过 libssh2 的 no-ack 的 python byndings 将一组文件发送到远程服务器,但由于缺乏文档,我完全不知道库的使用情况。
我尝试使用 libssh2 的 C 文档但没有成功。
由于我使用的是 python 3.2,所以 paramiko 和 pexpect 是不可能的。有人可以帮忙吗?
编辑:我刚刚在 no-Ack 的博客评论中找到了一些代码。
import libssh2, socket, os
SERVER = 'someserver'
username = 'someuser'
password = 'secret!'
sourceFilePath = 'source/file/path'
destinationFilePath = 'dest/file/path'
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((SERVER, 22))
session = libssh2.Session()
session.startup(sock)
session.userauth_password(username, password)
sourceFile = open(sourceFilePath, 'rb')
channel = session.scp_send(destinationFilePath, 0o644, os.stat(sourceFilePath).st_size)
while True:
data = sourceFile.read(4096)
if not data:
break
channel.write(data)
exitStatus = channel.exit_status()
channel.close()
Run Code Online (Sandbox Code Playgroud)
似乎工作正常。