Shell Script Execute on Remote Machine
ShellScriptExecuteonRemoteMachine
Iremembermypreviouscompanyhadthissimilarscript.
#!/bin/bash
#####################################
#howtorun
#./dump-css-devices-to-csv.shHOSTPORTSYS_USERDB_NAMEUSERPASSWORD
#####################################
start=$SECONDS
DEFAULT_DB_HOST="localhost"
DEFAULT_PORT=22
DEFAULT_SYSTEM_USER="root"
DEFAULT_DB_USER=“admin"
DEFAULT_DB_TABLE=“test"
DB_HOST=${1:-$DEFAULT_DB_HOST}
PORT=${2:-$DEFAULT_PORT}
SYSTEM_USER=${3:-$DEFAULT_SYSTEM_USER}
DB_TABLE=${4:-$DEFAULT_DB_TABLE}
DB_USER=${5:-$DEFAULT_DB_USER}
echo"PreparetheRemoteDBServerENVforDUMP"
ssh-i~/.ssh/sky-p$PORT$SYSTEM_USER@$DB_HOST"rm-fr/home/sillycat/css_device_dumps"
ssh-i~/.ssh/sky-p$PORT$SYSTEM_USER@$DB_HOST"mkdir/home/sillycat/css_device_dumps"
ssh-i~/.ssh/sky-p$PORT$SYSTEM_USER@$DB_HOST"touch/home/sillycat/css_device_dumps/css_devices_only_csv.txt"
ssh-i~/.ssh/sky-p$PORT$SYSTEM_USER@$DB_HOST"chmod666/home/sillycat/css_device_dumps/css_devices_only_csv.txt"
echo"StarttodumpdevicestoCSV"
ssh-i~/.ssh/sky-p$PORT$SYSTEM_USER@$DB_HOST"exportPGPASSWORD=$6;psql-U$DB_USER-w-d$DB_TABLE-hlocalhost-c\"COPY(
SELECT
DISTINCTON(dap.serialnumber,dap.platform)dap.serialnumberas\"serialNumber\",
dap.platform,
dap.uuidasdeviceUUID,
dap.enabled,
dap.pairingtokenas\"pairingToken\",
dap.currentfirmwareversionas\"firmware\",
dap.timezoneoffsetinminutesas\"timezoneOffsetinMinutes\",
dap.pairingstatusas\"pairingStatus\",
dap.lastlogindateas\"lastLoginDate\",
ua.usagetypeas\"usageType\",
ua.uuidas\"userUUID\",
ua.displayNameAS\"systemName\",
ua.extensionAS\"extension\",
re.remoteIPas\"remoteIP\",
ug.uuidAS\"groupUUID\"
FROM
sillycat_4_1.deviceaccountpairingdapJOIN
sillycat_4_1.useraccountASuaONua.id=dap.useraccountidJOIN
sillycat_4_1.usergroupASugON(ua.groupid=ug.id)LEFTJOIN
sillycat_4_1.remoteendpointASreon(dap.serialNumber=re.serialNumber)
ORDERBY
dap.serialnumber,dap.platform,dap.lastlogindateDESC)
TO'/home/sillycat/css_device_dumps/css_devices_only_csv.txt'WITH(FORMATCSV,HEADER,DELIMITER',',ENCODING'UTF8',FORCE_QUOTE*)\";"
echo"DownloadtheCSVtoLocal"
#removeanyoldcsvfilesalreadyondisk
rm-fcss_devices_only_csv.txt
#getusersfilefromcssdatabase
sudoscp-i~/.ssh/sky-P$PORT$SYSTEM_USER@$DB_HOST:/home/sillycat/css_device_dumps/css_devices_only_csv.txt./css_devices_only_csv.txt
end=$SECONDS
duration=$((end-start))
echo"Scriptcompletedin$durationseconds."
Reference:
https://zaiste.net/a_few_ways_to_execute_commands_remotely_using_ssh/