2016年9月2日 星期五

最笨的方法, 讓遠端機器執行本地寫的 script (比較不安全) -- -- sshpass

How to run a local script on the remote host -- sshpass

Googledoc

Security Issue

  • Your password shows in the script.
  • sshpass -f file_with_password [ref]

This is an ugly method

For pure script and pure local script solution, please refer this link (view).

Script file version

command

username=jing
ip=192.168.33.10
# Step 1: copy your local file to the remote site
# Note:  Option StrictHostKeyChecking=no is mandatory otherwise the file will not be copy.
sshpass -p 1234 scp  -o StrictHostKeyChecking=no "Your_Local_Script.sh" $username@$ip:~

# Step 2: run the script on the remote site
sshpass -p 1234 ssh -o StrictHostKeyChecking=no -t $username@$ip ". ./Your_Local_Script.sh"

Ex:
#!/bin/bash

ip=192.168.33.10
ssh-keygen -f ~/.ssh/known_hosts -R $ip
sudo apt-get -y --force-yes install  sshpass


username=jing
ip=192.168.33.10
sshpass -p 1234 scp common_fun.sh $username@$ip:~
sshpass -p 1234 ssh -o StrictHostKeyChecking=no -t $username@$ip ". ./common_fun.sh; fun_setup_locale"


Command version

sshpass -p 1234 ssh -o StrictHostKeyChecking=no -t stack@$ip "echo nameserver 8.8.8.8 | sudo tee /etc/resolv.conf"