SSH Remote Tool

SSH Automatic Command

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

1
2
3
4
5
timeout=10
pause 5

connect '192.168.1.254 /ssh /auth=password /user=root /passwd=1234567890 /nosecuritywarning'

If you want to run SSH command you can refer below example this will run command and close terateam.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
timeout=10
pause 5
connect '192.168.1.254 /ssh /auth=password /user=root /passwd=1234567890 /nosecuritywarning'

pause 2
sendln 'cat /etc/arcwrt_release'

;sendln 'tr69_trigger setvalue Device.ManagementServer.EnableCWMP=1'
pause 4

;sendln 'tr69_trigger setvalue Device.ManagementServer.URL="http://172.21.201.112:8080/acs"'
;pause 5

closett

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:

Make a basic ssh remote connection

Syntax:

plink -ssh -pw <password> admin@192.168.0.1 "command"
ex: plink -ssh user1@10.10.1.11 -pw password@123 (hostname; date)

Note:

  • 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.

Use one line command

  • one command with grep
1
plink -ssh -pw 123456 test@192.168.1.106 "cat /etc/arcwrt_release | grep 'ARCWRT_PROJECT_NUMBER'"

or

1
plink -ssh -pw 123456 test@192.168.1.106 "grep 'ARCWRT_PROJECT_NUMBER' /etc/arcwrt_release"
  • Multiply command
1
plink -ssh -pw 123456 test@192.168.1.106 "ls;echo Hello World;ls"
  • Advance Filter command
1
plink -ssh -pw <password> root@<IP> "grep 'ARCWRT_PROJECT_NUMBER' /etc/arcwrt_release | cut -d '\"' -f 2; tr69_trigger getvalue Device.ManagementServer.URL"

Run Command by variable or file

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%

:: command with variable
%PLINK_PATH% -ssh -pw %SSH_PASS% test@%GATEWAY_IP% %VERSION_COMMAND%
echo.
echo Script finished..

File(filter command)

  • Add command into command_1.txt file
1
2
cat /etc/arcwrt_release | grep 'ARCWRT_PROJECT_NUMBER' | cut -d '"' -f 2
#grep ARCWRT_PROJECT_NUMBER /etc/arcwrt_release | cut -d '"' -f 2
  • Create bat shell script
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
@echo off
setlocal EnableDelayedExpansion

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"
)

echo.
echo Firmware version: !FW_VERSION2!

pause

Execute Command by file or variable

Command as variable

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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

tr69_command.txt

1
2
tr69_trigger getvalue Device.ManagementServer.URL
tr69_trigger getvalue Device.DeviceInfo.SoftwareVersion
  • 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"
)

echo Detected URL: !CURRENT_URL1!
echo Detected Ver: !CURRENT_SW_VER1!

  • Case3 append all result from file command
1
2
3
4
5
FOR /F "usebackq tokens=*" %%V IN (`"%PLINK_PATH%" -ssh -no-antispoof -pw %SSH_PASS% root@%IP_ADDRESS% -batch -m %GET_URL_CMD_FILE%`) DO (
IF "!TOTAL_RESULT!"=="" (SET "TOTAL_RESULT=%%V") ELSE (SET "TOTAL_RESULT=!TOTAL_RESULT!; %%V")
)

echo All Results: !TOTAL_RESULT!

HotCode Command

1
2
3
4
5
6
FOR /F "tokens=*" %%A IN ('
%PLINK_PATH% -ssh -no-antispoof -pw %SSH_PASS% root@%IP_ADDRESS% -batch ^
"tr69_trigger getvalue Device.ManagementServer.URL && tr69_trigger getvalue Device.DeviceInfo.SoftwareVersion"
') DO (
echo Output: %%A
)