Disclaimer: Its a collection from lots of other site(s) and few of my notes. I would also like to declare that I am not owning lots of its content. Please feel free to contact me directly if you want me to remove any of your content, that you don't want to share to other through this blog.
Monday, 14 March 2016
who need other app to copy files from pc to phone
Some time we need to copy few files from our computer to our smart phone and most of the time we use the data cable to copy data for that, and sometime, we need to install some other application again on our system [ example: Android File Transfer ] and who need keep taking there data cable!
Who wants to read long document to get things done. So lets do the work:
Requirement:
1. You need to have python install
2. Your system and phone need to be in the same wifi network.
[I don't have wifi, No problem, make your phone as wifi spot and make your system to join that network. ]
Why to do this, we can do ever other way:
We can use bluetooth, but this is faster.
We can configure web server, but why to do lots of that work too? Need to install webserver and configure too.
So... Here is the steps:
steps1:
cd to the directory where you have the file located.
start python SimpleHTTPServer on any port. [Make sure, you don't have firewall blocking this]
python -m SimpleHTTPServer 60000
Step2:
On your smart phone open web browser, and give the ip address of your system:<port>
example: my laptop ip 192.169.1.100 and I am running the python http server on 60000, then on my smart phone web browser:
http://192.168.1.100:60000/
Here you go, you can see all your files, that you want to copy. Select the file and do "save as link" and the file is copied to your smartphone.
Tuesday, 9 February 2016
jenkins_with_api
How we can use jenkins with the api.
Lets say we have a job called "test_job" and your username is "amund" and your token is "my_long_token_string" and your "test_job" is parameter driven, then:
consider your patameter name is: "name".
command example:
curl -X POST http://amund:my_long_token_string@jenkins_server:8080/job/test_job/build --data-urlencode json='{"parameter":[{"name":"name","value":"your value"}]}'
NOTE:
http://username:token@IP:port/job/job_name/build --data-urlencode json='{"parameter":[{"name":"name","value":"your value"}]}'
You can also use your username and password, but its not recommended:
curl -X POST --user <username>:<password> http://jenkins_server/job/your_job/build --form json='{"parameter":[{"name":"given_name","value":"your_value"}]}'
OR
curl -X POST --user <username>:<password> http://jenkins_server/job//your_job/build --form json='{"parameter":[{"name":"given_name","value":"your_value"}]}'
If you want to create a script on the top of the curl call, you can do that, note that within the json if you are using a variable you need to use as example:
example:
IPADDRESS=$@
in general we can use as ${IPADDRESS} or $IPADDRESS
inside json data, we need to use as:
"'"$IPADDRESS"'"
which is: " then ' then " and close the same.
example:
curl -X POST http://${USER}:${TOKEN}@${JENKINS_SERVER}/${JOB} --data-urlencode json='{"parameter":[{"name":"IPs","value":"'"$IPADDRESS"'"}]}'
Lets say we have a job called "test_job" and your username is "amund" and your token is "my_long_token_string" and your "test_job" is parameter driven, then:
consider your patameter name is: "name".
command example:
curl -X POST http://amund:my_long_token_string@jenkins_server:8080/job/test_job/build --data-urlencode json='{"parameter":[{"name":"name","value":"your value"}]}'
NOTE:
http://username:token@IP:port/job/job_name/build --data-urlencode json='{"parameter":[{"name":"name","value":"your value"}]}'
You can also use your username and password, but its not recommended:
curl -X POST --user <username>:<password> http://jenkins_server/job/your_job/build --form json='{"parameter":[{"name":"given_name","value":"your_value"}]}'
OR
curl -X POST --user <username>:<password> http://jenkins_server/job//your_job/build --form json='{"parameter":[{"name":"given_name","value":"your_value"}]}'
If you want to create a script on the top of the curl call, you can do that, note that within the json if you are using a variable you need to use as example:
example:
IPADDRESS=$@
in general we can use as ${IPADDRESS} or $IPADDRESS
inside json data, we need to use as:
"'"$IPADDRESS"'"
which is: " then ' then " and close the same.
example:
curl -X POST http://${USER}:${TOKEN}@${JENKINS_SERVER}/${JOB} --data-urlencode json='{"parameter":[{"name":"IPs","value":"'"$IPADDRESS"'"}]}'
Monday, 14 December 2015
perl mysql connection note
### From mysql side ###
NOTE:
1. Install mysql server.
2. update /etc/mysql/my.cnf to listen outside from the localhost.
3. You should have a user with the access to a database, By default you can connect to remote mysql database as root user.
4. The user should have the access to connect from remote host.
https://www.digitalocean.com/community/tutorials/how-to-create-a-new-user-and-grant-permissions-in-mysql
# creating a test database
CREATE DATABASE test;
# created a mysql user [ For this example, I have created an mysql user ]
CREATE USER ‘mysql'@'%' IDENTIFIED BY 'password';
# Grant the mysql user to access database [ Not secure example but, just for example ]
GRANT ALL PRIVILEGES ON * . * TO 'mysql@'%';
# Enable the mysql server to listen from outside
/etc/mysql/my.cnf
#bind-address = 127.0.0.1
##########################################################
##########################################################
### From perl side ###
#!/usr/bin/perl
use DBI;
use strict;
my $host = ‘mysql_db_server';
my $driver = "mysql";
my $database = "test";
my $dsn = "DBI:$driver:$database;host=$host";
my $userid = "mysql";
my $password = "password";
my $dbh = DBI->connect($dsn, $userid, $password ) or die $DBI::errstr;
NOTE: you need to have DBI::mysql module in your host.
####### another example ######
#!/usr/bin/perl
use DBI;
use strict;
my $host = ‘mysql_db_server';
my $driver = "mysql";
my $database = "test";
my $dsn = "DBI:$driver:$database;host=$host";
my $userid = "mysql";
my $password = "password";
my $dbh = DBI->connect($dsn, $userid, $password ) or die $DBI::errstr;
#
#
my $query="show databases";
print "\n\UFollowing is the output of query: \"$query\"\n\n";
my $sth=$dbh->prepare($query);
$sth->execute();
while (my @row=$sth->fetchrow_array)
{
print $row[0]."\n";
}
#
#
$query="show tables";
print "\n\UFollowing is the output of query: \"$query\"\n\n";
$sth=$dbh->prepare($query);
$sth->execute();
while (my @row=$sth->fetchrow_array)
{
print $row[0]."\n";
}
#
#
$query="describe domains";
print "\n\UFollowing is the output of query: \"$query\"\n\n";
$sth=$dbh->prepare($query);
$sth->execute();
while (my @row=$sth->fetchrow_array)
{
print $row[0]."\n";
}
$sth->finish;
## Note: close, only at the end. No need to close in middle.
### output of the above example ###
FOLLOWING IS THE OUTPUT OF QUERY: "SHOW DATABASES"
information_schema
mysql
performance_schema
test
FOLLOWING IS THE OUTPUT OF QUERY: "SHOW TABLES"
domains
FOLLOWING IS THE OUTPUT OF QUERY: "DESCRIBE DOMAINS"
id
name
url
descr
Ref links
http://aruljohn.com/code/perl/mysqlselect.html
Friday, 11 December 2015
Using ssh-agent
Using ssh-agent
ssh-agent -s > AmundSshAgent
source AmundSshAgent
ssh-add <key>
ssh-agent -s > AmundSshAgent
source AmundSshAgent
ssh-add <key>
Sometime, we need to perform some daily task, that need grant access. In these case, we can use the following few tricks to make use along with cronjobs.
[step1]
$ ssh-agent -s > sshagent_amit
[step2] [you should see something like following output. ]
$ source sshagent_amit
$ cat sshagent_amit
SSH_AUTH_SOCK=/tmp/ssh-iHFFf28257/agent.28257; export SSH_AUTH_SOCK;
SSH_AGENT_PID=28258; export SSH_AGENT_PID;
echo Agent pid 28258;
[step3]
ssh-add <your_keys>
[step3]
ssh-add <your_keys>
NOTE: After the system got restared, you need to update your agent file, or you can automate this one too.
Now you can just logout, and login again and source the file [ example: sshagent_amund] and then, you should see your keys, when you type "ssh-add -L"
[step4]
The script that we have plans to add into the crontab.
#!/bin/bash
source sshagent_amund
ssh amit@remote_host "command" > output_file.txt
In cron entry:
* * * * * /bin/bash -l -c '/path/to/your/script'
-l Make bash act as if it had been invoked as a login shell (see INVO-CATION below).
-c Command
update the cron time as per your need.
Wednesday, 11 November 2015
sudo su as
Type 1:
cat ScriptFile
whoami
sudo su - user2 <<EOF
whoami
EOF
$ ssh user1@RemoteHost 'bash -s' < ScriptFile
Output:
user1
user2
Stderr: Pseudo-terminal will not be allocated because stdin is not a terminal.
using pssh:
[ in this approach: (-l) no need to copy file to remote host to run the command. ]
$ pssh -i -H RemoteHost -l user1 -o OutPutDir -I 'bash -s'< ScriptFile
[1] 19:28:14 [SUCCESS] RemoteHost
user1
user2
$ pssh -i -H RemoteHost -l user1 -o OutPutDir -I < ScriptFile
Output:
[1] 19:27:30 [SUCCESS] RemoteHost
user1
user2
Stderr: Pseudo-terminal will not be allocated because stdin is not a terminal.
NOTE: [ quick pssh help]
cat ScriptFile
whoami
sudo su - user2 <<EOF
whoami
EOF
$ ssh user1@RemoteHost 'bash -s' < ScriptFile
Output:
user1
user2
Stderr: Pseudo-terminal will not be allocated because stdin is not a terminal.
using pssh:
[ in this approach: (-l) no need to copy file to remote host to run the command. ]
$ pssh -i -H RemoteHost -l user1 -o OutPutDir -I 'bash -s'< ScriptFile
[1] 19:28:14 [SUCCESS] RemoteHost
user1
user2
$ pssh -i -H RemoteHost -l user1 -o OutPutDir -I < ScriptFile
Output:
[1] 19:27:30 [SUCCESS] RemoteHost
user1
user2
Stderr: Pseudo-terminal will not be allocated because stdin is not a terminal.
NOTE: [ quick pssh help]
-h HOST_FILE, --hosts=HOST_FILE
-H HOST_STRING, --host=HOST_STRING
-l USER, --user=USER username (OPTIONAL) [ small l ]
-p PAR, --par=PAR max number of parallel threads (OPTIONAL)
-o OUTDIR, --outdir=OUTDIR
-i, --inline inline aggregated output and error for each server
-I, --send-input read from standard input and send as input to ssh [ caps i ]
pssh -i -h hosts.txt -l user1 o OutputDir -I 'bash -s' < ScriptFile
pssh -h hosts.txt -l irb2 -o /tmp/foo uptime
Tuesday, 10 November 2015
perl html5 video page gen code
#!/usr/bin/env perl
print ("<!DOCTYPE html>\n");
print ("<html>\n");
print ("<body>\n");
my @files = `ls *.mp4`;
foreach $a (@files) {
print ("<video width=\"320\" height=\"240\" controls>\n");
print ("<source src=\"$a\" type=\"video/mp4\">\n");
print ("</video>\n");
}
print ("</body>\n");
print ("</html>\n");
print ("<!DOCTYPE html>\n");
print ("<html>\n");
print ("<body>\n");
my @files = `ls *.mp4`;
foreach $a (@files) {
print ("<video width=\"320\" height=\"240\" controls>\n");
print ("<source src=\"$a\" type=\"video/mp4\">\n");
print ("</video>\n");
}
print ("</body>\n");
print ("</html>\n");
Wednesday, 12 August 2015
php image gallery
Steps:
- create your main folder. [example pictures ] [ mkdir pictures]
- create another folder as images under pictures [ mkdir pictures/images ] [ NOTE: In the following code the subfolder name it expect as images ]
- copy the following code to pictures folder, as filename index.php
- copy all your images to pictures/images
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Gallery from Folder Demo</title>
<style type="text/css">
<!--
li{
list-style-type:none;
margin-right:10px;
margin-bottom:10px;
float:left;
}
-->
</style></head>
<body>
<ul>
<?php
$dirname = "images/";
$images = scandir($dirname);
shuffle($images);
$ignore = array(".", "..");
foreach($images as $curimg){
if(!in_array($curimg, $ignore)) {
echo "<li><a href=\"$dirname$curimg\"><img src=\"$dirname$curimg\" height=200 width=200 /></a></li>\n ";
}
}
?>
</ul>
</body>
</html>
### compress image ###
Along with the above you also might need to compress the file.
at ubuntu, you can use "convert" command to compress the image.
NOTE: the smaller the percentage the smaller is the file.
convert source.jpg -resize 20% destination.jpg
A 4MB file 20% came around 350KB file.
Subscribe to:
Posts (Atom)