ch-t chat

———-

Been ungodly sick lately with all the bad weather. One of the reasons not much new has come out. hopefully that will change soon.

Do not know if I mentioned but I pur Arch linux on the Pogoplug V2. Arch linux is different though most distros are supposed to go to systemd. I also installed arch in a desktop remotely via ssh. The installs are documented on www.instructable.com/member/computothought.

We are getting close to 1,000,000 views on ww.instructable.com/member/computothought. we are going to have a big party when it does.

Can’t believe I still have a few old pentium one based boards. Everything form a router, robot  to a somple desktop

Probably turn my old 486 laptop dumb terminal. Putty will load from a floppy.

P000-256-267-481-f1016

——————————————————————–

Sometimes every once in a while, you find an old generally unused command, but now you find it is very valuable. One such command is know as ttyrec. As the name suggests, you can probably record what is typed at the terminal.   This is excellent for documentation purposes. It is a must for people who do any amount of work on the command line.  An example: recently did a remote install of Arch linux on a remote machine.  It would have been nice to have a record of all the work done.

Well how do you use it? There are actually two parts of the system. ttyrec does the recording and ttyplay  will playback what you have recorded.  So you will want to invoke ttyrec with a filename (for later playback,) which will generate a new shell or prompt.  Go ahead and trpe in the coomands you want to do, then when you are finnished, use <control>d to exit the the recording. You will see the word exit to confirm the exit of the ttyrec program.

$ ttyrec filename
$ echo hello world
hello world
$<control>d   exit

Now to playback what your have typed in exactly as you have typed it then use:

$ ttyshow filename

The playback will show everything as it was done like a movie.Now anytime you want to check out what you did during the recording session, it is at your fingertips. Also too makes for great educational media.

———————————————————————

List commonly used commans:

$ history | awk {‘print $2’} | sort | uniq -c | sort -k1 -rn | head
127 sudo
96 vim
86 cd
62 youtube-dl
61 ls
29 ssh
29 rm
29 gcc
28 vlc
23 locate

List files in last modified order

$ ls -al
total 684
drwxr-xr-x    2 eddie eddie   4096 Mar 22 16:54 .
drwxr-xr-x 1002 eddie eddie 299008 Mar 24 03:06 ..
-rw-r–r–    1 eddie eddie  33245 Mar 22 16:54 biscuit.jpg
-rw-r–r–    1 eddie eddie  50528 Mar 22 16:47 mm1.jpg
-rw-r–r–    1 eddie eddie  77970 Mar 22 16:47 mm2.jpg
-rw-r–r–    1 eddie eddie  77621 Mar 22 16:48 mm4.jpg
-rw-r–r–    1 eddie eddie  38828 Mar 22 16:48 mm5.jpg
-rw-r–r–    1 eddie eddie  30756 Mar 22 16:49 mm6.jpg
-rw-r–r–    1 eddie eddie  22836 Mar 22 16:50 mm7.jpg
-rw-r–r–    1 eddie eddie  11167 Mar 22 16:50 mm8.jpg
-rw-r–r–    1 eddie eddie  30467 Mar 22 16:51 mm9.jpg

$ ls -t
biscuit.jpg  mm8.jpg  mm6.jpg  mm4.jpg  mm1.jpg
mm9.jpg      mm7.jpg  mm5.jpg  mm2.jpg
$ ls -tr
mm1.jpg  mm4.jpg  mm6.jpg  mm8.jpg  biscuit.jpg
mm2.jpg  mm5.jpg  mm7.jpg  mm9.jp

———————————————————————

$ ssh -G 2>&1 | grep -e illegal -e unknown > /dev/null && echo “System clean” || echo “System infected”
System clean

———————————————————————

$ apt-get moo
(__)
(oo)
/——\/
/ |    ||
*  /\—/\
~~   ~~

“Have you mooed today?”…

$ aptitude -vv moo
Didn’t I already tell you that there are no Easter Eggs in this program?

