We verified whether the operation can be performed according to the title.
-Verification environment- Windows 10 2 virtualbox VMs (Centos7)
Technology used/function | purpose of use |
---|---|
Task Manager | For periodic execution of Linux server scripts from Windows |
teraTermMacro | To ssh connect to the Jump server and kick the Shell script |
expect | To kick a Shell script on the Target server from the Jump server |
Windows bat | Since it is inconvenient to set ttl directly in TaskManager, details will be explained later. |
The setting will proceed in the reverse flow of the image. ① Target server ・ Install the shell script you want to process ・ Process to acquire the memory usage rate ② Jump server ・ Install expect and install shell script using its function) ③ Windows local PC ・ TeraTermMacro (created for ShellScript Kick of Jump server) ・ Windows bat ・ TaskManager periodic execution setting
Work target: Target server Purpose: Set up a shell script for the process you want to run (as a process to record memory usage)
$ cat memory-use-rate-check.sh
#!/bin/bash
# create file memory-use.log
if [[ ! -e /var/tmp/memory-use.log ]]; then
touch /var/tmp/memory-use.log
chmod 755 /var/tmp/memory-use.log
fi
# memory use rate check
MemTotal=`free | grep Mem: | awk -F " " '{print$2}'`
MemoFree=`free | grep Mem: | awk -F " " '{print$4}'`
MemUse=`expr $MemTotal - $MemoFree`
MemRate=`expr $MemUse "*" 100 / $MemTotal`
echo `date +%Y-%m-%d/%H:%M` >> /var/tmp/memory-use.log
echo "Memory Used Rate:" $MemRate"%" >> /var/tmp/memory-use.log
Execution result: It's like recording the memory usage in a log file.
[vagrant@Target tmp]$ tail -n2 memory-use.log
2020-12-10/11:31
Memory Used Rate: 16%
Work target: Jump server Purpose: Remotely execute memory-use-rate-check.sh installed in 1 from the Jump server. 2-1. Install expect
[vagrant@Jump tmp]$ sudo yum install expect -y
2-2. Installed a shell script that uses the expect function (automates the dialogue response). Purpose: Kick memory-use-rate-check.sh of Jump → target.
[vagrant@Jump tmp]$ cat expect-for-target.sh
#!/bin/sh
expect -c "
set timeout 2
spawn env LANG=C /usr/bin/ssh [email protected]
expect \"password:\"
send \"vagrant\n\"
expect \"~]$\"
send \"cd /var/tmp\n\"
expect \"]$\"
send \"bash ./memory-use-rate-check.sh\n\"
expect \"tmp]$\"
exit 0
"
[vagrant@Jump tmp]$
Work target: Local Windows PC Purpose: Kick expect-for-target.sh on Windows PC → Jump server
Expect_test> type jump-expect-kick.Instead of the ttl ← cat command, use the type command to display the contents.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
username = 'vagrant'
hostname = '192.168.18.20'
passwdfile = 'passwd.dat'
portnum = '22'
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
call login
call exec
call exit
end
:login
getpassword passwdfile username userpasswd
msg = hostname
strconcat msg ':portnum /ssh /auth=password /user='
strconcat msg username
strconcat msg ' /passwd='
strconcat msg userpasswd
strconcat msg inputstr
connect msg
return
:exec
wait '$ '
msg = 'cd '
strconcat msg '/var/tmp'
sendln msg
wait ']$ '
msg = 'bash ./expect-for-target.sh'
sendln msg
pause 8
return
:exit
wait '$ '
msg = 'exit'
sendln msg
Work target: Windows PC
This completes.
-PS- I decided to join the Advent calendar in a hurry This is what I did recently in my drawer.
Recommended Posts