Executing remote command and saving input to file [closed] - linux

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
$

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 copy files over 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 7 years ago.
Improve this question
How do I copy a file using ssh from my test server to my production server, how do I do this for a single file and how do I do this for multiple files?
From Window to Linux
Download https://cygwin.com/ this will give you a proper terminal which will then allow you to run the linux commands as listed below in the From Linux to Linux section.
From Linux to Linux
The essential command is this
The command
scp [ssh login to remote server]:[filepath] [local filepath]
To copy a single file example
scp user#your.server.example.com:/path/to/foo/[filename] /home/user/Desktop/[filename]
To copy a directory example
scp -r user#your.server.example.com:/path/to/foo /home/user/Desktop/
To use full power of scp you need to go through next steps:
Setup public key authentication
Create ssh aliases
Then, for example if you'll have this ~/.ssh/config:
Host test
User testuser
HostName test-site.com
Port 22022
Host prod
User produser
HostName production-site.com
Port 22022
you'll save yourself from password entry and simplify scp syntax like this:
scp -r prod:/path/foo /home/user/Desktop # copy to local
scp -r prod:/path/foo test:/tmp # copy from remote prod to remote test
More over, you will be able to use remote path-completion:
scp test:/var/log/ # press tab twice
Display all 151 possibilities? (y or n)

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

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

Find out how many SSH connections currently exist [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 a simple shell script on my Linux server which checks if an rsync job is running or if any client accesses some directories from the server via Samba. If this is the case then nothing happens, but if are there no jobs and Samba isn't used than the server goes into hibernation.
Is there any simple command which I can use to check if an SSH connection to the server exists? I want to add this to my shell script so that the server doesn't hibernate if such a connection exists.
Scan the process list for sshd: .
Established connections look something like this: sshd: <username>…
ps -A x | grep [s]shd
should work for you.
use who command
it gives output like
username pts/1 2013-06-19 19:51 (ip)
You could parse that to see how many non locals are added and get their usernames (or there are options see man who for more info
gives a count of how many non localhost users there are
who | grep -v localhost | wc -l

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.

Resources