$ aptitude -vvv moo
Stop it!
Didn’t I already tell you that there are no Easter Eggs in this program?
$ aptitude -vvvv moo
Okay, okay, if I give you an Easter Egg, will you go away?
$ aptitude -vvvvv moo
All right, you win.

/—-\
——-/      \
/               \
/                |
—————–/                  ——–\
———————————————-
eddie@oedt01:~$ aptitude -vvvvvv moo
What is it?  It’s an elephant being eaten by a snake, of course.

———————————————————————

Almost to a million views on the instructables site.

Screenshot from 2014-03-11 17:40:46

———————————————————————

The ability to get data from one computer to another is essential for one or more computers on a network. What happens that one computer essential becomes a host/server and the other computer the client, A good example of this might be a networked game, but it all starts with a simple peiece of cde that can get more complicated as the need arises. The server code for this simple example.

Server.c

#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <time.h>

int main(int argc, char *argv[])
{
int listenfd = 0, connfd = 0;
struct sockaddr_in serv_addr;

char sendBuff[1025];
time_t ticks;

listenfd = socket(AF_INET, SOCK_STREAM, 0);
memset(&serv_addr, ‘0’, sizeof(serv_addr));
memset(sendBuff, ‘0’, sizeof(sendBuff));

serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
serv_addr.sin_port = htons(5000);

bind(listenfd, (struct sockaddr*)&serv_addr, sizeof(serv_addr));

listen(listenfd, 10);

while(1)
{
connfd = accept(listenfd, (struct sockaddr*)NULL, NULL);

ticks = time(NULL);
snprintf(sendBuff, sizeof(sendBuff), “%.24s\r\n”, ctime(&ticks));
write(connfd, sendBuff, strlen(sendBuff));

close(connfd);
sleep(1);
}
}

The you need code for the client

Client.c

#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <arpa/inet.h>

int main(int argc, char *argv[])
{
int sockfd = 0, n = 0;
char recvBuff[1024];
struct sockaddr_in serv_addr;

if(argc != 2)
{
printf(“\n Usage: %s <ip of server> \n”,argv[0]);
return 1;
}

memset(recvBuff, ‘0’,sizeof(recvBuff));
if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
printf(“\n Error : Could not create socket \n”);
return 1;
}

memset(&serv_addr, ‘0’, sizeof(serv_addr));

serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(5000);

if(inet_pton(AF_INET, argv[1], &serv_addr.sin_addr)<=0)
{
printf(“\n inet_pton error occured\n”);
return 1;
}

if( connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0)
{
printf(“\n Error : Connect Failed \n”);
return 1;
}

while ( (n = read(sockfd, recvBuff, sizeof(recvBuff)-1)) > 0)
{
recvBuff[n] = 0;
if(fputs(recvBuff, stdout) == EOF)
{
printf(“\n Error : Fputs error\n”);
}
}

if(n < 0)
{
printf(“\n Read error \n”);
}

return 0;
}

Of course to make thecode usable you would

$ gcc server.c -o server

$ gcc client.c client

The goal in this case is for the client to grab the time from the server or host. Optimally you want to do this between two diffferent machines, but for basic testing purposes you ran run the programs on the same machine,

$ ,/server &

[1] 6823

Useage: ./client hostipaddress

$ ./client 192.168.2.85

Tue Mar 11 13:40:15 2014

Another example that ais a bit more interactive. The server will play pback what is typed at the client.

Server1.c

/*
C socket server example
*/

#include<stdio.h>
#include<string.h>    //strlen
#include<sys/socket.h>
#include<arpa/inet.h> //inet_addr
#include<unistd.h>    //write

