ssh and rsync in one command line from Ruby - ruby

I need to write a bash command line that uses ssh to connect to "first_machine" and from here run rsync to another machine remoteuser#10.2.2.150.
This is my code:
first_machine = user#192.16.58.12
cmd="ssh #{first_machine} \"rsync -e\"ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null\" --archive -H /home/user/aaaaa remoteuser#10.2.2.150:/home/remoteuser/\" "
exit_status = system("#{cmd}")
I tested the ssh command and also the rsync separately. They work fine, so I can do ssh from the machine where I run the Ruby script and:
rsync -e"ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null" --archive -H /home/user/aaaaa remoteuser#10.2.2.150:/home/remoteuser/
works well when launched in the #{first_machine} -> folder is copied.
Instead, when I run my code through the Ruby script I receive the error:
Permission denied, please try again.
Permission denied, please try again.
Permission denied (publickey,password).
rsync: connection unexpectedly closed (0 bytes received so far) [sender]
rsync error: unexplained error (code 255) at io.c(605) [sender=3.0.9]
This error does not occur if I run the command separately.

First, fix the user problems pointed out by the Tin Man in the other answer. Do you have public key authentication set up between first_machine and the user running the ruby script?
Then, look at your quoting and you'll see that the command you're passing to ssh consists only of rsync -e. Maybe try this:
first_machine = 'user#192.16.58.12'
cmd="ssh #{first_machine} \"rsync -e 'ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null' --archive -H /home/user/aaaaa remoteuser#10.2.2.150:/home/remoteuser/\" "
exit_status = system(cmd)
Disclaimer: I've never used Ruby before, but a 2 minute glance at some tutorial sites made it clear you've got syntax errors in your sample code.
Is there a reason not to do this as a bash script?

When you "run the command separately", you're running it as a user with known permissions and an environment that's set.
Permission denied, please try again.
Permission denied, please try again.
Permission denied (publickey,password).
is your hint that the permissions aren't right for the account when it logs in from the code. This isn't a Ruby problem, it's a result of not using the right account, or not setting up the environment; It's outside Ruby.

Related

SSH sudo inside script different behaviour

