Thursday, June 25, 2015

Screenshots in Fedora LXDE

I was just about to make screenshot on my system and I noticed that there is no application for that. Best solution I found is gnome-screenshot, it's easy to use and it has a nice "flash" effect when making screenshot.

Step 1 is install gnome-screenshot

sudo dnf install gnome-screenshot -y

I don't like my screenshots mixing with other pictures I have so I created directory Screenshots

mkdir ~/Pictures/Screenshots

How to make gnome-screenshot put your shots there:

       gsettings set org.gnome.gnome-screenshot auto-save-directory "file:///home/$USER/Pictures/Screenshots/"
 

Now we need to make "shortcut" to taking a screenshot. In my case, button PrintScreen will take full screenshoot and Ctrl+PrintScreen will make only active windows screenshot.

Open ~/.config/openbox/lxde-rc.xml with your favorite editor and insert these lines inside before </keyboard> line.

<keybind key="Print">
        <action name="Execute">
                <command>gnome-screenshot</command>
        </action>
</keybind>
<keybind key="C-Print">
        <action name="Execute">
                <command>gnome-screenshot -w</command>
        </action>
</keybind>

After you saved and closed file, execute:

openbox --reconfigure

Tuesday, June 16, 2015

Mikrotik backup script using SSH/SCP - easy and dirty way

*Disclaimer*
This is not high-end, ultra safe script. It's considered a bad practice to store your passwords in cleartext, especially in script file. I am beginner in scripting and this is just a way to automate task that when done by hand could take very long time. But, if you still want to proceed, read on!

For this script to work we will need to install program called sshpass, install it from your distro repository. This script assumes that all your Mikrotik devices have same username and password and SSH service enabled.
EDIT: After some experimenting it seems that sometimes ConnectTimeout=10 is not enough, if you have troubles connecting, increase to 20.

#!/bin/bash
#Don't just copy and paste this script, adjust all paths to your needs
#You can remove any line starting with hashtag because those are comments and they are not executed

NOW=$(date +%d-%b)
#This line creates variable NOW with day and month, like "03-May"
#I will use this example, "03-May" to make it easier to follow

COUNTER=0
#sets counter to zero

mkdir /home/locodog/backups/mikrotik/$NOW
#creates directory with name according to date "03-May" for example

for i in $(cat /home/locodog/mikrotik-backup-script/tiks.txt); do
#tiks.txt is file that has list of all Mikrotik devices you want to backup
COUNTER=$((COUNTER + 1))
#adds +1 to counter for every device it attempts to backup

echo -e $COUNTER"." "Backing up device \033[1m$i\033[0m" 
#tells you which device is currently being backed up

sshpass -p "password" ssh -oStrictHostKeyChecking=no -o ConnectTimeout=15 $i -l username system backup save name=$NOW
#Connects to mikrotik device from list and creates backup file with name "03-May"

sshpass -p "password" scp -oStrictHostKeyChecking=no -o ConnectTimeout=10 username@$i:$NOW.backup /home/locodog/backups/mikrotik/$NOW/$i.backup
#Downloads previously created backup file but now that file is downloaded into folder "03-May" 
#and file has name 192.168.1.1.backup (or whatever IP address your Mikrotik has
#keep in mind that backup file inside your Mikrotik will not be deleted and it will have a name 03-May.backup
done

TOTAL=$(echo $COUNTER)
#Creates variable "total" with value equal to total number of devices that were backed up (or attempted)
for f in $(cat /home/locodog/mikrotik-backup-script/tiks.txt); do
if [ ! -f /home/locodog/backups/mikrotik/$NOW/$f.backup ];
 then
echo -e "Backup of \033[1m$f\033[0m was not done" && COUNTER=$((COUNTER - 1 ))
fi
done
#this section checks is there any backup file missing and
#gives you IP address of device where backup could not be performed for whatever reason

echo -e "Backup was successful on \033[1m$COUNTER\033[0m out of \033[1m$TOTAL\033[0m Mikrotik devices."
#Here you get report, on how many devices did you try to do backup and how many sucessful

After adjusting this script to your needs save it in file for example backup-mikrotik.sh
and make it executable with chmod +x backup-mikrotik.sh

File tiks.txt (or whatever you decide to call it) should have list of IP addresses of your Mikrotik devices like this:
192.168.1.1
192.168.1.2
192.163.25.4
10.25.66.8

And so on. This file should be plain text, that means don't create it with LibreOffice or anything like that. Use plain text editor like Gedit, Kate, Leafpad, Vim.

Wednesday, June 10, 2015

Samsung Printer/Scanner drivers for Linux Fedora

To install Samsung printer drivers go to
http://www.samsung.com/us/support/downloads
Click on Printers and choose your printer model. Most likely it will be tar.gz file. In termial go to folder where you downloaded the file. In most cases it will be ~/Downloads and type

tar xvf uld_v1.00.06.tar.gz

This is name of my downloaded file, you will here type name of file you downloaded. After content of archive is extracted type

sudo ~/Downloads/uld/install.sh

