Erstellen Sie eine Batchdatei oder Verknüpfung zu PuTTY (ssh), die eine Sitzung öffnet und einen Befehl ausführt

Ich öffne oft eine SSH-Sitzung, um denselben einzelnen Befehl auszuführen. Ich habe alles eingerichtet, um mich anzumelden, ohne ein Passwort einzugeben (über SSH Key-Based Auth), also habe ich mich gefragt, ob es eine Möglichkeit gibt, eine Verknüpfung oder eine Batch-Datei in Windows zu erstellen, die PuTTY oder ein ähnliches Programm lädt, dann feuere diesen Befehl ab (und wahrscheinlich beenden, wenn das Ergebnis gut ist).

Author: FreeSoftwareServers, 2017-12-19

7 answers

Verwenden Sie die Kommandozeilen-PuTTY-Version plink.exe, um eine SSH-Verbindung zu einem Host Ihrer Wahl zu initiieren. Verwenden Sie den Schalter -ssh, um eine Verbindung mit SSH herzustellen. Mit dem Schalter -m können Sie eine Befehlsdatei einfügen:

plink.exe -ssh host1 -m C:\path\to\commands.txt

Sie können plink.exe von hier herunterladen.

Der letzte Schritt wäre, eine Verknüpfung mit plink.exe mit den gewünschten Parametern zu erstellen.

Weitere Parameter finden Sie in der Plink-Dokumentation: Plink.exe-Dokumentation

 14
Author: Smeerpijp,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/techietown.info/template/agent.layouts/content.php on line 61
2017-12-19 15:38:08

Um eine Befehlsausführung zu automatisieren, verwenden Sie Plink (from PuTTY package), nicht PuTTY selbst.

Plink akzeptiert einen Befehl in seiner Befehlszeile:

plink.exe user@host command

Wenn Sie weiterhin Kitt verwenden möchten, können Sie verwenden -m wechseln Sie, um eine Befehlsdatei anzugeben (Plink unterstützt auch den Schalter -m).

 8
Author: Martin Prikryl,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/techietown.info/template/agent.layouts/content.php on line 61
2017-12-20 06:26:13

Sie können Putty-Konfigurationen verwenden, um dies zu erreichen.

Laden Sie putty und konfigurieren Sie Ihre Sitzung.

Geben Sie den Remote-Befehl ein, den Sie hier ausführen möchten:

Putty Befehl der Fernbedienung box

Bevor Sie dann auf "Open" klicken, kehren Sie zur Registerkarte "Session " zurück (oben) und speichern Sie Ihre Konfiguration.

Erstellen Sie nun eine Verknüpfung zu putty.exe und fügen Sie das Flag -load hinzu, zum Beispiel:

%PATH_TO_PUTTY%\putty.exe -load my_config

Jetzt können Sie einfach auf die Verknüpfung klicken und es wird Ihre session, führe deinen Befehl aus.

 6
Author: Attie,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/techietown.info/template/agent.layouts/content.php on line 61
2017-12-19 14:10:00

Wenn Sie Windows 10 verwenden, könnten Sie daran interessiert sein zu wissen, dass Microsoft Berichten zufolge eine Beta von OpenSSH (Client und Server):

[...] gehe zu" Optionale Funktionen verwalten "und dann + "Funktion hinzufügen". Sie können dann in der Liste nach unten scrollen und die Funktionen OpenSSH Client (Beta) und OpenSSH Server (Beta) in Windows finden. Die Idee, einen anderen Remote-Dienst unter Windows auszuführen, kann entmutigend sein, daher geben wir Ihnen keine Schuld, wenn Sie den Server nicht installieren möchten.

Nach der Installation können Sie einfach Ihre Befehlszeile starten und den OpenSSH-Client verwenden, indem Sie ssh gefolgt von dem Befehl ssh ubuntu@someIP eingeben.

Hier ist der aktuelle (Stand November 28, 2017) ssh command usage Guide, den wir mit dem Windows OpenSSH Client erhalten:

C:\WINDOWS\system32>ssh
usage: ssh [-46AaCfGgKkMNnqsTtVvXxYy] [-b bind_address] [-c cipher_spec]
           [-D [bind_address:]port] [-E log_file] [-e escape_char]
           [-F configfile] [-I pkcs11] [-i identity_file]
           [-J [user@]host[:port]] [-L address] [-l login_name] [-m mac_spec]
           [-O ctl_cmd] [-o option] [-p port] [-Q query_option] [-R address]
           [-S ctl_path] [-W host:port] [-w local_tun[:remote_tun]]
           [user@]hostname [command]
C:\WINDOWS\system32>

Wenn dies funktioniert, sollten Sie ssh wie bei einem *Nix verwenden können:

C:\WINDOWS\system32>ssh user@host "ls -l ~"
 2
Author: Michael,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/techietown.info/template/agent.layouts/content.php on line 61
2017-12-19 20:50:50

Dachte Nur, ich würde stecken Sie das Skript, das ich hoffentlich zu speichern einige Leute Stunden von syntax und man-page research -

A) SingleIP, IP Range, or IPList file

B) script name to run on the IP/range (actual unix sh/ksh type script)

C) optionally do nmap scan for port 22 open (i.e. subnet with some non-Linux machines)

-want to avoid IPs putty cannot connect to, avoiding script being able to continue properly

D) optionally run pscp command to autocache the host key (putty does not do this automatically)

Dieser Stapel setzt voraus:

1. you have the full PuTTY package and SSH key installed in C:\Program Files\PuTTY\

2. batch/scripts/IPlist files installed in C:\Program Files\PuTTY\scripts\

