Execute a shell script in another unix machine using SSH [duplicate] - bash

This question already has answers here:
SSH in shell script with password
(3 answers)
Closed 9 years ago.
I am trying to execute a script which is present in another unix machine from my unix box using below command:
HOST=myhostname
USER=myuser
ssh -o StrictHostKeyChecking=no -l $USER $HOST "/tmp/myscript.sh"
But the command prompts me to enter the password, but I don't want the command to prompt for password, instead of that I want to pass the password as parameter for my command. But I am not able to find an option to pass the password as parameter to SSH command.
Please help me on how to send password as part of command, instead of the command to prompt it. I am using BASH shell script.

Use EXPECT
Save this as mylogin.exp (or some other name you like), and change the names and password:
#!/usr/bin/expect -f
spawn ssh myname#some_server.com
expect {
password: {send "mypassword\n"}
}
interact
Then just run the command:
./mylogin.exp
That will just get you logged in. If you want to run a command instead, you can just put that at the end of the ssh command.

Related

Shell script EXECUTE CODE ON REMOTE SERVER AND get the result in local [duplicate]

This question already has answers here:
how to execute an local script in remote server with parameters
(2 answers)
Closed 4 years ago.
I want to check when the files have arrived on an remote unix server. I have made an script on my local server which puts filename and its date into an csv file but I need that file to be saved in my local server and not on remote server.
What should be the command like which let's me ssh to that server and execute rest of my code there and output the result in my local.
you may try doing:
ssh [username]#[servername] "command" >> /path/to/outfile.out
However, This command required password-less authentication between source and destination.
You need to setup key based authorization and then just execute commands like that:
ssh USER#HOST 'COMMAND'
Or you can use sshpass to give a password directly in command line (not recommended!):
sshpass -p 'YourPassword' ssh USER#HOST 'COMMAND'

Automatic ssh script without expect and without key (with user/password) [duplicate]

This question already has answers here:
Shell script to automate SSH login using password
(2 answers)
Closed 5 years ago.
I am trying to make a script in bash that connects via ssh to another machine.
I only have user access to this machines so
I can't use expect (It is not installed and I can't install it)
I can't install ssh keys
So I have to log in via username and password.
Is there a way to make my script send my password just using bash?
You can do that with sshpass, however, it's insecure.
sshpass -p YOUR_PW ssh ...
That will connect without asking for password.
You might need to add the flag -oStrictHostKeyChecking=no to ssh for auto-accepting keys.

ssh in shell script, then execute commands after password

I'm trying to write a simple shell script to execute commands on my server box via ssh.
I'm not trying to pass a password within the shell script, I'm just wondering how I can get it to run commands on that box after the password is entered.
So far, when I execute my script, nothing happens after I enter the password. Or, it executes when I kill the ssh process.
I'm sure it's really easy, but I've been searching for hours and nothing has come up on the net, probably because nobody else needs to ask that.
Is there some way to do this natively within the shell? Thanks!
Look if you want to ssh without prompting password then you need a key authentication rather password authentication.
Else try;
sudo apt-get install sshpass
sshpass -p your_password ssh user#hostname
And to execute command at remote;
ssh user#host 'command'

run a local script as root on remote server [duplicate]

This question already has answers here:
How to use SSH to run a local shell script on a remote machine?
(22 answers)
Closed 5 years ago.
I try to run a local script on multiple remote servers as root. I don't have su to root on those but just can run root commands using sudo. So far I tried:
for host in $(cat hosts_list); do ssh -tt $host "echo mypassword | sudo bash -s" < ./myscript.sh
And in myscript.sh there is something like:
echo "test test123" >> /etc/tests
exit 0
But it looks like not working and won't change the file. What is the proper way to run this script as root and without typing password separately for each host?
Ok, then why do you "echo mypassword" ?
Can't you add your SSH account to the sudoers file with NOPASSWD ?
From man sudoers:
authenticate If set, users must authenticate themselves via a password (or other means
of authentication) before they may run commands. This default may be
overridden via the PASSWD and NOPASSWD tags. This flag is on by default.

Automatically input a password when a bash script is run [duplicate]

This question already has answers here:
How to provide password to a command that prompts for one in bash?
(8 answers)
Closed 8 years ago.
For example, say if I have a script saying:
#!/bin/bash
sudo setpci -s 00:02.0 F4.B=00
How do I put the root password into the script so that it accepts it as the password when it reads and executes the sudo line (so I don't have to type it manually)?
Spawning an expect session within your bash script is typically how you automate interactive prompts.
e.g.
expect -c "
spawn sudo setpci -s 00:02.0 F4.B=00
expect -nocase \"password:\" {send \"$PASS\r\"; interact}
"
Note that this isn't the recommended approach in this case as it is a huge security hole to store your root password in a bash script.
The correct solution would be to edit your /etc/sudoers/ to not prompt you for a password for that binary.
#in /etc/sudoers
neohexane ALL=(ALL) NOPASSWD : /usr/bin/setpci

Resources