Giving Password by command line on asking for it by bash script [closed] - linux

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 9 years ago.
Improve this question
I am writing a bash script for windows powershell.
I am remotely connecting to server by ssh but the problem is connection needs a password and i have to give it by script.
I tried pipelining too by echo "password" | ssh user#remote.host.
But is it still asking for password user#remote.host's password:. Is there any way to enter password by command line or bash script only?

Use ssh-keygen to generate a new SSH key pair, and ssh-copy-id user#machine to copy it to the remote host.
This will let you log in without a password, and it won't introduce any security issues by passing the password as a string.

Use sshpass to pass password to ssh
$ sshpass -p 'password' ssh username#server.example.com

Related

Bash script check permissions to run command on remote [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 4 years ago.
Improve this question
I have a local development machine and from my bash script am sending commands to the remote server.
How can I write bash code to check if I am allowed to run the remote command so that I can handle the success/failure response from my script?
Alternatively, how can I capture the output so that I can parse it and detect if it succeeded. The difficulty with parsing is that the ssh command might trigger a password prompt so I can't interfere with that.
That bash script uses ssh -qt to send the remote commands
Command
ssh user#host -qt "sudo -u www /usr/local/bin/php /mnt/data/script.php"
Output:
[sudo] password for xxx:
Sorry, user xxx is not allowed to execute '/usr/local/bin/php /mnt/data/script.php' as www on host.domain.com
Assuming that user != root above: you can't - there's no way to read /etc/sudoers or /etc/sudoers.d/* in a normally set-up Linux box if you're not root, so apart from trial & error there's nothing to be done.
As for capturing the result - that's fairly simple (parsing it, of course, is a different story, depending on what you're doing over there).
output=$( ssh user#host -qt "sudo -u www /usr/local/bin/php /mnt/data/script.php" 2>&1 )
After the execution (and you typing the password for sudo)
echo $? # gives you the return-code of what happened on the far end, if it's a success that should be 0
echo $output # gives you the strings to parse

How to auto connect jumper or proxy and server with one command ssh [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 4 years ago.
Improve this question
I remember one of my friend is using ssh file configuration to make it be done, but I can't find the command that should be written in that file to achieve this result.
So what he did is just type the command
ssh [alias of the server]
and the shell automatically go to jumper (proxy) submit the password in there,
then go to the server and again submit the password there, so he doesn't need to re-enter password during ssh into the server.
Another question, is this able to be done for Windows server?
You can use something like this:
Host jump
User [username]
HostName [ip address]
Host [server ip address] [server alias]
HostName [server ip address]
User [username]
Port [port]
ProxyCommand ssh -q -W %h:%p jump 2>/dev/null
Host Jump is a proxy server.
The command that will help you take a leap on the proxy server then continue to the server is ProxyCommand.
%h: is using your username
%p: is using your password from id_rsa.pub.
Reference: https://www.ssh.com/ssh/config/
I've never tried this except with public key authentication, but assuming:
You want to ssh from origin-box to target-box
target-box has a network name of target.box.domain
origin-box is not authorized on target-box
proxy-box is authorized on target-box
origin-box is authorized on proxy-box
You ssh/config on origin-box would be something like:
Host target-box
ProxyCommand ssh -q proxy-box -W target.box.domain

Issue with changing login shell [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 6 years ago.
Improve this question
I want to change the default login shell on my Ubuntu.
To change the shell I ran the following:
$ chsh -s /usr/bin/zsh
After that I restart my terminal but my default shell is still bash.
$ echo $SHELL
/bin/bash
These are shells installed on my machine:
$ cat /etc/shells
# /etc/shells: valid login shells
/bin/sh
/bin/dash
/bin/bash
/bin/rbash
/bin/zsh
/usr/bin/zsh
Record for my user in /etc/passwd is also changed like expected:
$ cat /etc/passwd|grep myuser
myuser:x:1000:1000:myuser,,,:/home/myuser:/usr/bin/zsh
I successfully changed my login shell the same way on my mac but I seem to be having some issue on Ubuntu. Am I missing something here?
You need to logout and log back into Ubuntu so that your GUI based X terminal shells pick up the new shell value.
Note: Ctrl+Alt+F[1-6] represent 6 virtual shells while GUI login is on Ctrl+Alt+F7

how to make ssh fail if any parameter is wrong [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 9 years ago.
Improve this question
I'm using the command ssh -i /home/ssh_keys/10_1_1_127 root#10.1.1.127 date for checking the date on some other machine,
If some parameter is wrong, like the user, the ip or the identity file doesn't exists,
ssh asks for password
for example, if I write ssh -i /home/ssh_keys/10_1_1_1277 root#10.1.1.127 date
whilst /home/ssh_keys/10_1_1_1277 doesn't exists, I get:
root#10.1.1.127's password:
I wanted to know if it is possible, and if so, then how to make ssh fail if some parameter isn't right, so ssh won't ask me for a password if I enter wrong parameters...
Thanks
All these changes should be done via root or a sudo enabled user.
In /etc/ssh/sshd_config set the following entries to no:
ChallengeResponseAuthentication no
PasswordAuthentication no
UsePAM no
If you need help finding those specific lines, use grep:
grep -n "PasswordAuthentication" /etc/ssh/sshd_config
^^or whatever^^
This outputs the line number
Then restart ssh
/etc/init.d/ssh restart
or
service ssh restart
depending on your flavor of Linux.

Executing remote command and saving input to file [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
I need to access to multiple hosts through SSH, execute a specific command (show ms info) and capture the output to a file. I need to copy that file back to my linux machine
I want to use ssh and expect to supply the password
My problem is saving the output to a text file and looping around 100 machines simultaneously.
It is more straightforward than you think:
host1 $ ssh user#host2 ls > remote-output.txt
Enter passphrase for key '/home/user/.ssh/id_rsa':
host1 $ ls
remote-output.txt
host1 $
To do it for multiple hosts, I suggest using ssh-agent and setting up autorization keys:
$ ssh-agent bash
$ ssh-add
Enter passphrase for /home/user/.ssh/id_rsa:
$ for h in host1 host2;do ssh $h ls > $h.txt; done
$ ls
host1.txt host2.txt
$

Resources