I'm trying to set some automation inside local network and started working with some shell scripting and something that I saw - very strange behaviour SSH inside script according to how script running(with or without sudo):
What I have:
ComputerA and ComputerB.
Inside ComputerA:
A shell script script.sh:
cp /dir1/file1 /dir2/file2
ssh username#ComputerB "sudo reboot"
/etc/ssh/ssh_config file with some configurations to work without ssh-keys (they always changes on ComputerB):
StrictHostKeyChecking no
UserKnownHostsFile=/dev/null
GlobalKnownHostsFile=/dev/null
Inside ComputerB:
In /etc/sudoers file:
username ALL=(ALL:ALL) NOPASSWD:ALL
When I connecting through SSH to ComputerA and running script.sh without sudo, I get permission error to write to /dir2 (it's OK) and next command on ComputerB executes normally (reboots ComputerB), but I'm running sudo script.sh. It copy file and then I got strange - SSH asks me username password. Tried different variants to change ssh command to something like:
ssh -t username#ComputerB "echo user_pass | sudo -S reboot"
but nothing helped.
So I need help to figure out what happens and what to do to execute sudo script.sh without entering password for ssh command inside.
Thanks!
Don't run script.sh with sudo on computerA; instead modify the script like so:
sudo cp /dir1/file1 /dir2/file2
ssh username#ComputerB "sudo reboot"
The reason that you're seeing the strange behaviour is that you're actually becoming root on computerA (I assume you have a keypair set-up for your regular user and expect to connect to computerB passwordless?), and that root on computerA doesn't have a keypair that computerB knows about.

Enter Private Key Passphrase for Git in While SSH'ing bash script

Here's the code I have so far in an <<EOF while ssh'ing into a box
mkdir ~/.ssh/
rm ~/.ssh/config
touch ~/.ssh/config
echo 'Host *******' >> ~/.ssh/config
echo ' StrictHostKeyChecking no' >> ~/.ssh/config
cp ~/gittoken ~/.ssh/id_rsa
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_rsa
cd /root
git clone ***********
expect "assword:"
send "$git_token_passphrase\r";
interact
The error I'm getting is
Permission denied (publickey).
fatal: Could not read from remote repository.
I've sort of combined multiple different answers I've seen on SO that I thought might help my case.
I can run all these commands when I'm manually running them on the box. So my guess is that running them while ssh'ed through a script requires some extra step. Also, expect and send don't work on my system, but I figured I'd put them there anyway to show I'd tried them.
/bin/bash: line 105: expect: command not found
/bin/bash: line 106: send: command not found
/bin/bash: line 107: interact: command not found
You may use sshpass to achieve this.
But since now the security of the whole setup rests not in the head of
whoever knows the password but is only as strong as
the security in the script or—if you're a bit
less slack—into some file holding the password, you might as well just
remove password protection from your SSH key by running
ssh-keygen -f ~/.ssh/id_rsa -p
and entering an empty password twice.

exec bash file with putty

I'm trying to execute a bash file with putty/plink but gives me an error.
On windows Ive got this batch:
E:\putty\plink.exe user#link -pw password -m E:\folder\test.sh
on bash file Ive got this:
#!/bin/bash
vtoff
vtadmin check connector /PCS/ConnectionModels/Arbor/
and the error:
C:\folder>e:\folder\test.bat
C:\folder>e:\putty\plink.exe user#link -pw password -m e:\folder\test.sh
ksh[4]: vtoff: not found
ksh[5]: vtadmin: not found
C:\folder>
The documentation for plink says that -m specifies that it should "read remote command(s) from file".
Since #!/bin/bash isn't a command, and your error message references ksh, it's pretty clear that bash is nowhere to be found in this question!
As for your actual error message, it seems that the commands aren't found, probably because they're not in your PATH.

Linux expect script

I've spent the better part of 8 hours trying to figure this out with google, so I hope this warrants asking here.
I need a script that will auto-enter a password when I try to connect from my lubuntu image in vmware to a physical device connected by usb.
I've tried at least 50 different scripts I've found online, but none of them worked (or even recognised spawn as a command)
This is my script:
#!/usr/expect
spawn CPY2T_old.sh
expect "root#10.9.8.2's password:"
send "ThePassword"
expect eof
The contents of CPY2T_old.sh is
#!/bin/bash
cd hellolinux/src/Exercise$1
scp $2 root#10.9.8.2:
The above bash script works fine, but I have to enter the password, which is what I'm trying to avoid in the first place. The expect script gives the following when I execute in cmd:
spawn: command not found
couldn't read file "root#10.9.8.2's password:": no such file or directory
The program 'send' can be found in the following packages:
* mailutils-mh
* nmh
Try: sudo apt-get install <selected package>
couldn't read file "eof": no such file or directory
I've downloaded mailutils and nmh at least a dozen times by now as well. Elsewhere I read I need to #echo off at the top, but this command isn't recognised and gives an error.
EDIT: I can't do passwordless ssh to this device, so please don't suggest it.
I see 2 errors: first
#!/usr/expect
You want
#!/usr/bin/expect
That should have caused an error: how are you launching your expect script?
Second
send "ThePassword"
You forgot to hit enter
send "ThePassword\r"
#!/usr/bin/expect
set timeout 60
spawn ssh user#ip
expect "user#ip's password: "
send "Password\r"
interact
Note:Please be sure of all scripts are executable with command $ chmod +x #file_name or $ chmod 700 #file_name.
\r to execute
Github link:https://github.com/asarari207/Lunix_sh

Rsync command fails in Cygwin

I've been struggling with this for a day now and can't figure out what I'm doing, I haven't been able to find anything with Google some hopefully someone here can help:
I'm trying to use rsync to sync a folder between my Ubuntu server and my window's machine using Cygwin. I issue:
$ rsync -av -e "ssh 10.0.0.28 -pxxxx -i /home/my_user/.ssh/id_rsa -l my_user" my_user#10.0.0.28:/backup/folder/ /backup/folder/
bash: 10.0.0.28: command not found
rsync: connection unexpectedly closed (0 bytes received so far) [Receiver]
rsync error: error in rsync protocol data stream (code 12) at io.c(226) [Receiver=3.1.1]
If I just extract the code in the double quotes and run it it correctly opens the ssh connection. Does anyone know what is going on here?
You probably should not specify an address and login in ssh command. Rsync will do it for you. Try
rsync -av -e "ssh -pxxxx -i /home/my_user/.ssh/id_rsa" my_user#10.0.0.28:/backup/folder/ /backup/folder

Resources