Also, you will here type path where you extracted file from step 1. This command must be executed with elevated privileges (sudo) or it will fail.
Confirm execution of script with "y" and go through EULA with Enter. Be carefull because after EULA you will have another confirmation and if you just keep pressing Enter like maniac default answer will be "No" and you will have to run script again. So, choose yes. Next you will be asked do you want to make firewall rules if you intent do use printer over network, after answering that (I choosed No) script will finish installation of drivers.
Start Printer Settings and turn on you printer. Click Add and select your make and model. You will be asked to install drivers, but since we already did that you can hit Cancel. Confirm friendly name and apply settings. Printer installation should now be completed, if you want you can print test page.

Monday, June 8, 2015

SSH Access Denied and SFTP connection closed Fedora

After having my Fedora 21 messed enough that it wasn't worth fixing, I decided to install new shiny Fedora 22 LXDE spin. Installation went (almost) good. And you will see why I say almost.

Upon finishing installation and enabling ssh server with

systemctl start sshd
systemctl enable sshd

I tried to login from my other computer. After asking for password it would simply return Access Denied. Using verbose mode didn't help me much. With my first google search I came upon this problem with guy who was asking help simply saying that he solved his problem by changing his password. 
I know my password, I just typed it in to log onto my system, and I have saved profile on second computer so I don't even need to type it. (yes, I know it's a security hole).
I never had pleasure to meet with ssh setup so I assumed that there is something in sshd_config that I didn't set correctly. So, instead of going through all settings I just copied sshd_config from co-worker's computer, because it works there.
No go. Another 20 minutes of going through every single line inside config file, no go.
Eventually i thought "hmm, maybe I should try changing the password"...
And I kid you not, it was the solution. I even typed in same exact password and this time it worked.

Now just to restore some files I previously backed up, over sftp, I'm too lazy to copy it over usb drive.
sftp username@hostname...
insert password
Connection Closed
Goddamit!

Let's browse Google some more. Most people were having trouble with key login, but almost nobody with password.

sftp -v myusername@myhost
And there it was
debug1: Exit code 127

To make story short, when I copied sshd_config file from another computer I didn't consider that it was Slackware. In most cases that wouldn't matter, but in mine it did because in sshd_config there is line

Subsystem sftp /usr/libexec/sftp-server

But on my system, location of sftp server was

/usr/libexec/openssh/sftp-server

After changing that line in config file and systemctl restart sshd it worked.

Saturday, June 6, 2015

DNS probe finished no internet problem on Windows 8

One day I fired my laptop with Windows 8.1 on it, connected to home WiFi and started Chrome but no website could be open.
Error was DNS probe finished no internet.

I was able to ping 8.8.8.8 and anything else by IP address, but no hostname could be resolved. Tried setting Google DNS servers, OpenDNS servers, but with same result.

ipconfig /flushdns also didn't do any good.

netsh winsock reset didn't help

Other device on that same WiFi network (my phone) worked just fine. After connecting laptop to cable connection everything worked without any problems. It was clear that problem was wireless related.
Next step was downloading new set of drivers for wireless card. Completely uninstalling and deleting old drivers and installing new (actually it was same version) solved my problem.

One more possible solution I came accross, but I didn't have chance to test because driver reinstallation fixed everything is

netsh int tcp set heuristics disabled
netsh int tcp set global autotuninglevel=disabled
netsh int tcp set global rss=enabled


Creating swap file

One of problems I came across was that I didn't have partition for swap. In modern day, if you have enough RAM, swap partition is not necessary but it is required if you want to use suspend-to-disk. I found out that I could make swap file that will work same as swap partition.

You will need root access:

sudo -s 

Now create actual file

dd if=/dev/zero of=/swapfile bs=1M count=2048

This command will create file named swapfile with size of 2GB (1MB x 2048). You can adjust these numbers to fit your needs.

Let's close huge local vulnerability but giving only root rights to read and write to the file with

chown root:root /swapfile
chmod 0600 /swapfile

After file is created type this command to make it swap

mkswap /swapfile


And to activate swap file

swapon /swapfile


To make your system activate this file after reboot, open /etc/fstab file with

vi /etc/fstab


And insert this line (adjust name of your file accordingly)

/swapfile none swap 0 0 



To check if your file is active or not, type

free -m




Removing Swap file or partition


First find your swap partition with

sudo fdisk -l

And look for Linux Swap file. This is sample output


Device     Boot     Start       End   Sectors  Size Id Type
/dev/sda1  *         2048    206847    204800  100M  7 HPFS/NTFS/exFAT
/dev/sda2          206848 164339711 164132864 78.3G  7 HPFS/NTFS/exFAT
/dev/sda3       164339712 564938406 400598695  191G  7 HPFS/NTFS/exFAT
/dev/sda4       564938750 625139711  60200962 28.7G  5 Extended
/dev/sda5       564938752 621996031  57057280 27.2G 83 Linux
/dev/sda6       621998080 625139711   3141632  1.5G 82 Linux swap / Solaris

In my case, swap partitions is /dev/sda6, to disable swap on this partition I type

swapoff /dev/sda6

Now go to /etc/fstab and comment out line with swap. This is that line in my fstab file:

UUID=9f0f62d3-44fd-4286-9b90-1faff3b4a44c swap                    swap    defaults        0 0

To comment it, just add # on the beginning of the line. After rebooting, using free -m you could check that your swap is not there anymore (or if you had more than 1 swap partition/file it is reduced).