I am showing how to use some third party tool to run SSH to remote connection and run some some command.
TeraTerm
SSH Connection
Create ttl file The ttl file is macro script, that TeraTerm can be able to load. In this file you have to add the command you want to execute after login ssh.
Below this terateam script just use SSH to make connection. User no need to enter account and password. It will just automatic open SSH connection then you can type command
If you want to check your ttl script work or not, just open terateam control>macro and load the file will see if script work or not.
Create a bat If you want to automatic run the script, you have to use bat file to open terateam and load the macroscript like below. After you add .bat file then it will automatic load the ttl file.
1 2 3 4 5
@echo off cd c:\ cd C:\Program Files (x86)\teraterm5 REM Your ttl file full location ttermpro.exe /m="C:\Users\chenchih_lee\Desktop\ssh_autoscript\SSH\SSH_Host.ttl"
Note: TeraTeam is a window third party tool for SSH/TELNET/Com port tool so it will only be running on Window OS.
PLINK
Plink download is a tool just like putty, which allow to use cli command to do ssh connection. If you want to do SSH automation. Let me show plink related basic command:
If you see Access granted, you have to enter to login, you can use: -no-antispoof
The -m option tells Plink to read the entire command sequence from the file.
batch means it will run as silently as possible, so it won’t even ask you to accept the fingerprint. This will never work on your first connection. Without -batch you will see a “press enter to start the session” prompt, however, that goes away with -no-antispoof.
You can either add your command into a file or as variable when run ssh will execute it.
File( basic):
Add command into cli.txt file
1 2 3
ls pwd cat /etc/arcwrt_release
Create bat shell script Put your command into file, it will read it and run it.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
@echo off timeout /t 3 /nobreak SET PLINK_PATH="C:\plink.exe" SET SSH_PASS=123456 SET GATEWAY_IP=192.168.1.106 SET COMMANDS_FILE=cli.txt SET VERSION_COMMAND="cat /etc/arcwrt_release" echo Connecting to %GATEWAY_IP% as root... :: Command with file %PLINK_PATH% -ssh -pw %SSH_PASS% test@%GATEWAY_IP% -m %COMMANDS_FILE%
REM === CONFIG === set "PLINK_PATH=C:\plink.exe" set "SSH_PASS=123456" set "GATEWAY_IP=192.168.1.106" set "USER=test" set "REMOTE_CMD=grep ARCWRT_PROJECT_NUMBER /etc/arcwrt_release | cut -d '\"' -f 2"
:: Command in file set "FW_VERSION=NONE"
REM File FOR /F "usebackq delims=" %%V IN (`%PLINK_PATH% -ssh -pw %SSH_PASS% %USER%@%GATEWAY_IP% -m command_1.txt`) DO ( set "FW_VERSION=%%V" ) echo Firmware version: !FW_VERSION!
:: Command as variable
set "FW_VERSION2=NONE" FOR /F "usebackq delims=" %%V IN (`"%PLINK_PATH% -batch -ssh -pw %SSH_PASS% %USER%@%GATEWAY_IP% !REMOTE_CMD:|=^|!"`) DO ( set "FW_VERSION2=%%V" )
REM -----Result PASS------ REM SSH to DUT and Get TR69 URL and SW Vers, command as variable
@echo off setlocal enabledelayedexpansion
SET PLINK_PATH=C:\Progra~1\PuTTY\plink.exe SET SSH_PASS=password SET IP_ADDRESS=192.168.1.254
REM Define commands SET REMOTE_CMD1=tr69_trigger getvalue Device.ManagementServer.URL SET REMOTE_CMD2=tr69_trigger getvalue Device.DeviceInfo.SoftwareVersion
REM Run only one command echo =====SSH to [!IP_ADDRESS!] get SW Ver, and Tr69 URL======== FOR /F "usebackq tokens=*" %%V IN (`"%PLINK_PATH%" -ssh -no-antispoof -pw %SSH_PASS% root@%IP_ADDRESS% -batch %REMOTE_CMD1%`) DO ( SET "CURRENT_URL=%%V" ) echo Detected URL: !CURRENT_URL!
FOR /F "usebackq tokens=*" %%V IN (`"%PLINK_PATH%" -ssh -no-antispoof -pw %SSH_PASS% root@%IP_ADDRESS% -batch %REMOTE_CMD2%`) DO ( SET "CURRENT_FW=%%V" )
echo Detected FW : !CURRENT_FW!
echo ================================== pause
Command As File
Basic setting:
1 2 3 4 5 6 7 8
@echo off setlocal enabledelayedexpansion
:: SET PLINK_PATH=C:\Progra~1\PuTTY\plink.exe SET PLINK_PATH=D:\plink.exe SET SSH_PASS=password SET IP_ADDRESS=192.168.1.254 SET GET_URL_CMD_FILE=tr69_command.txt
Case1: Append only one command from file Even though the file have two command but it only save one command to result
1 2 3 4 5 6
echo one result FOR /F "usebackq tokens=*" %%V IN (`"%PLINK_PATH%" -ssh -no-antispoof -pw %SSH_PASS% root@%IP_ADDRESS% -batch -m %GET_URL_CMD_FILE%`) DO ( SET "CURRENT_URL=%%V" ) echo Detected URL: !CURRENT_URL!
Case2: append two result from file command
1 2 3 4 5 6 7 8 9 10 11 12
echo Multiply Result
SET "COUNT=0" FOR /F "usebackq tokens=*" %%V IN (`"%PLINK_PATH%" -ssh -no-antispoof -pw %SSH_PASS% root@%IP_ADDRESS% -batch -m %GET_URL_CMD_FILE%`) DO ( SET /A COUNT+=1 IF !COUNT!==1 SET "CURRENT_URL1=%%V" IF !COUNT!==2 SET "CURRENT_SW_VER1=%%V" )