pythonで時間の計算をやってみた

やりたいこと
ある時刻「aa:aa」と「bb:bb」の差分の時間を計算させたい

なんとなくpythonで試してみる。
ちょっと調べるとweb上に沢山載っているので、ありがたく参考にした。

>>> import time
>>> from datetime import datetime
>>> t1 = '10:00'
>>> t2 = '08:35'
>>> t1 - t2
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for -: 'str' and 'str'

変数に時間を入れてみて、引き算してみる。文字列とみなされるので引き算は当然エラー。

>>> time1=time.strptime(t1, '%H:%M')
>>> print (time1)
time.struct_time(tm_year=1900, tm_mon=1, tm_mday=1, tm_hour=10, tm_min=0, tm_sec=0, tm_wday=0, tm_yday=1, tm_isdst=-1)
>>> time2=time.strptime(t2, '%H:%M')
>>> time1-time2
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for -: 'time.struct_time' and 'time.struct_time'

time.strptime()という関数が文字列を時刻の形にデータを直してくれるみたい。
年月日は指定していないので、今回は無視する。これもダメ。
#tm_wday
#tm_yday
#tm_isdst
#については調べる

もう少し調べてみると、datetime.strptime()というのもあるみたい。試してみる。

>>> time3=datetime.strptime(t1, '%H:%M') - datetime.strptime(t2,'%H:%M')
>>> time3
datetime.timedelta(0, 5100)
>>> print(time3)
1:25:00
>>> print((time3)*2)
2:50:00
>>>

これは内部では秒に変換してくれているみたい。この状態で引き算してみると、行けた。

ldapでsshログインできるようにする

ldapの構築メモです。

■やりたいこと
ldapサーバを立ててユーザを作成し、
sshでログインできるようにする。

■環境
OS:Cent6.7
openldap:openldap-2.4.40-5.el6.x86_64


参考にしたサイト
サーバ:

https://genchan.net/server/1670
http://www.unix-power.net/centos7/openldap.html

クライアント:
namihira.hatenablog.com






slappasswd<適当なパスワード>

ldapサーバ<適当なパスワード>

ldifファイル保存場所
/etc/openldap/ldap_data
ドメイン設定

database bdb
suffix "dc=my-domain,dc=com"
rootdn "cn=Manager,dc=my-domain,dc=com"

■サーバ編
ldapサーバ設定

#rootユーザで操作

Configuration Backendで管理しない場合、以下をやっておくといいかも

vi /etc/sysconfig/slapd
SLAPD_OPTIONS="-f /etc/openldap/slapd.conf"

設定ファイルのテンプレートをコピー

cp /usr/share/openldap-servers/slapd.conf.obsolete slapd.conf

管理パスワードを設定、暗号化された部分をコピー

slappasswd -s <パスワード>
{SSHA}G0mrY8fyCKqEqHlLerSIf2CPyKiipMXI3

データベースファイルのテンプレートをコピー

cp /usr/share/openldap-servers/DB_CONFIG.example /var/lib/ldap/DB_CONFIG

設定ファイルの変換実施

slaptest -u -f /etc/openldap/slapd.conf -v

所有者の変更

chown -R ldap:ldap /var/lib/ldap
chown -R ldap:ldap /etc/openldap/slapd.d

slapdの起動

service slapd start
slapd を起動中: [ OK ]

■クライアント編
#rootユーザにて実施

ldapのクライアントが入っているか確認する。無い場合はいれておく

# rpm -qa |grep openldap
openldap-2.4.40-5.el6.x86_64
openldap-clients-2.4.40-5.el6.x86_64
compat-openldap-2.3.43-2.el6.x86_64
openldap-devel-2.4.40-5.el6.x86_64
openldap-servers-2.4.40-5.el6.x86_64

以下のコマンドを実施、認証関係の設定を実施

# authconfig-tui

必要な項目を[スペースキー]を使って[*]を入れていく
ldapサーバのIPアドレスを入力、ベースDNにドメイン名と入れる(192.168.1.xx/dc=my-domain,dc=comと設定)

pam関連、nsswitchの設定ファイルをいじる
/etc/pam.d/system-authを編集、「sss」部分を「ldap」に。