3. The NMAP and PuTTY working directories are in your command path

Https://www.harmonyhit.com/PuttyBatch.bat

Dies ist hauptsächlich nützlich, um ein einzelnes Skript (z. B. das Aktualisieren eines SSL-Zertifikats) auf einer Reihe von Computern zu haben

Hier ist die vollständige batch-Datei:

@ECHO OFF

rem  You must modify the CACHEKEY and SCRIPT section to reflect your SSH key location

set IPFILE=IPList_temp.txt
set runagain=n
set OPTION=1
set RANGE=
set IP=
set SCAN=n
set KEYCACHE=n

:OPTION
CLS
ECHO (Working directory is set to C:\Program Files\PuTTY\scripts\)
ECHO. 
ECHO 1. Single IP
ECHO 2. IP list file
ECHO 3. IP Range
ECHO.
set /p OPTION="Choose IP type:"
IF %OPTION%==3 GOTO IPRANGE
IF %OPTION%==2 GOTO IPLIST
IF %OPTION%==1 GOTO SINGLEIP
echo Please select a valid option
GOTO OPTION

:IPRANGE
ECHO Enter the IP range(s) as in the following example. Use a space between multiple ranges:
ECHO       i.e. "10.21.0.15-99 10.21.1.15-100"
set /p RANGE=Enter Range: 
echo %RANGE% > %IPFILE%
GOTO SCRIPTNAME

:SINGLEIP
set /p IP=Enter IP:
echo %IP% > %IPFILE%
GOTO SCRIPTNAME

:IPLIST
set /p IP=Enter IPList filename:
copy /Y %IP% %IPFILE%
GOTO SCRIPTNAME

:SCRIPTNAME
set /p SCRIPT=Enter script name:
if not exist %SCRIPT% (
echo filename does not exist!
GOTO SCRIPTNAME )

ECHO.
set /p SCAN=Do Nmap scan first? (recommended):
if %SCAN%==n GOTO :RUNCACHE

rem Check whether IP type is "range" as nmap cannot read an IP range from file and must be type directly in nmap command
IF %OPTION%==3 GOTO NMAPRANGE 
:NMAP
echo Scanning IPs for port 22 open...
nmap --open -n -p22 -iL %IPFILE% -oG - | findstr /E Up > nmap_temp.txt
GOTO AFTERNMAP

:NMAPRANGE
echo Scanning IPs for port 22 open...
nmap --open -n -p22 %RANGE% -oG - | findstr /E Up > nmap_temp.txt

:AFTERNMAP
echo DONE
rem     nmap formatting is not correct, the following removes extra info
for /f "tokens=2" %%A in (nmap_temp.txt) do echo %%A >> nmap_temp2.txt
rem remove hidden space at end of IP (requires "repl" be in the batch file directory)
type "nmap_temp2.txt" | repl " " "" > %IPFILE%

:RUNCACHE
set /p KEYCACHE=Scan and cache SSH key (y/n) ?
if %KEYCACHE%==n GOTO SCRIPT

:CACHEKEY
rem Run through all IPs to cache the SSH Host Key if not already cached
for /F "tokens=*" %%A in (%IPFILE%) do ( echo y | "C:\Program Files\PuTTY\pscp.exe" -l root -i "C:\Program Files\PuTTY\SSH.ppk" -touch %%A:/tmp/test )

:SCRIPT
for /F "tokens=1" %%A in (%IPFILE%) do ( "C:\Program Files\PuTTY\putty.exe" -ssh %%A -t -l root -i "C:\Program Files\PuTTY\SSH.ppk" -m "C:\Program Files\PuTTY\scripts\%SCRIPT%" )
if exist nmap_temp.txt (del nmap_temp.txt)
if exist nmap_temp2.txt (del nmap_temp2.txt)
set /p runagain="Press Enter to Finish or y to rerun"
if %runagain%==y GOTO OPTION
 2
Author: Todd Porter,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/techietown.info/template/agent.layouts/content.php on line 61
2019-06-07 16:19:01

Sie können sich auch Software wie mRemoteNG, MobaXterm oder SecureCRT ansehen, die Ihre SSH-Verbindungen für Sie verwaltet, dies verknüpft auch mit den gespeicherten PuTTY-Sitzungen, sodass Sie eine Vorlage auf die Sitzung anwenden können.

 1
Author: Gubbins,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/techietown.info/template/agent.layouts/content.php on line 61
2017-12-19 15:18:27

Dies ist das letzte "CMD", das ich einfach auf meinem Dateiserver speichern und eine Verknüpfung auf meinem Desktop erstellen kann.

::FreeSoftwareServers
::Automated Opening of SSH Tunnel & Execute CMD on Remote Host
::https://superuser.com/questions/1278434/create-a-batch-file-or-shortcut-to-putty-ssh-that-opens-a-session-and-runs-a-c

set puttydir="C:\Program Files\PuTTY"
set exe=plink.exe
::Profile must exist in PuTTY
set remotehost=FileServer
set remotecmd="chmod 777 /mnt/mdadm/torrents -R"

cd %puttydir% 

%exe% %remotehost% %remotecmd%

::Test First Manually in CMD Prompt
::Note Remote Host does NOT have access to BashRC Alias's
::start "C:\Program Files\PuTTY\" plink.exe -ssh FileServer touch /tmp/testfile
::start "C:\Program Files\PuTTY\" plink.exe -ssh FileServer ~/script.sh
 0
Author: FreeSoftwareServers,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/techietown.info/template/agent.layouts/content.php on line 61
2017-12-29 07:01:37