How to export/import PuTTy sessions list?

后端 未结 15 1190
时光取名叫无心
时光取名叫无心 2020-12-22 14:41

Is there a way to do this?

Or I have to take manually every record from Registry?

相关标签:
15条回答
  • 2020-12-22 15:09

    Example:
    How to transfer putty configuration and session configuration from one user account to another e.g. when created a new account and want to use the putty sessions/configurations from the old account

    Process:
    - Export registry key from old account into a file
    - Import registry key from file into new account

    Export reg key: (from OLD account)

    1. Login into the OLD account e.g. tomold
    2. Open normal 'command prompt' (NOT admin !)
    3. Type 'regedit'
    4. Navigate to registry section where the configuration is being stored e.g. [HKEY_CURRENT_USER\SOFTWARE\SimonTatham] and click on it
    5. Select 'Export' from the file menu or right mouse click (radio ctrl 'selected branch')
    6. Save into file and name it e.g. 'puttyconfig.reg'
    7. Logout again

    Import reg key: (into NEW account)

    1. Login into NEW account e.g. tom

    2. Open normal 'command prompt' (NOT admin !)

    3. Type 'regedit'

    4. Select 'Import' from the menu

    5. Select the registry file to import e.g. 'puttyconfig.reg'

    6. Done

    Note:
    Do not use an 'admin command prompt' as settings are located under '[HKEY_CURRENT_USER...] 'and regedit would run as admin and show that section for the admin-user rather then for the user to transfer from and/or to.

    0 讨论(0)
  • 2020-12-22 15:09

    There is a PowerShell script at ratil.life/first-useful-powershell-script-putty-to-ssh-config which can convert the sessions to a format that can be used in .ssh/config. It can also be found on GitHub.

    This excerpt contains the main guts of the code, and will print the resulting config directly to stdout:

    # Registry path to PuTTY configured profiles
    $regPath = 'HKCU:\SOFTWARE\SimonTatham\PuTTY\Sessions'
    
    # Iterate over each PuTTY profile
    Get-ChildItem $regPath -Name | ForEach-Object {
    
        # Check if SSH config
        if (((Get-ItemProperty -Path "$regPath\$_").Protocol) -eq 'ssh') {
            # Write the Host for easy SSH use
            $host_nospace = $_.replace('%20', $SpaceChar)
            $hostLine =  "Host $host_nospace"
    
            # Parse Hostname for special use cases (Bastion) to create SSH hostname
            $puttyHostname = (Get-ItemProperty -Path "$regPath\$_").HostName
            if ($puttyHostname -like '*@*') {
                $sshHostname = $puttyHostname.split("@")[-1]
                }
            else { $sshHostname = $puttyHostname }
            $hostnameLine = "`tHostName $sshHostname"   
    
            # Parse Hostname for special cases (Bastion) to create User
            if ($puttyHostname -like '*@*') {
                $sshUser = $puttyHostname.split("@")[0..($puttyHostname.split('@').length - 2)] -join '@'
                }
            else { $sshHostname = $puttyHostname }
            $userLine = "`tUser $sshUser"   
    
            # Parse for Identity File
            $puttyKeyfile = (Get-ItemProperty -Path "$regPath\$_").PublicKeyFile
            if ($puttyKeyfile) { 
                $sshKeyfile = $puttyKeyfile.replace('\', '/')
                if ($prefix) { $sshKeyfile = $sshKeyfile.replace('C:', $prefix) }
                $identityLine = "`tIdentityFile $sshKeyfile"
                }
    
            # Parse Configured Tunnels
            $puttyTunnels = (Get-ItemProperty -Path "$regPath\$_").PortForwardings
            if ($puttyTunnels) {
                $puttyTunnels.split() | ForEach-Object {
    
                    # First character denotes tunnel type
                    $tunnelType = $_.Substring(0,1)
                    # Digits follow tunnel type is local port
                    $tunnelPort = $_ -match '\d*\d(?==)' | Foreach {$Matches[0]}
                    # Text after '=' is the tunnel destination
                    $tunnelDest = $_.split('=')[1]
    
                    if ($tunnelType -eq 'D') {
                        $tunnelLine = "`tDynamicForward $tunnelPort $tunnelDest"
                    }
    
                    ElseIf ($tunnelType -eq 'R') {
                        $tunnelLine = "`tRemoteForward $tunnelPort $tunnelDest"
                    }
    
                    ElseIf ($tunnelType -eq 'L') {
                        $tunnelLine = "`tLocalForward $tunnelPort $tunnelDest"
                    }
    
                }
    
            # Parse if Forward Agent is required
            $puttyAgent = (Get-ItemProperty -Path "$regPath\$_").AgentFwd
            if ($puttyAgent -eq 1) { $agentLine = "`tForwardAgent yes" }
    
            # Parse if non-default port
            $puttyPort = (Get-ItemProperty -Path "$regPath\$_").PortNumber
            if (-Not $puttyPort -eq 22) { $PortLine = "`tPort $puttyPort" }
    
            }
    
            # Build output string
            $output = "$hostLine`n$hostnameLine`n$userLine`n$identityLine`n$tunnelLine`n$agentLine`n"
    
            # Output to file if set, otherwise STDOUT
            if ($outfile) { $output | Out-File $outfile -Append}
            else { Write-Host $output }
        }
    
    }
    
    0 讨论(0)
  • 2020-12-22 15:11

    The answer posted by @m0nhawk doesn't seem to work as I test on a Windows 7 machine. Instead, using the following scripts would export/import the settings of putty:

    ::export
    @echo off
    set regfile=putty.reg
    pushd %~dp0
    
    reg export HKCU\Software\SimonTatham %regfile% /y
    
    popd
    

    --

    ::import
    @echo off
    pushd %~dp0
    set regfile=putty.reg
    
    if exist %regfile% reg import %regfile%
    
    popd
    
    0 讨论(0)
提交回复
热议问题