Copy file to remote host through intermediate (jump) host - linux

Now I am connecting to endpoint using this command:
ssh my.jumphost.com -t 'export iip=111.22.3.44; bash'
The problem is that I don't have direct access to this IP, I mean I can't just ssh 111.22.3.44 from my jumphost. So basically I can reach destination host only if I export this variable with IP address from jumphost.
I've already looked into scp command and the way with ssh tunnel, but seems like both of them are required direct access to destination host.
I've also tried
cat test.py | ssh my.jumphost.com -t 'export iip=111.22.3.44' 'cat > /home/user/test.py'
but in that case file is being copied to jumphost only.
Any advice or guidance would be greatly appreciated!

Related

Sending files to SSH client over SSH using SCP

The situation is that my client machine does not use static IP but the server machine is using static IP.
I connect to the server machine using ssh from the client machine.
Are there any easy ways to send files from the server machine to the client machine using scp or other commands?
I want to execute commands inside the server.
You can scp from the client machine and get the file from the server.
client$ scp user#server:< path to file > < path to destination folder in client >
eg, if you want file1 in the home directory from the server copied to the current directory in your client, you can do the following:
$ scp user#server:~/file1 .
ref: https://linuxize.com/post/how-to-use-scp-command-to-securely-transfer-files/
If you don't really need to connect using the ssh command, you can connect using the sftp command (ftp-like interface through an ssh connection):
$ sftp user#server
And then just use the command get:
sftp> get <file>
Commands like ls, cd and pwd also work in this sftp interface.

SCP and sshpass - Can't copy from remote source to local destination using script on PIs - debian11

I am struggling to copy files from a remote source to my local destination
I am using scp and I have tried adding sshpass to send the password
I have a script that copies from my local source to a remote destination which works:
sudo sshpass -p "pi" ssh -o StrictHostKeyChecking=no pi#$VAR_IP ls /some_dir
this just connects to it without having to put in additional commands to accept the connection if it is the first time
sudo sshpass -p "pi" scp /path_to_app/$VAR_APP pi#$VAR_IP:/home/pi/$VAR_APP/
this successfully copies from my local source to my remote destination
Now... Even though the scp documentation says I can scp remote source to local destination
I can't seem to get it to work, here is how I am trying to do it in a different script:
sudo sshpass -p "pi" ssh -o StrictHostKeyChecking=no pi#$VAR_IP ls /some_dir
this is just to initialize not to have to accept connection, same as the last script
sudo sshpass -p "pi" scp pi#$VAR_IP:/home/pi/$VAR_APP/logs/file /some_local_dir/
This gives me the error: scp: /home/pi/App_Name/logs/file: No such file or directory
the path doesn't exist on local but does on remote, so it seems it is trying to find it locally instead of remotely, any ideas on this?
I looked at all the related posts about this and the man pages but can't find an answer to my specific case
I cannot do the cert key thing as I have too many sites, it would take forever
I saw in one of the posts someone tried it without sshpass, I tried it too like this:
sudo scp pi:pi#$VAR_IP:/home/pi/$VAR_APP/logs/file /some_local_dir/
This gave me the error: ssh: Could not resolve hostname pi: Name or service not known
I don't think it works like that so I didn't go further down that vein
I hope I gave enough info with clarity
any help would really be appreciated
thank you so much for your time and input
You mention that this command is not working sudo sshpass -p "pi" scp pi#$VAR_IP:/home/pi/$VAR_APP/logs/file /some_local_dir/
Did you check this?
sudo sshpass -p "pi" ssh pi#$VAR_IP 'ls -l /home/pi/$VAR_APP/logs/file /some_local_dir/' to check the directory is exist
If that issue is still there, I recommend you to try pssh and pscp which are parallel ssh that could do the same thing as sshpass
I managed to fix it, for anyone that comes across this problem
Here is how I found the fix:
The file I was looking for was a root file but I was sshing as pi.
Even though I sudoed the script, and sudoed sshpass
That does not mean scp is sudo, so each command in a line needs its own sudo
eg:
sudo sshpass -p "pi" scp pi#IP:/file /local_dir/
This doesn't work because sshpass has sudo but scp does not, however
sudo sshpass -p "pi" sudo scp pi#IP:/file /local_dir/
This works perfectly because scp now has sudo rights

