I am attempting to use R to ssh into a remote unix server from windows (I normally connect through PuTTY) and execute a command. I have already been able to write code to sftp files from this server to my local using the commands below:
data_bin <- getBinaryURL(file_remote, userpwd = credentials,
ftp.use.epsv = FALSE)
writeBin(data_bin, file_output)
However, the first function seems restricted to pointing to individual files. What I would like to do would be to connect to my remote server in a similar fashion, but this time pass on a command that I would normally execute at the command line? Can anyone point me to the correct methodology or resource. Thanks!
Try the documentation from ?system.
Just as an example, if I run the command sftp via system():
> system("sftp")
usage: sftp [-1246aCfpqrv] [-B buffer_size] [-b batchfile] [-c cipher]
[-D sftp_server_path] [-F ssh_config] [-i identity_file] [-l limit]
[-o ssh_option] [-P port] [-R num_requests] [-S program]
[-s subsystem | sftp_server] host
sftp [user#]host[:file ...]
sftp [user#]host[:dir[/]]
sftp -b batchfile [user#]host
Related
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
I have read that RSYNC over SSH requires a single colon : after USER#HOST, whereas connecting directly to a daemon require a double colon ::. However in order to get my RSYNC command line to work shown below, i have to use a double colon?? Can someone please explain this? download is the name of the remote virtual directory.
Cheers,
rsync -trv --progress --timeout=10 -e 'ssh -p 46000' hexfeed#11.22.33.44::download /tmp/test1
The :: tells this rsync command to expect the remote to be already running a daemon, but the -e then says that instead of opening a network connection to the given server at the default port of 873, it should run the command ssh... to create the connection and expect a daemon at the other end.
This can only work if the remote runs a command like rsync --server --daemon --config=somefile . when you login via ssh -p 46000.
I am trying to run a shell script to execute a binary on a remote linux box. Both the binary and the shell script are on my local window machine. Is there any way through which i can run the binary to the remote machine directly from windows through command line tools like PLINK?
I don't want to put the binary and the script to all the remote linux boxes which
i want them to run on,Instead I want to run the shell script which will intern invoke the binary and do the desirable functions directly through my local machine.
You can run the shell script remotely, just by piping it through ssh:
cat my_script.sh | ssh -T my_server
(Or whatever the windows/plink equivalent is.)
However, you can't run the binary remotely through a pipe, the file will have to exist on the remote server. You can do this by pushing the file from your windows machine to a known location on the remote server, and then editing your script to expect the file to exist in that location:
scp my_binary my_server:/tmp
cat my_script.sh | ssh -T my_server
And then just have your script run that binary:
/tmp/my_binary
Or you can write the script so that it pulls the binary file from a central location where you're hosting it:
wget -O /tmp/my_binary http://my_fileserver/my_binary
/tmp/my_binary
Note, if the shell script doesn't do anything else besides invoke the binary, then you don't need it. You can just fire the commands directly through ssh:
ssh -T my_server "cd /tmp && wget http://my_fileserver/my_binary && ./my_binary"
You will have to copy the binary to the remote Linux box before it can be executed. However, you could have a script on the windows machine that uses sftp to transfer the binary program to a temporary directory under /tmp before running it, so there is no manual setup required.
I am currently developing the new VBScript to execute the Shell (through Putty software) in UNIX server,
Set shell = WScript.CreateObject("WScript.Shell")
shell.Exec D:\Putty.exe hostname -l username -pw password 1.sh
I am getting connection refused error.
when I run the below command without my script (1.sh)
shell.Exec D:\Putty.exe hostname -l username -pw password
Connection is getting established without any issues.
Also, I just wanted to extract the output, once extracted, the session should get closed automatically.
This doesn't work in putty.exe. Putty has however a dedicated program to do these kind of things, it's called plink.exe - there you can pass commands and read the output just as you would expect, and your example should work just like you specified it.
PuTTY Link: command-line connection utility
Release 0.63
Usage: plink [options] [user#]host [command]
("host" can also be a PuTTY saved session name)
Options:
-V print version information and exit
-pgpfp print PGP key fingerprints and exit
-v show verbose messages
-load sessname Load settings from saved session
-ssh -telnet -rlogin -raw -serial
force use of a particular protocol
-P port connect to specified port
-l user connect with specified username
-batch disable all interactive prompts
The following options only apply to SSH connections:
-pw passw login with specified password
-D [listen-IP:]listen-port
Dynamic SOCKS-based port forwarding
-L [listen-IP:]listen-port:host:port
Forward local port to remote address
-R [listen-IP:]listen-port:host:port
Forward remote port to local address
-X -x enable / disable X11 forwarding
-A -a enable / disable agent forwarding
-t -T enable / disable pty allocation
-1 -2 force use of particular protocol version
-4 -6 force use of IPv4 or IPv6
-C enable compression
-i key private key file for authentication
-noagent disable use of Pageant
-agent enable use of Pageant
-m file read remote command(s) from file
-s remote command is an SSH subsystem (SSH-2 only)
-N don't start a shell/command (SSH-2 only)
-nc host:port
open tunnel in place of session (SSH-2 only)
-sercfg configuration-string (e.g. 19200,8,n,1,X)
Specify the serial configuration (serial only)
I am trying to automate my SFTP command using a UNIX shell script but for some reason it doesn't work. Here is my code as below. Please share your thoughts/insights.
#This ftp script will copy all the files from source directory into the local directory.
#!/bin/sh
HOST='sftp.xyz.com'
USER='ABC'
PASSWORD='123'
SRC_DIR='From_Src'
TRGT_DIR='/work/'
FILE='abc.txt'
sftp -u ${USER},${PASSWORD} sftp://${HOST} <<EOF
cd $SRC_DIR
lcd $TRGT_DIR
get $FILE
bye
EOF
echo "DONE"
When I try executing the above code I get the below error.
sftp: illegal option -- u
usage: sftp [-1246Cpqrv] [-B buffer_size] [-b batchfile] [-c cipher]
[-D sftp_server_path] [-F ssh_config] [-i identity_file] [-l limit]
[-o ssh_option] [-P port] [-R num_requests] [-S program]
[-s subsystem | sftp_server] host
sftp [user#]host[:file ...]
sftp [user#]host[:dir[/]]
sftp -b batchfile [user#]host
There is no -u option for sftp, see the manual for available options. You can pass the username in this format:
sftp username#hostname
So in your case:
sftp sftp://${USER}#${HOST} <<EOF
This will prompt you the password though. If you don't want a password prompt, take a look at this topic: How to run the sftp command with a password from Bash script?
First, learn how to set up keys so that you can ssh, scp, and sftp to a server without a password. Look at ssh-keygen. It is fairly easy. I bet there are how tos on this site. In brief, generate your keys with ssh-keygen. They are created in ~/.ssh. Then add your public key to ~/.ssh/authorized_keys on the destination host where ~ is the home directory of the user you want to log in as. i.e. "ABC" in your example.
Now, you can just do "sftp ABC#sftp.xyz.com" and you will be at the sftp prompt on sftp.xyz.com. From there, getting your script to work should be easy.
My real suggestion is blow off sftp and use scp. e.g.
scp /path/to/the/source_file user#host:/remote/path/file
Its that simple. No "cd" and other gunk to deal with. You are making this way harder than it really is.
Good luck