开发者

Logging in via a script to a remote server and execute a set of commands

How can I login to a remote server and execute a set of comm开发者_StackOverflowands then when done logout and continue my script?

Thanks.


ssh can be used to execute a command, rather than start a remote interactive login shell. For example:

ssh user@host ls

Will log into host and execute the ls command.

You can use this inside a bash script as normal:

#!/bin/bash

# do local commands

ssh user@host "ls; grep something file.txt; copy a b"

# do more local commands

From ssh's man page, the exit status will be the exit status of the remove command or 255 if an error occurred.


Small variation with easier code formatting using ssh and bash -s:

echo '
globchar="*"
ls -1d $globchar
ls -ld $globchar
' |
ssh user@host "bash -s --"


As others have stated, you can use ssh. However, if you're running a script, you may want to setup ssh to login without a password. You can do that by setting up a public/private key via the ssh-keygen command. Also change the permission of keyfiles (id_rsa, id_rsa.pub) as 600 for public key and 400 for private key for security of modification. chmod 600 id_rsa.pub ;chmod 400 id_rsa

On your system, you run ssh-keygen to generate the public and private keys. On Unix/Linux/Mac, these sit in the $HOME/.ssh directory. (Keep the passphrase blank!). Then, you want to create a file called authorized_keys on the remote machine under the $HOME/.ssh directory and copy your public key there.There is no need of generating encryption keys on remote m/c.

Now, when you do ssh or scp to the remote machine, you don't have to give the password.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