Transfer file from remote server behind another server (jumphost) to local machine using pscp - sftp

I am trying to transfer file from a remote server behind jumphost server to my local machine using pscp and SFTP. I know how to do it for a remote server but not for jumphost-ed server. Also I need to make it automated (no user interaction needed), how do I do it? Thanks.

You can do it the same way you already execute command over the jump host:
Execute commands on remote server behind another server (jumphost) using Plink
Just use pscp, where you are using plink.
Something like this:
pscp -pw password2 -proxycmd "plink -ssh user1#jumphost -pw password1 -nc anotherIP:22" user2#example.com:/remote/path/file.txt .

You can use a proxycommand:
scp -oProxyCommand = "ssh -W %h:%p username/password#jumphost" username/password#remotehost:/some/path/on/remote/host some/path/on/local/machine

Related

sftp equivalent of lftp command returns Permission denied

My task is to port working lftp command to it's sftp equivalent. I have a private key generated at ~/key.key.
Working lftp command:
lftp -u Username,'pass' xxx.xxx.xx.xx ssl:key-file key.key
Not working sftp equivalent:
sftp -i ~/key.key Username#xxx.xxx.xx.xx
The sftp command asks me for a password, i provide the same one available in lftp command and the process exits with Permission denied (publickey,password,keyboard-interactive).
Is there any way i can debug what's happening or maybe something very obvious I'm doing wrong? Thanks in advance for any clues.
You seem to be use using FTPS (FTP over TLS/SSL) with lftp, not SFTP (over SSH).
OpenSSH sftp is SFTP-only, it does not support FTPS. Those are completely different protocols.

Unable to login to unix server using Plink in batch ifle

we are facing one issue with Plink while running the batch files, we are running batch files using autosys, the batch files are available in my windows client server and one of the batch file will call the plink to connect the unix server but we are facing the issue to connect the unix server, when I run the batch script using command prompt then the plink can be connected the unix server but it is not happening with autosys to run the batch scripts. below is the Plink command...
call %aScrDir%plink -l %hypSrvUser% -pw %hypSrvPwd% %essSvr% "sh /xxxxxxxxxxx"
when we see the error file which is generated by autosys there are some errors
"The server's host key is not cached in the registry. You
have no guarantee that the server is the computer you
think it is.
The server's rsa2 key fingerprint is:
ssh-rsa 2048 f9:5e:2a:4a:11:ed:40:91:80:3a:13:04:08:05:e7:ac
If you trust this host, enter "y" to add the key to
PuTTY's cache and carry on connecting.
If you want to carry on connecting just once, without
adding the key to the cache, enter "n".
If you do not trust this host, press Return to abandon the
connection.
Store key in cache? (y/n) Connection abandoned."
could you please give the suggestion to avoid this situations and where do we add the host key in the server.
appreciate your action on this.
Option 1 : If allowed, first start a manual connection to the server and confirm the SSH key
Option 2 : If you are running the last version, there is a -hostkey switch to indicate in command line the expected host key
Option 3 : Use something like
echo N | %aScrDir%plink -l %hypSrvUser% -pw %hypSrvPwd% %essSvr% "sh /xxxxxxxxxxx"
That is, pipe the n character to the plink command to answer no to the query to save the host key.
Try this it would be working fine.
cd to the plink.exe file directory
echo Y | .\plink.exe -pw xxyyxxyy root#host_ip 'ls -lah'

How to transfer a file using sftp in UNIX

I want to transfer a .png file from a directory on my computer to a directory on a remote server.
I have to use SFTP to secure the file and transfer mode. And I already have a UNIX script (.ksh) file to copy the files in the normal mode. How do I implement the transfer in SFTP mode?
Use sftp instead of whatever command you are using in your .ksh script. See sftp man for reference.
You may also want to look at scp secure copy - scp man.
EDIT
sftp is mostly for interactive operations, you need to specify host you want to connect to:
sftp example.com
you will be prompted for username and passsword, and the interactive session will begin..
Although it can be used in scripts, the scp is much more easy to use:
scp /path/to/localfile user#host:/path/to/dest
you will be prompted for password..
Edit 2
Both scp and sftp use ssh as underlying protocol, see this and this
The best way to setup them to run from scripts is to setup passwordless authentication using keys. See this and this. I use this extensively on my servers.. After you setup keys, you can run
scp -i private-key-file /path/to/local/file user#host:/path/to/remote
sftp -oIdentityFile=private-key-file -b batch-file user#host
If you want to authenticate with password, you may try the expect package. The simplest script may look like this:
#!/usr/bin/expect
spawn sftp -b batch-file user#host
expect "*?assword:*"
send "pasword\n"
interact
See this, this and this for more info.
Send commands through sftp on one line:
Make a file and save it as my_batch_file:
cd /root
get blah.txt
bye
Run this to execute your batch file:
eric#dev /home/el $ sftp root#10.30.25.15 < my_batch_file
Connecting to 10.30.25.15...
Password:
sftp> cd /root
sftp> get blah.txt
Fetching /root/blah.txt to blah.txt
sftp> bye
The file is transferred
That moved the blah.txt from remote computer to local computer.
If you don't want to specify a password, do this:
How to run the sftp command with a password from Bash script?
Or if you want to do it the hacky insecure way, use bash and expect:
#!/bin/bash
expect -c "
spawn sftp username#your_host
expect \"Password\"
send \"your_password_here\r\"
interact "
You may need to install expect, change the wording of 'Password' to lowercase 'p' to match what your prompt receives. The problems here is that it exposes your password in plain text in the file as well as in the command history. Which nearly defeats the purpose of having a password in the first place.

How to rename a file via sftp using one-liner?

Is there a one-liner to use "sftp" to move a remote file on a remote server to another directory (on the same remote server)?
For sftp it is rename. If you ask for sftp invocation, using bash it's something like
sftp -b - host <<<"rename a/file b/file"

How to change shell in unix

I'm new to unix. I need to copy file over ssh. This is what I do
me#localhost ~ $ ssh you#remotehost
Then I established ssh so I get
you#remotehost ~ $
I'd like to use scp to copy files from localhost to remotehost. Once I have ssh connection, how do I change to prompt back to me#localhost so that I can use the scp command? Is there a command for that?
Edit: The reason I need the ssh is because after I copied the file I have to execute it. Is there a way to remain in the ssh session and use scp to copy the file that I'm editing at localhost
You do not have to first create an SSH connection to use SCP. Simply use the scp command from your shell, and it will connect to the other server.
Most shells exit with exit. CtrlD may also work.
You can also:
scp /path/to/local-file you#remotehost:/remote/path
Try screen command.
You can use scp on either side. Here are two examples:
If you are on your local host:
scp myfile you#remotehost:
If you are on the remote host:
scp you#<localhost's hostname>:myfile .
Substitute your localhost's hostname for <localhost's hostname> in the second command. If you are behind a router, it will be easier to use the first one.
Both assume that myfile is in the home directory on localhost and is being sent to the home directory on remotehost.

Resources