Shell script remotely

I have one script running on server and doing some job on other server
I have many scp commands and ssh commands, this is why each time I have to enter the remote server password at each remote command.
is there any way to establish ssh connection between the servers so I type the remote password only once?
thanks
I would suggest to setup an ssh config together with ssh keys. In a nutshell the config will hold an alias for one or more remote servers.
ssh remote_server1
ssh remote server2
While your config file will look something like this:
Host remote_server1
Hostname 192.168.1.12
user elmo
IdentityFile ~/.ssh/keys/remote.key
...
If an ssh config file is not for you (although I can highly recommend it), you can use sshpass as well.
sshpass -p 't#uyM59bQ' ssh username#server.example.com
Do note that the above does expose your password. If someone else has access to your account, the history command will show the code snippet above.

how to transfer data between local and remote server connected via intermediate server?

I can login by ssh -X servA from local, then ssh -X servB from servA
To copy data from local to servB, I scp files from local to servA, then from servA to servB.
Is it feasible to copy files from local to servB directly and vice versa?
You can use nc (net cat) as a proxy for ssh.
So for your example, edit your ~/.ssh/config file to look like this:
Host servB
ProxyCommand ssh -q servA nc servB 22
As long as nc is in your path you should now be able to ssh or scp directory to servB
If you don't have nc you can do it with ssh -W if your version is new enough (>= OpenSSH 5.4),
Host ServB
ProxyCommand ssh -W ServB:22 servA
Use ProxyCommand in ssh config file.
This is what I usually do (I do it in a Mac machine don't know if it's different from a Windows machine):
Once you have set up connection with any of the servA or servB you can do:
Copy from local to servA or servB:
$ scp -P <port-number used> <file location to copy from> <username_in servA/servB>#localhost:<file location to copy to>
NOTE: This works being in your local machine without ssh-ing to any of the servA/servB, just need to establish connection.
or from servA to servB:
$ scp -P <port-number used> <username_in servA>#localhost:<file location to copy from> <port-number used> <username_in servB>#localhost:<file location to copy to>
NOTE: I haven't tried this scp from server to server but seems a little bit straight forward.
Just trying to help here.

Download a file from a Server with double ssh

I connect to a Server with ssh
Step 1:
$ ssh userid#something.com
and then it asks for password and everything is ok
Then I connect to a DB
Step2:
$ssh user1#smthing_else
and then it asks for password and everything is ok
Now when I type ls I can see the file that I want to download...
How can I download this file on my Desktop..??
You need to scp the file twice in order to bring it to local m/c if you don't have direct access. First ssh to the server 1 and run the command to download it. Then run this command again from your local m/c.
scp -r -i path-to-secret-key ubuntu#ec2-address:/home/ubuntu/app-folder-location /home/user/local-mc-location
As you don't have the key, use the below command
scp -r ubuntu#ec2-address:/home/ubuntu/app-folder-location /home/user/local-mc-location
Update:
path-to-secret-key is the private key address which is used in ec2 instances to ssh. They are used for authentication and are present in home/.ssh/private-key. They have a permission of 400 and are either .pem extension for unix m/c's or ppk extension for windows m/c's
I guess you can't directly reach the "inner" host from the outside? In that case you have to ssh into the outer host, then you can use scp to copy the file from the inner host to the outer one. Accordingly, you can then copy the file to your local pc from the outer host with scp (or whatever you can use in that case).
scp works like this (to copy a local file to a remote host):
scp myfile.txt user#somehost.com:/home/user/whatever
resp. like this (remote to local):
scp user#somehost.com:/home/user/whatever/myfile.txt .

Resources