int main(int argc , char *argv[])
{
int socket_desc , client_sock , c , read_size;
struct sockaddr_in server , client;
char client_message[2000];

//Create socket
socket_desc = socket(AF_INET , SOCK_STREAM , 0);
if (socket_desc == -1)
{
printf(“Could not create socket”);
}
puts(“Socket created”);

//Prepare the sockaddr_in structure
server.sin_family = AF_INET;
server.sin_addr.s_addr = INADDR_ANY;
server.sin_port = htons( 8888 );

//Bind
if( bind(socket_desc,(struct sockaddr *)&server , sizeof(server)) < 0)
{
//print the error message
perror(“bind failed. Error”);
return 1;
}
puts(“bind done”);

//Listen
listen(socket_desc , 3);

//Accept and incoming connection
puts(“Waiting for incoming connections…”);
c = sizeof(struct sockaddr_in);

//accept connection from an incoming client
client_sock = accept(socket_desc, (struct sockaddr *)&client, (socklen_t*)&c);
if (client_sock < 0)
{
perror(“accept failed”);
return 1;
}
puts(“Connection accepted”);

//Receive a message from client
while( (read_size = recv(client_sock , client_message , 2000 , 0)) > 0 )
{
//Send the message back to client
write(client_sock , client_message , strlen(client_message));
}

if(read_size == 0)
{
puts(“Client disconnected”);
fflush(stdout);
}
else if(read_size == -1)
{
perror(“recv failed”);
}

return 0;
}

Client1.c

/*
C ECHO client example using sockets
*/
#include<stdio.h> //printf
#include<string.h>    //strlen
#include<sys/socket.h>    //socket
#include<arpa/inet.h> //inet_addr

int main(int argc , char *argv[])
{
int sock;
struct sockaddr_in server;
char message[1000] , server_reply[2000];

//Create socket
sock = socket(AF_INET , SOCK_STREAM , 0);
if (sock == -1)
{
printf(“Could not create socket”);
}
puts(“Socket created”);

server.sin_addr.s_addr = inet_addr(“127.0.0.1”);
server.sin_family = AF_INET;
server.sin_port = htons( 8888 );

//Connect to remote server
if (connect(sock , (struct sockaddr *)&server , sizeof(server)) < 0)
{
perror(“connect failed. Error”);
return 1;
}

puts(“Connected\n”);

//keep communicating with server
while(1)
{
printf(“Enter message : “);
scanf(“%s” , message);

//Send some data
if( send(sock , message , strlen(message) , 0) < 0)
{
puts(“Send failed”);
return 1;
}

//Receive a reply from the server
if( recv(sock , server_reply , 2000 , 0) < 0)
{
puts(“recv failed”);
break;
}

puts(“Server reply :”);
puts(server_reply);
}

close(sock);
return 0;
}

of couse, you need to compile these also.

$ gcc server1.c -o server1

$gcc client2.c -o client1

$. ./server1 &

$ ./client

Socket created
Connection accepted
Connected

Enter message : test
Server reply :
test
Enter message :
asdasdf asd asd
Server reply :
asdasdf
Enter message : Server reply :
asdasdf
Enter message : Server reply :
asdasdf
Enter message : a s asd asd f
Server reply :
asdasdf
Enter message : Server reply :
ssdasdf
Enter message : Server reply :
asdasdf
Enter message : Server reply :
asdasdf
Enter message : Server reply :
fsdasdf
Enter message : ^CClient disconnected

[3]+  Done                    ./server1
<Control>c was keyed in.

———————————————————————

Some linuc commands to play with. Your distro may vary.

1. SYSTEM

$ uname –a => Display linux system information
$ uname –r => Display kernel release information (refer uname command in detail)
$ cat /etc/redhat_release => Show which version of redhat installed
$ uptime => Show how long system running + load (learn uptime command)
$ hostname => Show system host name
$ hostname -i => Display the IP address of the host (all options hostname)
$ last reboot => Show system reboot history (more examples last command)
$ date => Show the current date and time (options of date command)
$ cal => Show this month calendar (what more in cal)
$ w => Display who is online (learn more about w command)
$ whoami => Who you are logged in as (example + sreenshots)
$ finger user => Display information about user (many options of finger command)

2. Hardware

