Skip to content

Instantly share code, notes, and snippets.

@calavera
Created January 31, 2011 16:30
Show Gist options
  • Save calavera/804309 to your computer and use it in GitHub Desktop.
Save calavera/804309 to your computer and use it in GitHub Desktop.
/* example ex4.c */
/* compile with: gcc -g -Wall ex4.c -o ex4 -lvirt */
#include <stdio.h>
#include <stdlib.h>
#include <libvirt/libvirt.h>
#include <string.h>
static int authCreds[] = {
VIR_CRED_AUTHNAME,
VIR_CRED_PASSPHRASE,
};
static int authCb(virConnectCredentialPtr cred, unsigned int ncred, void *cbdata)
{
int i;
char buf[1024];
for (i = 0; i < ncred; i++) {
if (cred[i].type == VIR_CRED_AUTHNAME) {
printf("%s: ", cred[i].prompt);
fflush(stdout);
fgets(buf, sizeof(buf), stdin);
buf[strlen(buf) - 1] = '\0';
cred[i].result = strdup(buf);
if (cred[i].result == NULL)
return -1;
cred[i].resultlen = strlen(cred[i].result);
}
else if (cred[i].type == VIR_CRED_PASSPHRASE) {
printf("%s: ", cred[i].prompt);
fflush(stdout);
fgets(buf, sizeof(buf), stdin);
buf[strlen(buf) - 1] = '\0';
cred[i].result = strdup(buf);
if (cred[i].result == NULL)
return -1;
cred[i].resultlen = strlen(cred[i].result);
}
}
return 0;
}
int main(int argc, char *argv[])
{
virConnectPtr conn;
virConnectAuth auth;
auth.credtype = authCreds;
auth.ncredtype = sizeof(authCreds)/sizeof(int);
printf("ncredtype = %d", auth.ncredtype);
fflush(stdout);
auth.cb = authCb;
auth.cbdata = NULL;
conn = virConnectOpenAuth("esx://10.60.1.73?no_verify=1", &auth, 0);
if (conn == NULL) {
fprintf(stderr, "Failed to open connection to esx://10.60.1.73?no_verify=1\n");
return 1;
}
virConnectClose(conn);
return 0;
}
require 'rubygems'
require 'bundler/setup'
require 'libvirt'
AuthCallback = Proc.new do |cred, ncred, _|
1.upto(ncred) do |n|
cred_type = cred[n]
case cred_type.type
when :authname, :passphrase
STDOUT.print cred_type.prompt
STDOUT.flush
answer = STDIN.gets
return -1 unless answer
cred_type.resultlen = answer.chomp
end
return 0
end
end
auth = FFI::Libvirt::ConnectAuth.new
auth[:credtype][0] = :authname
auth[:credtype][1] = :passphrase
auth[:ncredtype] = 2
auth[:cb] = AuthCallback
auth[:cbdata] = nil
conn = FFI::Libvirt.virConnectOpenAuth("esx://10.60.1.73?no_verify=1", auth.to_ptr, 0)
p "CONNECTION!!!!! #{conn}"
FFI::Libvirt.virConnectClose(conn) unless conn
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment