Crontab failed to scp - linux

Am trying to send MySQL backup from server1 to server2 for backups storage
My command in shell..
scp myfile user#root:/file destination /
I use ssh-agent for paraphrase key pass..
In crontab normal just link to my shell When I use commands .. bash myshel.sh Is successful send but in crontab no will give Root mail saying Permission denied (publickey,gassapi-with -mic,password)
root /usr/Backup/myshell.sh

Related

How to save FTP session logs in file in Linux

I am using a bash script in Linux to transfer files to a server. My script is running from cron and I have directed output to a file but I cannot know from logs if the file has been transferred to B server or not.
This is the cron:
1>>/home/owais/script_test/logs/res_sim_script.logs 2>>/home/owais/script_test/logs/res_sim.logs
And the FTP is as below:
cd ${dir}
ftp -n $HOST <<END_SCRIPT
quote USER $USER
quote PASS $PASSWD
lcd $dir
cd $destDir
bin
prompt
put FILENAME
bye
The only thing that I get in the logs is:
Local directory now Directory_Name
Interactive mode off.
Instead of using FTP, there is rsync. Rsync is a fast and extraordinarily versatile file copying tool. It can copy locally, to or from another host over any remote shell, or to, or from a remote rsync daemon.
More information at the following webpage, https://linux.die.net/man/1/rsync
I have used ftp -inv Host << EOF >> LogFilePath and it worked. Thank you all for the support

lftp fails on cPanel cron

I have a bash script to backup a database and send it to another server, running the script on ssh (root) it sends the file correctly, but when using cPanel cron, i got this error:
cd: Fatal error: pseudo-tty allocation failed: No such file or directory
put: Fatal error: pseudo-tty allocation failed: No such file or directory
It looks like to fail on lftp changing to uploads folder
Cron
/bin/sh /home/test/backup/script.sh >> /home/test/backup/log.txt 2>&1
Bash
/bin/lftp sftp://user:pass#domain.com:22/uploads -e "put $FILE2; bye"
I assume your issue is this: You are logging in via SFTP using a ssh key (otherwise every time you try to sftp you would normally be required to enter a password and that will mess the cron). Probably you have the ssh key saved under the user root but when you execute the cron, it executes as a cpanel user (unless you executed directly in the root crontab). If executed as a cpanel user, and that user doesn't have the ssh key, then the cron hangs asking for the sftp password. Please be sure that the ssh private key used to SFTP as root is also added to the cpanel user's account under which the cron is executed. It should work then

FTP output redirect to a file on my linux box using shell script

I am looking for a Bash script to redirect a simple ls command output to a file on my Linux box from my FTP Server.
Here follows the step by step commands to illustrate what I am looking to script. The FTP site can be accessed without a user/password, so i am entering user as anonymous and password as blank when prompted.
ftp <FTP_SERVER>
Connected to <FTP_SERVER> (IP_ADDRESS).
220 Microsoft FTP Service
Name (<FTP_SERVER>:root): anonymous
331 Anonymous access allowed, send identity (e-mail name) as password.
Password:
230 Anonymous user logged in.
Remote system type is Windows_NT.
ftp> ls /Outgoing/Artemis/incremental/Hashes-1492870261.zip
227 Entering Passive Mode (10,37,108,77,5,87).
125 Data connection already open; Transfer starting.
04-22-17 07:11AM 227634 Hashes-1492870261.zip
226 Transfer complete.
I need the output of the command 'ls' command I am executing to be saved in a file on my linux box.
Here is the script i have:
ftp FTP_SERVER <<EOF >outputfile
quote USER anonymous
quote PASS
prompt noprompt
ls -la /Outgoing/Artemis/incremental/Hashes-1492870261.zip
quit
EOF
when i execute this i get a login failed error.
sh -x test.sh
+ ftp FTP_SERVER`
Password:
Login failed.
local: /Outgoing/Artemis/incremental/Hashes-1492870261.zip: No such file or directory
I used some random password as a test instead of blank (null) which I used previously, but still get the same error. How can I fix this?
Turn interactive mode off:
ftp -i -n FTP_SERVER <<EOF >outputfile
user <user> <password>
binary
ls -la .
quit
EOF
You might get on better with lftp:
lftp -e 'ls -l someFile.zip; quit' -u USER,PASSWORD FTPSERVER > ls.txt
where all the words in capitals need replacing by your own values.
Or you could try with curl:
curl ftp://FTPSERVER --user USER:PASSWORD

scp file to different user in the remote server from local

generally, i login to a server xyz.com using my login credentials(myuserid#xyz.com),
my home = /home/user/myuserid/
after login, i do "su - someuser" to access the files.
i would like to copy a file from local machine to a directory in someuser eg: /abc/someuser/temp
for this, i am using
scp somefile.txt myuserid#xyz.com:/abc/someuser/temp/
it is asking my password for myuserid and then says.. /abc/someuser/temp/ permission denied
what command shall i use to copy a file to su in remote host?
You'll have to use someuser's credentials to do the scp
scp somefile.txt someuser#xyz.com:/abc/someuser/temp/
Alternatively you can give myuserid permission to someuser's home directory.

How to give permission for the cron job file?

I have set the cron tab for my site. But I have got message in my mailing id like this "Permission denied" for the script. Can anyone help me telling what may be the problem.
Thanks......
You get this error while setting the crontab? or from a script running from the cron?
If while setting the crontab, try this:
You type: crontab -e
You get: -bash: /usr/bin/crontab: Permission denied
Problem: Your user is not in the cron group.
Solution: As root, edit the /etc/group file, find the cron group and add
your user to that line (the usernames are comma-separated).
Then re-login as your user.
Verify: Run command "groups". You should see "cron" in there.
(from http://www.parseerror.com/argh/crontab-e-Permission-denied.txt)
friends if any one wants schedule crons from other user
just do this
root user:-
ls /usr/bin/crontab
chmod 4755 /usr/bin/crontab
echo PATH
vi /etc/crontab
SHELL=/bin/bash
PATH=/usr/java/jdk1.5.0_22/bin:/root/bin:/usr/java/jdk1.5.0_22/bin:/usr/lib/qt-3.3/bin:/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/X11R6/bin
MAILTO=root
HOME=/
cd /etc/cron.d
create cron.allow file if not exist
vi cron.allow
root
other username
save and exist
su – username
/usr/bin/crontab -e
schedule here ……………….
I ran into this issue today and was baffled until I realized that the denied commands were SSH commands. I had forgotten that I was connecting with an SSH key that required a passphrase, so the real issue had nothing to do with cron in my case.
My solution was to create an additional key for this script with no passphrase (using ssh-keygen), install it on the remote server, and specify it in the script with the -i flag to the ssh commands.
ssh -i /path/to/id_rsa.no-passphrase user#remote command-to-run

Resources