0
votes

I am wondering if there is any way to create a bash script that accepts 1 parameter, which is file, and then it will do the following

  1. automatically log on remote host using sftp (password,username are hardwired somewhere)
  2. send a local file over
  3. exit and logon to the remote host using ssh
  4. execute some scripts
  5. get back the result from remote std output.
1
Yes this can be done. I can't give you a concrete example right now but I do remember that when I did this I needed to do a manual login to the remote host first to make sure the ssh key was added to the trust store. That info may come in handy... - Floris
'ssh user@host command' will run command on remote machine. The command can be sh somescript > output.txt. You can then scp the output.txt to your client machine. - Usman Saleem
@UsmanSaleem if you use ssh,it is needed to input the password of the remote machine interactively.I know there is a way to set host keys to make it through but that is a little complicated and may not be available in some conditions. - oyss
please edit your question to include these constraints (some ssh keys, and some not). You may be able to pass a password into ssh from you local machine using expect, but that will require installing expect and taking the time to learn it syntax. Good luck. - shellter
And setting ssh key is not difficult. Use ssh-keygen on your client machine. Then append the contents of ~/.ssh/id_rsa.pub on your host's ~/.ssh/authorized_keys. The permissions of authorized_keys file must be 600. - Usman Saleem

1 Answers

0
votes

using Perl and Net::OpenSSH:

use Net::OpenSSH;

my $ssh = Net::OpenSSH->new($host, user => $user, password => $password);
my $sftp = $ssh->sftp;
$sftp->put("/local/path/to/script", "bin/my_script"); # => ~/bin/my_script
$sftp->chmod("bin/my_script", 0755);
$ssh->system({stdout_file => '/tmp/script.out'}, "bin/my_script");