How to save in a file bash and psql history? - bash

I need help for the question on topic : i'm new to centos 6 environment and i need to export in a file the 2 histories so i can send it through e-mail.Thanks.

By default bash typed commands are saved in ~/.bash_history and psql saves it in ~/.psql_history, so you could send those files with this command:
$ mail -s 'My history files' -a ~/.bash_history -a ~/.psql_history myemail#gmail.com
Note that not all mail commands support the -a flag to send attachments, heirloom-mailx in ubuntu does.

Related

script to connect to a "list.txt" of servers

I am trying to find a way to connect to a list of servers written in a simple textfile to run one command and write the output to a file...
The small problem is, I have to login with a password... but it would not a problem to paste the password into the script.
the full command would be:
ssh "server_from_list.txt uptime | awk -F, '{sub(".*up ",x,$1);print $1}' >> /home/kauk2/uptime.out
lets assume the password is: abcd1234
Any suggestions??? I am not fit in scripting, sorry...
Many thanks to you all in advance...
regards,
Joerg
Ideally you should set up password-less login, but failing that you can use sshpass. First, get a single command working by trying the following:
export SSHPASS=abcd1234
Then you can try:
sshpass -e ssh user#server1 'uname -a'
When you get that debugged and working, you can use GNU Parallel to run the command on all servers in a file called list.txt
user#server1
user#server2
user#server3
user#server4
The command will be:
parallel -k -a list.txt sshpass -e ssh {} 'uptime'

Running sudo via ssh on remote server

I am trying to write a deployment script which after copying the new release to the server should perform a few sudo commands on the remote machine.
#!/bin/bash
app=$1
echo "Deploying application $app"
echo "Copy file to server"
scp -pr $app-0.1-SNAPSHOT-jar-with-dependencies.jar nuc:/tmp/
echo "Execute deployment script"
ssh -tt stefan#nuc ARG1=$app 'bash -s' <<'ENDSSH'
# commands to run on remote host
echo Hello world
echo $ARG1
sudo ifconfig
exit
ENDSSH
The file gets copied correctly and the passed argument printed as well. But the prompt for the password shows for two seconds then it says "Sorry, try again" and the second prompt shows the text I enter in plain text (meaning not masked) but also does not work if I enter the password correctly.
stefan#X220:~$ ./deploy.sh photos
Deploying application photos
Copy file to server
photos-0.1-SNAPSHOT-jar-with-dependencies.jar 100% 14MB 75.0MB/s 00:00
Execute deployment script
# commands to run on remote host
echo Hello world
echo $ARG1
sudo ifconfig
exit
stefan#nuc:~$ # commands to run on remote host
stefan#nuc:~$ echo Hello world
Hello world
stefan#nuc:~$ echo $ARG1
photos
stefan#nuc:~$ sudo ifconfig
[sudo] password for stefan:
Sorry, try again.
[sudo] password for stefan: ksdlgfdkgdfg
I tried leaving out the -t flags for ssh as well as using -S for sudo which did not help. Any help is highly appreciated.
What I would do :
ssh stefan#nuc bash -s foobar <<'EOF'
echo "arg1 is $1"
echo "$HOSTNAME"
ifconfig
exit
EOF
Tested, work well.
Notes :
for the trick to work, use ssh key pair instead of using a password, it's even more secure
take care of the place of your bash -s argument. Check how I pass it
no need -tt at all
no need sudo to execute ifconfig and better use ip a
I came up with another solution: Create another file with the script to execute on the remote server. Then copy it using scp and in the calling script do a
ssh -t remoteserver sudo /tmp/deploy_remote.sh parameter1
This works as expected. Of course the separate file is not the most elegant solution, but -t and -tt did not work when inlining the script to execute on the remote machine.

Sending mail using shell command in Ruby

I'm trying to send an email using shell command in my ruby script.
I use command
%x{echo "sometext" | mail -s "Account report #{file_tmp}" -a /home/linux/reports/#{file} #{address[0]}}
and I get
Send options without primary recipient specified. Usage: mail
-eiIUdEFntBDNHRVv~ -T FILE -u USER -h hops -r address -s SUBJECT -a FILE -q FILE -f FILE -A ACCOUNT -b USERS -c USERS -S OPTION users sh:
line 2: send_report#gmail.com: command not found
Why an email address is taken as second line of command and how to fix it?
Try stripping newlines on the file variable:
%x{echo "sometext" | mail -s "Account report #{file_tmp}" -a /home/linux/reports/#{file.strip} #{address[0]}}

How can I start a screen session using a specific config file?

I would like to be able to start a screen session using a certain config file. I know that I can use -c and then the file path to the config file but if I do that, then the sh script I am using does not work. You can see the sh script below:
#!/bin/bash
cd /media/kiancross/Minecraft_Server/1.6.4
screen -d -m -S MinecraftServer ./start.sh
screen -r MinecraftServer
I would have thought that I can do the following code:
#!/bin/bash
cd /media/kiancross/Minecraft_Server/1.6.4
screen -d -m -S -c MinecraftServer $HOME/config_file/mcserver.config ./start.sh
screen -r MinecraftServer
But then I get a message saying:
There is no screen to be resumed matching MinecraftServer.
After then checking to see if there is a screen session running it says that there are no screen sessions running
No Sockets found in /var/run/screen/S-kiancross.
Does anybody know how I can do this so that I can use a custom config file?
The command should be:
screen -d -m -S MinecraftServer -c $HOME/config_file/mcserver.config ./start.sh
The name of the screen session goes after -S and the path of the config file goes after -c. You inserted -c before the screen name.

Create postgresl database dump and download it using scp with sshpass in one command

Hi I want to automate my workflow of creating Postgres dump and downloading it. I want to do it from my local machine, for now I figured it out so far in two seperate commands:
sshpass -p "FuckinHardPass" ssh andi#1.2.3.4 "pg_dump -U andi andi_some_db -f /home/andi/PSQL_DUMPS/andi_some_db.sql"
sshpass -p "FuckinHardPass" scp -r andi#1.2.3.4:/home/andi/PSQL_DUMPS/andi_some_db.sql .
how can I join it in one command using pipes etc?
Thanks James Hightower for hint, using your answer I complete it in one command:
sshpass -p "FuckinHardPass" ssh andi#1.2.3.4 "pg_dump -U andi andi_some_db" > andi_some_db.sql
As pg_dump defaults to stdout as it's output file, and ssh displays the command's stdout on it's own stdout, you could do something like:
ssh andi#1.2.3.4 'pg_dump -U andi andi_some_db' > andi_some_db.sql
which would save the output from the command on your local disk as andi_some_db.sql
Depending on the size of your dump and the speed of your connection, you could perhaps benefit from pre-compressing your output:
ssh andi#1.2.3.4 'pg_dump -U andi andi_some_db | gzip' > andi_some_db.sql.gz
And so on.

Resources