$ dmesg => Detected hardware and boot messages (dmesg many more options)
$ cat /proc/cpuinfo => CPU model
$ cat /proc/meminfo => Hardware memory
$ cat /proc/interrupts => Lists the number of interrupts per CPU per I/O device
$ lshw => Displays information on hardware configuration of the system
$ lsblk => Displays block device related information in Linux (sudo yum install util-linux-ng)
$ free -m => Used and free memory (-m for MB) (free command in detail)
$ lspci -tv => Show PCI devices (very useful to find vendor ids)
$ lsusb -tv => Show USB devices (read more lsusb options)
$ lshal => Show a list of all devices with their properties
$ dmidecode => Show hardware info from the BIOS (vendor details)
$ hdparm -i /dev/sda # Show info about disk sda
$ hdparm -tT /dev/sda # Do a read speed test on disk sda
$ badblocks -s /dev/sda # Test for unreadable blocks on disk sda

3. Statistics

$ top => Display and update the top cpu processes (30 example options)
$ mpstat 1 => Display processors related statistics (learn mpstat command)
$ vmstat 2 => Display virtual memory statistics (very useful performance tool)
$ iostat 2 => Display I/O statistics (2sec Intervals) (more examples)
$ tail -n 500 /var/log/messages => Last 10 kernel/syslog messages (everyday use tail options)
$ tcpdump -i eth1 => Capture all packets flows on interface eth1 (useful to sort network issue)
$ tcpdump -i eth0 ‘port 80’ => Monitor all traffic on port 80 ( HTTP )
$ lsof => List all open files belonging to all active processes.(sysadmin favorite command)
$ lsof -u testuser => List files opened by specific user
$ free –m => Show amount of RAM (daily usage command)
$ watch df –h => Watch changeable data continuously(interesting linux command)

4. Users

$ id => Show the active user id with login and group(with screenshot)
$ last => Show last logins on the system (few more examples)
$ who => Show who is logged on the system(real user who logged in)
$ groupadd admin => Add group “admin” (force add existing group)
$ useradd -c “Sam Tomshi” -g admin -m sam => Create user “sam” and add to group “admin”(here read all parameter)
$ userdel sam => Delete user sam (force,file removal)
$ adduser sam => Add user “sam”
$ usermod => Modify user information(mostly useful for linux system admins)

5. File Commands

$ ls –al => Display all information about files/ directories(20 examples)
$ pwd => Show current directory path(simple but need every day)
$ mkdir directory-name => Create a directory(create mutiple directory)
$ rm file-name => Delete file(be careful of using rm command)
$ rm -r directory-name => Delete directory recursively
$ rm -f file-name => Forcefully remove file
$ rm -rf directory-name => Forcefully remove directory recursively
$ cp file1 file2 => Copy file1 to file2 (15 cd command examples)
$ cp -r dir1 dir2 => Copy dir1 to dir2, create dir2 if it doesn’t exist
$ mv file1 file2 => Move files from one place to another(with 10 examples)
$ ln –s /path/to/file-name link-name => Create symbolic link to file-name (examples)
$ touch file => Create or update file (timestamp change)
$ cat > file => Place standard input into file (15 cat command examples)
$ more file => Output the contents of file (help display long tail files)
$ head file => Output the first 10 lines of file (with different parameters)
$ tail file => Output the last 10 lines of file (detailed article with tail options)
$ tail -f file => Output the contents of file as it grows starting with the last 10 lines
$ gpg -c file => Encrypt file (how to use gpg)
$ gpg file.gpg => Decrypt file

6. Process Related

$ ps # Display your currently active processes (many parameters to learn)
$ ps aux | grep ‘telnet’ # Find all process id related to telnet process
$ pmap # Memory map of process (kernel,user memory etc)
$ top # Display all running processes (30 examples)
$ kill pid # Kill process with mentioned pid id (types of signals)
$ killall proc # Kill all processes named proc
$ pkill processname # Send signal to a process with its name
$ bg # Resumes suspended jobs without bringing them to foreground (bg and fg command)
$ fg # Brings the most recent job to foreground
$ fg n # Brings job n to the foreground

7. File Permission Related

