`
sillycat
  • 浏览: 2555106 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

Shell Script Execute on Remote Machine

 
阅读更多
Shell Script Execute on Remote Machine

I remember my previous company had this similar script.

#!/bin/bash

#####################################
# how to run
# ./dump-css-devices-to-csv.sh HOST PORT SYS_USER DB_NAME USER PASSWORD
#####################################

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 "Prepare the Remote DB Server ENV for DUMP"
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 "chmod 666 /home/sillycat/css_device_dumps/css_devices_only_csv.txt"

echo "Start to dump devices to CSV"

ssh -i ~/.ssh/sky -p $PORT $SYSTEM_USER@$DB_HOST "export PGPASSWORD=$6;psql -U $DB_USER -w -d $DB_TABLE -h localhost -c \"COPY (
SELECT
DISTINCT ON (dap.serialnumber, dap.platform) dap.serialnumber as \"serialNumber\",
dap.platform,
dap.uuid as deviceUUID,
dap.enabled,
dap.pairingtoken as \"pairingToken\",
dap.currentfirmwareversion as \"firmware\",
dap.timezoneoffsetinminutes as \"timezoneOffsetinMinutes\",
dap.pairingstatus as \"pairingStatus\",
dap.lastlogindate as \"lastLoginDate\",
ua.usagetype as \"usageType\",
ua.uuid as \"userUUID\",
ua.displayName AS \"systemName\",
ua.extension AS \"extension\",
re.remoteIP as \"remoteIP\",
ug.uuid AS \"groupUUID\"
FROM
sillycat_4_1.deviceaccountpairing dap JOIN
sillycat_4_1.useraccount AS ua ON ua.id = dap.useraccountid JOIN
sillycat_4_1.usergroup AS ug ON (ua.groupid = ug.id) LEFT JOIN
sillycat_4_1.remoteendpoint AS re on (dap.serialNumber = re.serialNumber)
ORDER BY
dap.serialnumber, dap.platform, dap.lastlogindate DESC )
TO '/home/sillycat/css_device_dumps/css_devices_only_csv.txt' WITH (FORMAT CSV, HEADER, DELIMITER ',', ENCODING 'UTF8', FORCE_QUOTE *)\";"

echo "Download the CSV to Local"
# remove any old csv files already on disk
rm -f css_devices_only_csv.txt
# get users file from css database
sudo scp -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 "Script completed in $duration seconds."


Reference:
https://zaiste.net/a_few_ways_to_execute_commands_remotely_using_ssh/


分享到:
评论

相关推荐

    Execute shell script on remote host using ssh.sh

    Jenkins构建Execute shell script on remote host using ssh命令;JenkinsSSH执行命令;

    Execute shell script on remote host using ssh(最新版).txt

    在Jenkins构建springboot项目自动部署的时候,配置远程执行Dockerfile所在服务器的SSH命令,这里把完整的命令贴出来,详细搭建过程见我的博客《基于Jenkins+Gitlab+Docker实现SpringBoot项目自动部署》

    Android-ExecuteShell是一个可以方便的执行shell命令的Android开源库

    【Android-ExecuteShell开源库详解】 ExecuteShell是一个专为Android开发者设计的开源库,它使得在Android应用程序中执行shell命令变得轻而易举。在Android系统中,有时我们需要执行一些低级的操作,如文件管理、...

    Shell Script

    ### Shell Script - Key Concepts and Examples #### Introduction to Shell Scripting Shell scripting is a powerful tool in Linux for automating tasks and managing system operations. It involves writing ...

    failed to execute script 解决方案+命令行执行传参exe

    在IT行业中,我们经常遇到各种错误,"failed to execute script"是其中之一,尤其在尝试运行Python脚本时。这个问题通常发生在使用Python的`py`或`exe`可执行文件时,尤其是在试图通过命令行传递参数时。让我们深入...

    lambduh-execute:从 AWS Lambda 执行任何 shell 字符串或 bash 脚本

    handler = function ( event , context ) { var result = { } execute ( result , { shell : "echo `ls /tmp/`" , // logs output of /tmp/ dir on your lambda machine logOutput : true } ) . then ( function (

    Python 3.85 pyinstaller win7 x64 failed to execute script pyi_rth_

    Python 3.85 使用pyinstaller在win10 x64下打包的应用程序,在win10 x64下运行完全正常,但是在win7 x64下则报错。failed to execute script pyi_rth_ pyi_rth_xxx 解决方法

    Python 使用pyinstaller打包成exe文件后运行出现Failed to execute script pyi_rth_certifi

    然而,在使用`pyinstaller`将Python脚本打包成exe文件后,可能会遇到“Failed to execute script pyi_rth_certifi”这样的错误。本文将详细解释这个问题的原因,并提供解决此问题的方法。 首先,`pyinstaller`在...

    Linux Shell Scripting Essentials 无水印pdf 0分

    Packed with easy-to-follow, hands-on examples to help you write any type of shell script with confidence Who This Book Is For This book is aimed at administrators and those who have a basic knowledge ...

    domino xapgesg开发 Execute Script

    【Execute Script 在 Domino XPages 开发中的应用】 在 Domino XPages 开发中,`Execute Script` 是一个非常关键的功能,它允许开发者执行 JavaScript 或者 Expression Language (EL) 表达式,以实现服务器端的数据...

    Android代码-AndroidShell

    Execute shell commands on Android. Download the latest AAR or grab via Gradle: compile 'com.jaredrummler:android-shell:1.0.0' Usage Common utility functions include: CommandResult Shell.run...

    cmake-call-shell-script

    本主题聚焦于如何使用 CMake 调用 shell 脚本来编译第三方库,如 `libevent`。`libevent` 是一个事件通知库,常用于网络编程,提供了异步事件处理的能力。 首先,我们来看 `CMakeLists.txt` 文件。这是 CMake 的...

    execute-script-plugin

    FIT2CLOUD Execute-Script-Plugin for JenkinsJenkins是当前最常用的CI服务器,FIT2CLOUD Execute-Script-Plugin for Jenkins的功能是:代码在Jenkins服务器构建通过之后,可以在FIT2CLOUD中在指定范围的虚机上执行...

    Practical Machine Learning with Python(pdf书+sourcecode)

    Part 1 focuses on understanding machine learning concepts and tools. This includes machine learning basics with a broad overview of algorithms, techniques, concepts and applications, followed by a ...

    Linux Shell Scripting Essentials(PACKT,2015)

    Discover how to write shell scripts easily, execute script files, debug, and handle errors. Next, you’ll explore environment variables in shell programming and learn how to customize them and add a...

    源码MATLAB Machine Learning 2017

    background on machine learning including learning control that is not often associated with machine intelligence. We coin the term “autonomous learning” to embrace all of these disciplines. The ...

    Oracle中EXECUTE IMMEDIATE用法

    在Oracle数据库中,`EXECUTE IMMEDIATE`是一个非常强大的特性,允许在运行时动态执行SQL语句或PL/SQL块。这一功能自Oracle 8i版本的DBMS_SQL包引入以来,极大地增强了PL/SQL的灵活性和动态性。通过`EXECUTE ...

    c++builder的Android程序中执行Shell命令

    本单元实现了一个伪的Shell,可以用于在Android程序中执行Shell命令,如果...myShell->Execute("命令"); //需要root权限 myShell->AskForRoot(); myShell->Execute("命令"); myShell->ExitRoot(); delete myShell;

    android通过jni执行shell命令

    这里,`callShellCommand`方法会调用`executeShellCommand`,后者是通过JNI调用的本地方法。 至于`logSystem.zip`,这可能包含了一个用于记录和查看系统日志的工具或示例。在Android中,`Log`类提供了方便的方法来...

Global site tag (gtag.js) - Google Analytics