Re: run perl script to configure remote computer using sudo

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 2018-11-08 9:51 p.m., D&R wrote:
> I have tried several ways to connect to remote computer:
> 
> $SERVER = qx{ssh -o ConnectTimeout=2 -o ControlPersist=yes $user\@$server
> "sudo hostname| cut -d "." -f1"}; chomp ($SERVER);
> 
> but commands that have metacharacters, especially the single quote, I can not
> get to work.
> 
> I can run the following from perl after ssh connection with single quotes
> included:
> 
> $ssh = Net::OpenSSH->new("$user\@$server");
> $CMD = "grep -c alias cp='cp -f' /etc/bashrc";
> $fh = $ssh->pipe_out($CMD);
> 
> while (<$fh>) {
>     $OUTPUT .= $_;
> }
> chomp ($OUTPUT);
> 
> and it works.
> 
> But if add 'sudo' for cases where I need to read a file that the login user
> does not have permissions for -- I can not get it to work.
> 
> I have searched and tried a number of recommendations and it either fails with
> a permission error or locks up or gives grep errors.
> 
> Thanks in advance,
> 
> David

Hi David,

  We have a program that does a lot of SSH'ing, and wrote a perl module
(as part of a larger project). We're using Net::SSH2 as the back-end. We
also use rsync (over ssh) to move files around.

  I can't speak to your specific issue, but perhaps our code can give
you some ideas?

https://github.com/digimer/anvil/blob/master/Anvil/Tools/Remote.pm

  There, look at the 'call()' method (line 258 in the current commit).
There is a bunch of sanity checking you can probably ignore, with the
most interesting bit starting at (current) line 414.

  Here is a condensed version (obviously untested) to hopefully make the
key steps easier to follow (our program uses translation so the user
messages are not immediately obvious in the source).

====
$ssh_fh = Net::SSH2->new(timeout => 1000);
if (not $ssh_fh->connect($target, $port))
{
	# examine '$@' for why it failed.
	die;
}

if (not $ssh_fh->auth_password($remote_user, $password))
{
	# Failed with password, try again in case passwordless ssh works
	if ($ssh_fh->auth_publickey($user, $public_key, $private_key))
	{
		# That worked.
	}
	else
	{
		# No luck
		die;
	}
}
else
{
	# Connected with password successfully
}

# We need to open a channel every time for 'exec' calls. We want to keep
# blocking off, but we need to enable it for the channel() call.
$ssh_fh->blocking(1);
my $channel = $ssh_fh->channel() or $ssh_fh->die_with_error;
if (not $channel)
{
	# Try again a few times and die if none work...
}
$ssh_fh->blocking(0);
$channel->exec("$shell_call");
my @poll = {
	handle => $channel,
	events => [qw/in err/],
};

# We'll store the STDOUT and STDERR data here.
my $stdout = "";
my $stderr = "";
			
# Now collect the data.
while(1)
{
	$ssh_fh->poll(250, \@poll);

	# Read in anything from STDOUT
	while($channel->read(my $chunk, 80))
	{
		$stdout .= $chunk;
	}
	while ($stdout =~ s/^(.*)\n//)
	{
		my $line = $1;
		push @{$stdout_output}, $line;
	}

	# Read in anything from STDERR
	while($channel->read(my $chunk, 80, 1))
	{
		$stderr .= $chunk;
	}
	while ($stderr =~ s/^(.*)\n//)
	{
		my $line = $1;
		push @{$stderr_output}, $line;
	}

	# Exit when we get the end-of-file.
	last if $channel->eof;
}
====

-- 
Digimer
Papers and Projects: https://alteeve.com/w/
"I am, somehow, less interested in the weight and convolutions of
Einstein’s brain than in the near certainty that people of equal talent
have lived and died in cotton fields and sweatshops." - Stephen Jay Gould
_______________________________________________
users mailing list -- users@xxxxxxxxxxxxxxxxxxxxxxx
To unsubscribe send an email to users-leave@xxxxxxxxxxxxxxxxxxxxxxx
Fedora Code of Conduct: https://getfedora.org/code-of-conduct.html
List Guidelines: https://fedoraproject.org/wiki/Mailing_list_guidelines
List Archives: https://lists.fedoraproject.org/archives/list/users@xxxxxxxxxxxxxxxxxxxxxxx



[Index of Archives]     [Older Fedora Users]     [Fedora Announce]     [Fedora Package Announce]     [EPEL Announce]     [EPEL Devel]     [Fedora Magazine]     [Fedora Summer Coding]     [Fedora Laptop]     [Fedora Cloud]     [Fedora Advisory Board]     [Fedora Education]     [Fedora Security]     [Fedora Scitech]     [Fedora Robotics]     [Fedora Infrastructure]     [Fedora Websites]     [Anaconda Devel]     [Fedora Devel Java]     [Fedora Desktop]     [Fedora Fonts]     [Fedora Marketing]     [Fedora Management Tools]     [Fedora Mentors]     [Fedora Package Review]     [Fedora R Devel]     [Fedora PHP Devel]     [Kickstart]     [Fedora Music]     [Fedora Packaging]     [Fedora SELinux]     [Fedora Legal]     [Fedora Kernel]     [Fedora OCaml]     [Coolkey]     [Virtualization Tools]     [ET Management Tools]     [Yum Users]     [Yosemite News]     [Gnome Users]     [KDE Users]     [Fedora Art]     [Fedora Docs]     [Fedora Sparc]     [Libvirt Users]     [Fedora ARM]

  Powered by Linux