$ chmod octal file-name # Change the permissions of file to octal , which can be found separately for user, group and world
octal value (more examples)
4 – read
2 – write
1 – execute
Example
$ chmod 777 /data/test.c # Set rwx permission for owner , rwx permission for group, rwx permission for world
$ chmod 755 /data/test.c # Set rwx permission for owner,rw for group and world
$ chown owner-user file # Change owner of the file (chown more examples)
$ chown owner-user:owner-group file-name # Change owner and group owner of the file
$ chown owner-user:owner-group directory # Change owner and group owner of the directory
Example
$ chown bobbin:linoxide test.txt
$ ls -l test.txt
-rw-r–r– 1 bobbin linoxide 0 Mar 04 08:56 test.txt
8. Network

$ ifconfig –a # Display all network ports and ip address (set mtu and other all options)
$ ifconfig eth0 # Display specific ethernet port ip address and details
$ ip addr show # Display all network interfaces and ip address(available in iproute2 package,powerful than ifconfig)
$ ip address add 192.168.0.1 dev eth0 # Set ip address
$ ethtool eth0 # Linux tool to show ethernet status (set full duplex , pause parameter)
$ mii-tool eth0 # Linux tool to show ethernet status (more or like ethtool)
$ ping host # Send echo request to test connection (learn sing enhanced ping tool)
$ whois domain # Get who is information for domain
$ dig domain # Get DNS information for domain (screenshots with other available parameters)
$ dig -x host # Reverse lookup host
$ host google.com # Lookup DNS ip address for the name (8 examples of host command)
$ hostname –i # Lookup local ip address (set hostname too)
$ wget file # Download file (very useful other option)
$ netstat -tupl # Listing all active listening ports(tcp,udp,pid) (13 examples)

9. Compression / Archives

$ tar cf home.tar home # Create tar named home.tar containing home/ (11 tar examples)
$ tar xf file.tar # Extract the files from file.tar
$ tar czf file.tar.gz files # Create a tar with gzip compression
$ gzip file # Compress file and renames it to file.gz (untar gzip file)

10. Install Package

$ rpm -i pkgname.rpm # Install rpm based package (Installing, Uninstalling, Updating, Querying ,Verifying)
$ rpm -e pkgname # Remove package
Install from source
./configure
make
make install (what it is)

11. Search

$ grep pattern files # Search for pattern in files (you will this command often)
$ grep -r pattern dir # Search recursively for pattern in dir
$ locate file # Find all instances of file
$ find /home/tom -name ‘index*’ # Find files names that start with “index”(10 find examples)
$ find /home -size +10000k # Find files larger than 10000k in /home

12. Login (ssh and telnet)

$ ssh user@host # Connect to host as user (secure data communication command)
$ ssh -p port user@host # Connect to host using specific port
$ telnet host # Connect to the system using telnet port

13. File Transfer

scp
$ scp file.txt server2:/tmp # Secure copy file.txt to remote host /tmp folder
$ scp nixsavy@server2:/www/*.html /www/tmp # Copy *.html files from remote host to current system /www/tmp folder
$ scp -r nixsavy@server2:/www /www/tmp # Copy all files and folders recursively from remote server to the current system /www/tmp folder
rsync
$ rsync -a /home/apps /backup/ # Synchronize source to destination
$ rsync -avz /home/apps linoxide@192.168.10.1:/backup # Synchronize files/directories between the local and remote system with compression enabled

14. Disk Usage

$ df –h # Show free space on mounted filesystems(commonly used command)
$ df -i # Show free inodes on mounted filesystems
$ fdisk -l # Show disks partitions sizes and types(fdisk command output)
$ du -ah # Display disk usage in human readable form (command variations)
$ du -sh # Display total disk usage on the current directory
$ findmnt # Displays target mount point for all filesystem
$ mount device-path mount-point # Mount a device

15. Directory Traverse

$ cd .. # To go up one level of the directory tree(simple & most needed)
$ cd # Go to $HOME directory
$ cd /test # Change to /test directory

– See more at: http://linoxide.com/guide/linux-command-shelf.html#sthash.wKhpbua3.dpuf

———————————————————————

Cast irin griddle toast.

SUNP0002

Advertisements