mailing lists <listas.cor...@yahoo.es> writes:

> are you sure?
>
> # emerge -s expect                                                            
>                                                         
> [ Results for search key : expect ]
> Searching...
>
> [...]
> *  dev-perl/Expect
>       Latest version available: 1.320.0-r1
>       Latest version installed: [ Not Installed ]
>       Size of files: 61 KiB
>       Homepage:      http://search.cpan.org/dist/Expect/
>       Description:   Expect for Perl
>       License:       || ( Artistic GPL-1+ )

It turned out to be something else, has nothing to do with ssh.


I'm resorting to libssh and do it in C.  For the record, see attachment
--- it'll need some refinement, but it's working.


#include <libssh/libssh.h>
#include <stdlib.h>
#include <stdio.h> 
#include <unistd.h>
#include <string.h>


int interactive_shell_session(ssh_session session)
{
	ssh_channel channel;
	int rc;
	char readbuffer[1024];
	char writebuffer[512];
	int nbytes, nwritten;
	int tries = 0;

	memset(writebuffer, 0, sizeof(writebuffer));
	strcpy(writebuffer, "display environment\n");

	channel = ssh_channel_new(session);
	if (channel == NULL)
	{
		return SSH_ERROR;
	}

	rc = ssh_channel_open_session(channel);
	if (rc != SSH_OK)
	{
		ssh_channel_free(channel);
		return rc;
	}

	rc = ssh_channel_request_pty(channel);
	if (rc != SSH_OK) return rc;
	rc = ssh_channel_change_pty_size(channel, 16, 24);
	if (rc != SSH_OK) return rc;
	rc = ssh_channel_request_shell(channel);
	if (rc != SSH_OK) return rc;


	while ((tries < 4) && ssh_channel_is_open(channel) && !ssh_channel_is_eof(channel))
	{
		nbytes = sizeof(writebuffer);
		nwritten = ssh_channel_write(channel, writebuffer, nbytes);
		if (nwritten != nbytes)
		{
			return SSH_ERROR;
		}

		nbytes = ssh_channel_read_nonblocking(channel, readbuffer, sizeof(readbuffer), 0);
		while (nbytes > 0)
		{
			if (write(1, readbuffer, nbytes) != (unsigned int) nbytes)
			{
				ssh_channel_close(channel);
				ssh_channel_free(channel);
				return SSH_ERROR;
			}

			nbytes = ssh_channel_read_nonblocking(channel, readbuffer, sizeof(readbuffer), 0);
		}
    
		if (nbytes < 0)
		{
			ssh_channel_close(channel);
			ssh_channel_free(channel);
			return SSH_ERROR;
		}

		tries++;
		// printf("\nsleeping\n");
		sleep(20);
	}

	return rc;
}


int main()
{
	ssh_session my_ssh_session;
	int rc;
	char *password;
	int verbosity = SSH_LOG_PROTOCOL;

	//
	// Open session and set options
	//
	my_ssh_session = ssh_new();
	if (my_ssh_session == NULL)
	{
		fprintf(stderr, "Error getting new session\n");
		exit(-1);
	}

	ssh_options_set(my_ssh_session, SSH_OPTIONS_HOST, "switch.example.com");
	ssh_options_set(my_ssh_session, SSH_OPTIONS_USER, "user");
	// ssh_options_set(my_ssh_session, SSH_OPTIONS_LOG_VERBOSITY, &verbosity);
	//
	// Connect to server
	//
	rc = ssh_connect(my_ssh_session);
	if (rc != SSH_OK)
	{
		fprintf(stderr, "Error connecting to remote host: %s\n", ssh_get_error(my_ssh_session));
		ssh_free(my_ssh_session);
		exit(-1);
	}

	//
	// Authenticate ourselves
	//
	password = "secret";
	rc = ssh_userauth_password(my_ssh_session, NULL, password);
	if (rc != SSH_AUTH_SUCCESS)
	{
		fprintf(stderr, "Error authenticating with password: %s\n", ssh_get_error(my_ssh_session));
		ssh_disconnect(my_ssh_session);
		ssh_free(my_ssh_session);
		exit(-1);
	}

	interactive_shell_session(my_ssh_session);
	fprintf(stderr, "\nError: %s\n", ssh_get_error(my_ssh_session));
	/* show_temperature(my_ssh_session); */

	ssh_disconnect(my_ssh_session);
	ssh_free(my_ssh_session);

	exit(0);
}

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/

Reply via email to