简体   繁体   English

如何使用shell脚本从窗口使用用户名/ ip地址和密码ssh进入远程linux服务器?

[英]How to ssh into remote linux server using username / ip address and password from window using shell script?

What i am trying to do is to login to remote server using SSH from my windows machine by running a shell script via git bash.我想做的是通过 git bash 运行 shell 脚本,使用 SSH 从我的 Windows 机器登录到远程服务器。

So i have a bunch of commands which i want to run over the remote linux machine and the person using this script is somone who has basic knowledge.所以我有一堆我想在远程 linux 机器上运行的命令,使用这个脚本的人是有基本知识的人。 so i just want then to open git bash and run script1.sh and i should login to remote server and run the script and connect to the server.所以我只想打开 git bash 并运行 script1.sh,我应该登录到远程服务器并运行脚本并连接到服务器。

so what i have tried is: ssh username@ip <password>所以我试过的是: ssh username@ip <password>

EDIT: You should consider installing Jenkins on the remote system. 编辑: 您应该考虑在远程系统上安装 Jenkins。

Running a command on a remote system can be done via:可以通过以下方式在远程系统上运行命令:

ssh user@host command arg1 arg2

If you omit the password, a password prompt will appear, to get around the password prompt, you should consider setting passwordless SSH login.如果省略密码,则会出现密码提示,要绕过密码提示,您应该考虑设置无密码 SSH 登录。 https://askubuntu.com/questions/46930/how-can-i-set-up-password-less-ssh-login https://askubuntu.com/questions/46930/how-can-i-set-up-password-less-ssh-login

Rephrasing what you said, you want to write a script (namely script1.sh) which does the following (in this order):换句话说,您想编写一个脚本(即 script1.sh)执行以下操作(按此顺序):

  • Start a ssh connection with a remote启动与远程的 ssh 连接
  • Execute some commands, possibly written in another script (namely script2.sh)执行一些命令,可能写在另一个脚本中(即 script2.sh)
  • Close the connection / Keep it open for additional command line commands关闭连接/保持打开状态以获取其他命令行命令

If you want to put the remote commands on script2.sh instead of listing them in script1.sh, you need to consider that script2.sh must be on the remote server.如果要将远程命令放在 script2.sh 而不是将它们列在 script1.sh 中,则需要考虑 script2.sh必须在远程服务器上。 If not, then you may consider to delegate to script1.sh the creation/copy of script2.sh on the remote machine.如果没有,那么您可以考虑将远程计算机上 script2.sh 的创建/复制委托给 script1.sh。 You may copy it in a temporary folder.您可以将其复制到一个临时文件夹中。

In this case, my suggestion is to write script1.sh as follows:在这种情况下,我的建议是这样编写script1.sh:

  • Copy of script2.sh with script2.sh 的副本

    scp /path/to/local/script2.sh user@host:/path/to/remote/script2.sh scp /path/to/local/script2.sh user@host:/path/to/remote/script2.sh

  • Switch bash sheel to remote shell with将 bash sheel 切换到远程 shell

    ssh user@host ssh 用户@主机

  • Execution of script2.sh执行script2.sh

    sh /path/to/remote/script2.sh sh /path/to/remote/script2.sh

Instead, if you prefer to list everything in just one script file, you may want to write:相反,如果您更喜欢在一个脚本文件中列出所有内容,您可能需要编写:

  • Switch bash sheel to remote shell with将 bash sheel 切换到远程 shell

    ssh user@host ssh 用户@主机

  • Execution of commands命令的执行

    echo "This command has been executed on the remote server" echo "该命令已在远程服务器上执行"

    echo "This command has also been executed on the remote server" echo "此命令也已在远程服务器上执行过"

    .. ..

  • Possibly closing the SSH connection to prevent that the user execute additional commands可能关闭 SSH 连接以防止用户执行其他命令

You may consider to copy the ssh-keys on the remote server so to avoid password prompts.您可以考虑在远程服务器上复制 ssh-keys 以避免密码提示。 More information here .更多信息在这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Linux中,如何使用ssh和Expect在远程服务器执行期间监视shell脚本的进度 - In Linux , how to monitor the progress of shell script during execution in remote server using ssh and expect 如何在Shell脚本中以非root用户身份登录(同时使用用户名和密码)? - How to login (using both username and password) as a non root user in linux in a shell script? 如何使用外壳程序脚本从远程服务器SSH读取.json文件 - How to read a .json file from a remote server SSH with a shell script 使用Shell脚本从另一个文件检查用户名和密码 - username and password checking from another file using shell script 如何使用 linux 和 windows 上的 shell 脚本询问用户名和密码 - how to ask for username and password with a shell script on linux and windows 如何从 Shell 脚本打印 IP 地址? - How to print IP address from Shell Script? 如何使用PHP在远程Linux机器上执行Shell脚本 - How to execute a shell script on a remote linux machine using php 如何使用bash脚本使用密码自动ssh进入服务器和su? - How to automatically ssh into server and su with password using bash script? linux bash shell:如何使用文件中的用户名和密码创建用户? - linux bash shell: how to create user with username and password from file? 使用配置文件的ssh命令在远程机器中执行shell脚本 - Execute shell script in remote machine using ssh command with config file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM