php-ssh icon indicating copy to clipboard operation
php-ssh copied to clipboard

using SshConfigFileConfiguration if the configuration has not defined "identityfile"

Open rkmax opened this issue 11 years ago • 3 comments

I suggets use the same behavior the SSH if i have a host configurate in ~/.ssh/config file like this

Host mytest Hostname ssh.mytest.com User testing Port 5022

the method SshConfigFileConfiguration::getAuthentication must check first if a ~/.ssh/id_rsa and ~/.ssh/id_rsa.pub exist and use at last Authentication\None

modifing the method SshConfigFileConfiguration::getConfigForHost like this

class SshConfigFileConfiguration extends Configuration
{
     const DEFAULT_FILE = '~/.ssh/id_rsa';
     // ...
     public function getConfigForHost($host)
    {
        $matches = array();
        foreach ($this->configs as $config) {
            if (fnmatch($config['host'], $host)) {
                $matches[] = $config;
            }
        }
        if (count($matches) == 0) {
            throw new RuntimeException("Unable to find configuration for host '{$host}'");
        }
        usort($matches, function ($a, $b) {
            return strlen($a['host']) > strlen($b['host']);
        });
        $result = array();
        foreach ($matches as $match) {
            $result = array_merge($result, $match);
        }
        unset($result['host']);
        if (isset($result['identityfile'])) {
            $result['identityfile'] = $this->processPath($result['identityfile']);
        } else if (file_exists($file = $this->processPath(self::DEFAULT_FILE))) {
            $result['identityfile'] = $file;
        }

        return $result;
    }
}

solves the problem quickly

rkmax avatar Oct 05 '14 14:10 rkmax

It would be awesome if you could do a PR.

Herzult avatar Oct 05 '14 17:10 Herzult

@Herzult i made it, can you merge please, i have other PR to send you ;)

rkmax avatar Oct 05 '14 19:10 rkmax

@Herzult would really appreciate this related PR to be merged, need the feature ;)

CharlyP avatar Oct 08 '14 06:10 CharlyP