使用 bash 脚本在 ssh 登录后在 ec2 上执行命令

execute command on ec2 after ssh login using bash script

此代码在我的系统上打开三个 windows 并登录到三个不同的 ec2 实例。

(xterm -geometry 70x70-0-0 -e ssh -i key1 ec2-user@52.x.x.x;) & 
(xterm -geometry 70x70+485-200 -e ssh -i key1 ec2-user@52.x.x.x;) &
(xterm -geometry 70x70+0-0 -e ssh -i key1 ec2-user@52.x.x.x;) 

但是,我想登录这三个实例并在其中执行各自的命令。类似于:

(xterm -geometry 70x70-0-0 -e ssh -i key1 ec2-user@52.x.x.x; **run python file on this instance**) & 
(xterm -geometry 70x70+485-200 -e ssh -i key1 ec2-user@52.x.x.x; **run node file on this instance**) &
(xterm -geometry 70x70+0-0 -e ssh -i key1 ec2-user@52.x.x.x; **run R file on this instance**)

主要更改是去掉“** 运行”之前的分号(以及使用真正的命令)。像

(xterm -geometry 70x70-0-0 -e ssh -i key1 ec2-user@52.x.x.x **运行 python 此实例上的文件** ) &
(xterm -geometry 70x70+485-200 -e ssh -i key1 ec2-user@52.x.x.x **运行 此实例上的节点文件** ) &
(xterm -geometry 70x70+0-0 -e ssh -i key1 ec2-user@52.x.x.x ***run R file on this instance*** ) &

在选项(例如-i)和登录信息之后,ssh接受命令和参数。

这样做:

(xterm -geometry 70x70-0-0 -e ssh -i key1 ec2-user@52.x.x.x -t "script.py ; bash";) &

-t 选项创建一个 tty 并在 script.py 之后使用 bash 你会得到一个交互式的 bash 来继续命令。