#%PAM-1.0
# This file is auto-generated.
# User changes will be destroyed the next time authconfig is run.
auth required pam_env.so
auth sufficient pam_unix.so nullok try_first_pass
auth requisite pam_succeed_if.so uid >= 500 quiet
#auth sufficient pam_sss.so use_first_pass
> auth sufficient pam_ldap.so use_first_pass
auth required pam_deny.so
account required pam_unix.so broken_shadow
account sufficient pam_localuser.so
account sufficient pam_succeed_if.so uid < 500 quiet
#account [default=bad success=ok user_unknown=ignore] pam_sss.so
> account [default=bad success=ok user_unknown=ignore] pam_ldap.so
account required pam_permit.so
password requisite pam_cracklib.so try_first_pass retry=3 type=
password sufficient pam_unix.so sha512 shadow nullok try_first_pass use_authtok
#password sufficient pam_sss.so use_authtok
> password sufficient pam_ldap.so use_authtok
password required pam_deny.so
session optional pam_keyinit.so revoke
session required pam_limits.so
session [success=1 default=ignore] pam_succeed_if.so service in crond quiet use_uid
session required pam_unix.so
#session optional pam_sss.so
> session optional pam_ldap.so

同じようにpassword-auth-acも編集、いじる箇所は上記と同じ

/etc/nsswitch.confも編集「sss」を「ldap」へ
nslscdサービスを起動
自動起動設定も設定

# service nslcd start
Starting nslcd: [ OK ]
# chkconfig nslcd on

ログインできるか確認

# ssh user@192.168.1.xx
Could not chdir to home directory /home/user: No such file or directory
-bash-4.1$ hostname
-bash-4.1$ whoami

→一応成功

homeディレクトリが無いので、ログインした時に作成する設定を入れる
system-auth、password-authを修正
ファイル末尾に以下を追加

session optional pam_oddjob_mkhomedir.so skel=/etc/skel umask=022

oddjobdサービスを起動

# service oddjobd start
Starting oddjobd: [ OK ]

一通りはこれで完了。

teratermで文字化けした

teraterm経由でcentos6.5をいじっていて、日本語入力したら文字化けに遭遇


LANGは以下

[root@xxxxx]# locale
LANG=en_US.UTF-8
LC_CTYPE="en_US.UTF-8"
LC_NUMERIC="en_US.UTF-8"
LC_TIME="en_US.UTF-8"
LC_COLLATE="en_US.UTF-8"
LC_MONETARY="en_US.UTF-8"
LC_MESSAGES="en_US.UTF-8"
LC_PAPER="en_US.UTF-8"
LC_NAME="en_US.UTF-8"
LC_ADDRESS="en_US.UTF-8"
LC_TELEPHONE="en_US.UTF-8"
LC_MEASUREMENT="en_US.UTF-8"
LC_IDENTIFICATION="en_US.UTF-8"
LC_ALL=

これをググって、"ja_JP.utf8"の環境に

[root@xxxxx]# locale
LANG=ja_JP.utf8
LC_CTYPE="ja_JP.utf8"
LC_NUMERIC="ja_JP.utf8"
LC_TIME="ja_JP.utf8"
LC_COLLATE="ja_JP.utf8"
LC_MONETARY="ja_JP.utf8"
LC_MESSAGES="ja_JP.utf8"
LC_PAPER="ja_JP.utf8"
LC_NAME="ja_JP.utf8"
LC_ADDRESS="ja_JP.utf8"
LC_TELEPHONE="ja_JP.utf8"
LC_MEASUREMENT="ja_JP.utf8"
LC_IDENTIFICATION="ja_JP.utf8"
LC_ALL=

で、dateコマンドで確認すると文字化けしてる・・・
色々試したけどダメ。。

最終的にteratermのフォントが原因だったことが判明。

[設定]-[フォント]で確認「consolas」とかいうのになってた
文字セット、日本語が選べないことが原因だった模様。

フォントを「MS ゴシック」に変更したところ、文字セットで日本語が選べるようになった。
フォントを変えたことにより、今まで文字化けしていた箇所がちゃんと表示されるようになった。

意識してないところに落とし穴が。。。