Showing results for 
Search instead for 
Did you mean: 

removing files via SSH

Go to solution
Regular Advisor

removing files via SSH

I need to copy & remove files from a vendor's sftp site. Since I want to do this via a script I've been trying to use scp & ssh. I can copy the files from the site using scp no problem. The vendor set up our ssh key on their side.

But I can't get the ssh command to remove the files on the vendor site. Here's the command I'm trying.

ssh -i /ssh/serverkey rm /outbound/clientfiles*07*09*

When I issue this command, it just comes back to the command prompt with no message at all, and the files are still there.

I have rights to remove these files and can remove them if I manually log in to the sftp site.

Should what I'm trying work?
Steven E. Protter
Exalted Contributor

Re: removing files via SSH


rsync -avH --stats --delete -e ssh /web/

--delete # deletes files at the destination deleted at the source
-e ssh uses ssh and therefore permits public key password exchange.

Steven E Protter
Owner of ISN Corporation
Mel Burslan
Honored Contributor

Re: removing files via SSH


ssh -vvv -i /ssh/serverkey rm /outbound/clientfiles*07*09*

and post the output here please. But I believe, your vendor may be to blame here. They mihgt be sensing the ssh logins and putting you to an exit command directly. Are you sure you are allowed to go in via ssh ? In other words, if you run:

ssh -i /ssh/serverkey

do you get a shell prompt ?
UNIX because I majored in cryptology...
Regular Advisor

Re: removing files via SSH

Thanks Mel,

I don't get a shell prompt so I think I'll go back to the vendor for some assistance. Here's the pertinent part of the output when I use the -v

debug1: Next authentication method: publickey
debug1: Offering public key: /ssh/serverkey
debug1: Server accepts key: pkalg ssh-rsa blen 277
debug1: read PEM private key done: type RSA
debug1: Authentication succeeded (publickey).
debug1: channel 0: new [client-session]
debug1: Entering interactive session.
debug1: channel 0: free: client-session, nchannels 1
Connection to closed.
debug1: Transferred: stdin 0, stdout 0, stderr 40 bytes in 0.0 seconds
debug1: Bytes per second: stdin 0.0, stdout 0.0, stderr 4150.3
debug1: Exit status -1
Mel Burslan
Honored Contributor

Re: removing files via SSH

Definitely something is happening on the JP Morgan and Chase site because you do not exit your rm command with exit code "0" which should indicate suceess. Instead it is exiting with code -1.

Good luck....
UNIX because I majored in cryptology...
Honored Contributor

Re: removing files via SSH

If you don't escape your wildcards, your shell will attempt to expand them on your local host, before the SSH command is started.

If no matches are found, what happens next depends on your shell configuration: some shells will then run the command line as-is, with the wildcards un-expanded. That may be happening here, as the ssh command gets invoked. Other shells may explicitly display an error message.

But if the un-escaped wildcard expression matches on your local host, the shell will blindly follow its programming and expand it to the list of matched filenames on local host, which is not what you want at this time.

Put backslashes in front of the wildcards to make sure they'll survive intact to the remote system.

ssh -i /ssh/serverkey rm /outbound/clientfiles\*07\*09\*

If that does not work, the remote account may be set up with a "scponly" pseudo-shell which ignores any non-scp commands. If this is the case, your only possibility is to use the batch mode of sftp:

sftp -o IdentityFile=/ssh/serverkey -b - <rm /outbound/clientfiles*07*09*

Regular Advisor

Re: removing files via SSH

Thanks Matt,

Yours is the first response to show the -o IdentityFile option as part of the way to use sftp in a batch mode as part of a script. It's not in the man pages or any thread I could find on the ITRC.

Now instead of using scp I can just use sftp to both transfer the files and remove them from the vendor's site.

Muchas Gracias!
Steven Schweda
Honored Contributor

Re: removing files via SSH

> [...] It's not in the man pages [...]

The "-o" option itself should be documented
clearly enough, but more digging may be
needed to expose what the SSH options might
be which you can pass through it.

dy # sftp -h
Usage: sftp [options] user@host[:file [file]]
-o ssh_option Used to pass options to ssh in the format used in ssh_config file.

"man ssh_config" knows all, tells all.